【发布时间】:2017-09-17 08:03:46
【问题描述】:
我有 198px-198px 的方形按钮。每个按钮下方都有一个图像和文本。由于并非所有图像都是方形的,因此我将所有图像的 max-width 和 max-height 设置为 128px,以便它们按比例缩放。不幸的是,我不能再将文本与底部中心对齐了。
我不能使用padding-top: 65px,它解决了其他一些 SO 问题,因为图像的高度可变。
我在文本的范围内尝试了position: absolute; top: 180px,但在那之后我无法让它居中(不能只指定left: 10px,因为文本的长度可变)
感谢您的帮助。
这里是一个 JSFiddle https://jsfiddle.net/vbgg3keu/3/(注意:你可以扩大输出面板的宽度,让所有的按钮排成一行)
HTML:
<button class="square-button">
<img class="small-img" src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"/><br/>
<span class="button-text">This text is too high</span>
</button>
<button class="square-button">
<img class="small-img" src="http://etc.usf.edu/clipart/36400/36459/ruler1_36459_lg.gif"/><br/>
<span class="button-text">This text is too high</span>
</button>
<button class="square-button">
<img class="small-img" src="https://tall.life/wp-content/uploads/2013/04/Giraffe.jpg"/><br/>
<span class="button-text">This text is the correct height</span>
</button>
<button class="square-button">
<img class="small-img" src="https://lh3.googleusercontent.com/dB3Dvgf3VIglusoGJAfpNUAANhTXW8K9mvIsiIPkhJUAbAKGKJcEMPTf0mkSexzLM5o=w300"/><br/>
<span class="button-text">This text is the correct height</span>
</button>
CSS:
.square-button {
width: 198px;
height: 198px;
float: left;
}
.small-img {
max-width: 128px;
max-height: 128px;
}
.button-text {
// this doesn't work because now I can't center the text
//position: absolute;
//top: 180px;
}
【问题讨论】:
标签: html css text-alignment text-align