【发布时间】:2015-02-10 22:09:39
【问题描述】:
我有一个固定大小的盒子,里面有两个元素,一个图像和一些文本。两者的宽度和高度都是可变的,我试图在图像下方的剩余空间内垂直对齐文本。
|-------- 184px --------|
(text-align:center)
+-----------------------+ - -
| +-------+ | | |
| |#######| | | |
| |#######| | | | ?
| |#######| | | 1 |
| +-------+ | | 6 |
| - - - - - - - - - - - | | 8 -
| | | p |
| Some vertically | | x |
| aligned text with | | | *
| variable length | | |
| | | |
+-----------------------+ - -
-
line-height不能使用,因为文本最多可以有三行 -
vertically-align似乎只适用于固定的height - JavaScript 总是为
img返回height:0(无论如何最好使用纯 CSS 解决方案)
HTML:
<div class="box boxed js-box">
<img src="/images/box-icon1.png">
<span class="text short js-box-text">
Title
</span>
<span class="text long js-box-text">
Detailled description when hovered
</span>
</div>
风格:
.box {
background: #FFF;
cursor: default;
height: 168px;
margin-top: 30px;
padding: 4px;
text-align: center;
width: 184px;
}
.box img {
border: 0;
display: block;
margin: auto;
margin-bottom: 24px;
position: relative;
top: 16px;
width: 76px;
}
.box .text {
bottom: 4px;
font-size: 12px;
position: relative;
}
.box .text.short {
font-size: 16px;
top: 8px;
}
.box .text.long {
display: none;
font-size: 14px;
}
【问题讨论】:
标签: javascript jquery html css vertical-alignment