html - 两行文本并排居中,HTML/CSS

标签 html css

所以我已经在 jsfiddle 中实现了我想要实现的目标。我想知道是否有更简单的方法来达到同样的效果:

Image reference

<div class="date"> 
<span class="day">05</span>
<div class="dateHolder">
    <span class="month">DEC</span>
     <span class="year">2013</span>
</div>

.date {
position: absolute;
font-family: Arial;
font-weight: bold;
background: rgba(0, 0, 0, 0.8);
color: #fff;
width: 110px;
height: 60px;
padding: 10px;
}
.dateHolder {
    margin-top: 10px;
}
.day {
    font-size: 50px;
    line-height: 60px;
    float: left;
}
.month, .year {
    float: right;
    font-size: 20px;
    line-height: 20px;
    display: block;
}

最佳答案

我认为布置日期的这些部分并垂直居中的最简单方法是 to use CSS table model :

.date {
    font-family: Arial;
    font-weight: bold;
    position:absolute;
    top: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.8);
    color: #fff;
    width: 110px;
    height: 60px;
    padding: 10px;
    display: table;
}
.dateHolder {  
    display: table-cell;
    vertical-align: middle;
    text-align: right;    
    font-size: 20px;
    line-height: 21px;
}
.day {
    font-size: 50px;
    line-height: 60px;
    display: table-cell;
    vertical-align: middle;
}
.dateHolder > span {
    display: block;
}

无需clearfix魔法和边距/填充调整。一切都会自动对齐。

关于html - 两行文本并排居中,HTML/CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18302719/

相关文章:

javascript - gmap3 在我的网站上不起作用

html - 您将如何将此 PSD 转换为 HTML?

html - 手动上移文本html

javascript - 如何撤消 html 表中的类更改

javascript - 垂直居中图像,基于容器高度

html - CSS 样式输入框和具有相同类的 href 链接

html - 如何在 Bootstrap 3 的 div 中用从 a 到 z 的字母对齐文本?

html - 导致导航栏元素落在导航栏之外的汉堡菜单

html - 在 react html5 应用程序中,如何并排放置 3 个选择列表?

html - 在 HTML 中混合 RTL 和 LTR 文本