空内联块元素的垂直对齐

发布于 2024-08-14 12:22:10 字数 549 浏览 5 评论 0原文

考虑以下 html/css:

<style>
span
{
  display:inline-block;
  width:5em;
  height:5em;
  padding:1em;
}
</style>

a
<span style="background-color:blue;">b</span>
<span style="background-color:green;"></span>
<span style="background-color:red;">c</span>
d

蓝色和红色框,以及框内部和周围的文本水平排列。空的绿色盒子则没有;它出现在其他两个框上方。如果我向绿色框中添加一些文本,这种行为就会停止,一切都会按照我想要的方式排列。

这种情况在 IE8(标准模式)、FireFox 3.0 和 Chrome 中一致发生,因此我假设空内联块元素有一些我不理解的属性。

我可以通过指定垂直对齐属性使框对齐,但四个文本值不再对齐。有什么想法吗?我被这个难住了。

Consider the following html/css:

<style>
span
{
  display:inline-block;
  width:5em;
  height:5em;
  padding:1em;
}
</style>

a
<span style="background-color:blue;">b</span>
<span style="background-color:green;"></span>
<span style="background-color:red;">c</span>
d

The blue and red boxes, plus the text both inside and surrounding the boxes lines up horizontally. The empty green box does not; it appears above the other two boxes. If I add some text to the green box, this behavior stops and everything lines up the way I want it to.

This happens consistently in IE8 (standards mode), FireFox 3.0 and Chrome, so I'm assuming there is some property of empty inline-block elements that I don't understand.

I can make the boxes line up by specifying a vertical-align property, but then the four text values are no longer aligned. Any thoughts? I'm stumped on this one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜中书 2024-08-21 12:22:10

给跨度顶部对齐,然后负边距等于您的填充,并且您不需要不间断的空格黑客。

span
{
    display:inline-block;
    width:5em;
    height:5em;
    padding:1em;
    vertical-align:top;
    margin-top: -1em;
}



a
    <span style="background-color:blue;">b</span>
    <span style="background-color:green;"></span>
    <span style="background-color:red;">c</span>
d

Give the span top alignment then negative margin equal to your padding, and you don't need the non-breaking space hack.

span
{
    display:inline-block;
    width:5em;
    height:5em;
    padding:1em;
    vertical-align:top;
    margin-top: -1em;
}



a
    <span style="background-color:blue;">b</span>
    <span style="background-color:green;"></span>
    <span style="background-color:red;">c</span>
d
木槿暧夏七纪年 2024-08-21 12:22:10

以防万一仍然可以帮助某人,另一个解决方案,强制从 CSS 中留出一些空间:

  span:empty:before {
    content: "\0020";
    display: inline-block;
  }

Just in case still helps someone, another solution, enforcing to have some space from CSS:

  span:empty:before {
    content: "\0020";
    display: inline-block;
  }
难如初 2024-08-21 12:22:10
a
<span style="background-color:blue;">b</span>
<span style="background-color:green;"> </span>
<span style="background-color:red;">c</span>
d
a
<span style="background-color:blue;">b</span>
<span style="background-color:green;"> </span>
<span style="background-color:red;">c</span>
d
倾城花音 2024-08-21 12:22:10

尝试在绿色范围内添加  

Try adding   inside of the green colored span.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文