html - CSS 问题 : vertically aligning label DIV

标签 html css vertical-alignment

我在对齐 <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;
}

Demo

关于html - CSS 问题 : vertically aligning label DIV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21993175/

相关文章:

html - VueJS : How to dynamically change a CSS class after a scroll position

css - 修复了不随页面滚动的 div 模态

javascript - 在 leaflet.js 中自定义放大/缩小按钮

css - 将无序列表与显示在图像下方的图像和文本垂直对齐?

html - 为什么 top 属性不适用于相对位置?

javascript - IE 中的第 n 个子选择器替换

java - 使用 ajax 从 servlet 填充下拉列表

javascript - 同步两个 Angular ui bootstrap 轮播

javascript - 使用选项卡式内容滚动页面

javascript - 垂直对齐和具有动态内容和高度的元素