[英]baseline vertical-align, but at the top
I'm trying to get a vertical alignment similar to baseline, but at the first line of text rather than the last. 我试图获得类似于基线的垂直对齐,但是在文本的第一行而不是最后一行。 Neither
top
nor text-top
gets the alignment right. top
和text-top
都没有正确对齐。 Any ideas? 有任何想法吗? Thanks!
谢谢!
edit: This assumes that the font size is the same in the elements being aligned. 编辑:这假定在对齐的元素中字体大小相同。 See comment below by @FelipeAls on the accepted answer .
请参阅以下@FelipeAls对已接受答案的评论 。
edit: now with code sample and picture : 编辑:现在使用代码示例和图片 :
What I'm trying to do is vertically align the text in several inline-block elements so that their baselines are all in the same position. 我要做的是垂直对齐几个内联块元素中的文本,使它们的基线都处于相同的位置。 I'd rather not do something like nudging things using margins and padding, because I find whenever I do that, I ended up playing whack-a-mole with the different browsers.
我宁愿做一些像利用边距和填充来推动事情的事情,因为无论什么时候我发现,我最终都会用不同的浏览器来玩傻瓜。
HTML: 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: 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;
}
See http://jsfiddle.net/don01001100/3t8v5L1j/3/ . 见http://jsfiddle.net/don01001100/3t8v5L1j/3/ 。
I'm afraid that you can only do that with CSS box-model, such as margin
, padding
. 我担心你只能使用CSS盒子模型,比如
margin
, padding
。
This is because the different inline-block elements are aligned at the baseline, but they have different padding
/ margin
settings, so their text is aligned slightly off vertically. 这是因为不同的内联块元素在基线处对齐,但它们具有不同的
padding
/ margin
设置,因此它们的文本略微垂直对齐。 If you change the padding
and margin
of the <button>
s and the <span>
s to be the same, then it should work. 如果更改
<button>
的padding
和margin
以及<span>
是相同的,那么它应该可以工作。
EDIT1 EDIT1
Actually, now that I think of it, you might be able to manually set values to vertical-align
in pixels. 实际上,现在我想起来了,您可以手动将值设置为
vertical-align
以像素为单位)。 Experiment with it (including negative values) to see what you want. 试验它(包括负值),看看你想要什么。 It will depend on the
padding
s and margin
s of the <span>
s and <div>
s. 它取决于
<span>
和<div>
s的padding
和margin
。
EDIT2 EDIT2
Actually, vertical-align: text-top
works pretty well for me: 实际上,
vertical-align: text-top
对我来说效果很好:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.