【发布时间】:2015-11-25 14:51:40
【问题描述】:
我试图获得类似于基线的垂直对齐方式,但在文本的第一行而不是最后一行。 top 和 text-top 都没有正确对齐。有任何想法吗?谢谢!
edit:这假设字体大小在对齐的元素中是相同的。请参阅下面@FelipeAls 对已接受答案的评论。
编辑:现在带有代码示例和图片:
我正在尝试做的是垂直对齐几个内联块元素中的文本,以便它们的基线都在同一位置。我宁愿不做一些像使用边距和填充来轻推的事情,因为我发现每当我这样做时,我最终都会在不同的浏览器上玩whack-a-mole。
HTML:
<div class="foo">
<span>one</span>
<span>two two two two</span>
<span>three three three three three three three three three</span>
<button type="button">action</button>
</div>
<div class="bar">
<span>one</span>
<span>two two two two</span>
<span>three three three three three three three three three</span>
<button type="button">action</button>
</div>
CSS:
body {
font-family: sans-serif;
font-size: 12px;
}
button {
font-family: inherit;
font-size: inherit;
}
div {
position: relative;
width: 500px;
text-align: center;
}
div.foo, div.bar {
margin-bottom: 2em;
}
div span {
text-align: center;
}
div.foo span {
display: inline-block;
width: 50px;
vertical-align: top;
}
div.bar span {
display: inline-block;
width: 50px;
vertical-align: baseline;
}
div button {
display: inline-block;
width: 125px;
}
【问题讨论】:
-
请包含代码示例而不是代码链接。并且更具体一点:您想将什么元素与什么对齐?
-
@Jonathan,对此感到抱歉!我已经添加了相关的 HTML 和 CSS。 jsfiddle 里面有一些额外的代码,但是我放在这里的是相关的。