我在对齐 <label>
时遇到问题放在 div 中。
这是我的 HTML:
<div class="one-whole index-border">
<div class="score red score--primary">
<label>20</label>
</div>
</div>
这是我的 CSS:
.one-whole {
100%;
}
.index-border {
border-bottom: 2px solid #D2D2D2;
}
.score {
border: none;
display: inline-block;
/* margin: 0; */
line-height: 1;
width: 120px;
height: 100px;
text-align: center;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
-ms-border-radius: 6px;
-o-border-radius: 6px;
border-radius: 6px;
-webkit-box-shadow: 0 0 4px rgba(51, 51, 51, 0.125);
-moz-box-shadow: 0 0 4px rgba(51, 51, 51, 0.125);
box-shadow: 0 0 4px rgba(51, 51, 51, 0.125);
color: white;
margin-bottom: 15px;
vertical-align: middle;
}
.red {
background: #CC0000;
background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #FF3400), color-stop(100%, #CC0000));
background-image: -webkit-linear-gradient(#FF3400, #CC0000);
background-image: -moz-linear-gradient(#FF3400, #CC0000);
background-image: -o-linear-gradient(#FF3400, #CC0000);
background-image: linear-gradient(#FF3400, #CC0000);
}
.score--primary {
border: 1px solid #CC0000;
font-size: 30px;
font-weight: bold;
}
我想使用 vertical-align: middle
会工作,但没有运气。
这是一个 fiddle :http://jsfiddle.net/oampz/aH86E/
如果有任何方法可以重构我的代码,那将会有所帮助。
谢谢
最佳答案
如果您不想使用 table-cell
,您可以随时为父级设置一个 line-height
。老实说,在我知道容器的高度可能会发生变化的情况下,我更喜欢 table-cell。
它将允许内容始终居中,使用此方法您必须更改 line-height
以匹配容器的高度。
.score{
line-height: 100px;
}
关于html - CSS 问题 : vertically aligning label DIV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21993175/