gpt4 book ai didi

css - 嵌套的内联元素 'vertical-align' 在 Chrome 和 Firefox 上的行为不同

转载 作者:行者123 更新时间:2023-12-04 08:10:55 27 4
gpt4 key购买 nike

我在span里面放了一个span元素,然后设置vertical-align: 20px用于外跨和 vertical-align: top对于内跨。 HTML 和 CSS 代码在这里。
codepen

.mid {
vertical-align: 20px;
outline: 1px solid;
}

.right {
vertical-align: top;
outline: 1px solid;
}

.se {
vertical-align: top;
}
<html>

<body>
<p class="out">A<span class="mid">B<span class="right">C</span>D</span><span class="se">E</span></p>

</body>

</html>

在 Chrome 上,它显示
enter image description here
在 Firefox 上,它显示
enter image description here
哪个是标准行为?

最佳答案

如果我们关注 the specification描述我们可以阅读的行框:

As described in the section on inline formatting contexts, user agents flow inline-level boxes into a vertical stack of line boxes. The height of a line box is determined as follows:

  1. The height of each inline-level box in the line box is calculated. For replaced elements, inline-block elements, and inline-table elements, this is the height of their margin box; for inline boxes, this is their 'line-height'.
  2. The inline-level boxes are aligned vertically according to their 'vertical-align' property. In case they are aligned 'top' or 'bottom', they must be aligned so as to minimize the line box height.
  3. The line box height is the distance between the uppermost box top and the lowermost box bottom. (This includes the strut, as explained under 'line-height' below.)

在 Chrome 中,元素显然在其行框之外,所以对我来说听起来像是一个错误。如果我们添加边框,我们可以清楚地看到溢出:

.mid {
vertical-align: 20px;
outline: 1px solid;
}

.right {
vertical-align: top;
outline: 1px solid;
}

.se {
vertical-align: top;
}

p {
border:2px solid red;
}
<html>

<body>
<p class="out">A<span class="mid">B<span class="right">C</span>D</span><span class="se">E</span></p>

</body>

</html>

如果您设置 vertical-align:-20px,另一个有趣的结果

.mid {
vertical-align: -20px;
outline: 1px solid;
}

.right {
vertical-align: top;
outline: 1px solid;
}

.se {
vertical-align: top;
}

p {
border:2px solid red;
}
<html>

<body>
<p class="out">A<span class="mid">B<span class="right">C</span>D</span><span class="se">E</span></p>

</body>

</html>

Firefox 会让 C 元素粘在 p 的顶部这是逻辑结果。 Chrome 将再次以不同的方式做事,我认为这也是一个错误。

值得注意的是,如果您使 mid element inline-block 你将有一个一致的行为,因为对齐更容易,浏览器和 C 现在都会考虑 mid 中的行框并将永远坚持到顶峰

.mid {
vertical-align: -20px;
outline: 1px solid;
display:inline-block;
}

.right {
vertical-align: top;
outline: 1px solid;
}

.se {
vertical-align: top;
}

p {
border:2px solid red;
}
<p class="out">A<span class="mid">B<span class="right">C</span>D</span><span class="se">E</span></p>

<p class="out">A<span class="mid" style="vertical-align: 20px">B<span class="right">C</span>D</span><span class="se">E</span></p>


好吧,如果我们按照下面的示例进行操作,似乎对 chrome 正在做什么有一个合乎逻辑的解释

.mid {
outline: 1px solid;
}

.right {
vertical-align: top;
outline: 1px solid;
}

.se {
vertical-align: 40px;
}

p {
border: 2px solid red;
display: inline-block;
}
<p class="out">A<span class="mid" style="vertical-align:baseline">B<span class="right">C</span>D</span><span class="se">E</span></p>

<p class="out">A<span class="mid" style="vertical-align: 20px;">B<span class="right">C</span>D</span><span class="se">E</span></p>

<p class="out">A<span class="mid" style="vertical-align: 40px">B<span class="right">C</span>D</span><span class="se">E</span></p>

<p class="out">A<span class="mid" style="vertical-align:-20px">B<span class="right">C</span>D</span><span class="se">E</span></p>

似乎 chrome 首先将 C 元素放在顶部(考虑默认基线对齐到 mid )然后对齐中间元素,这会产生奇怪的结果。 C 偏移设置为 vertical-align 的像素值的 .mid元素从顶部。

关于css - 嵌套的内联元素 'vertical-align' 在 Chrome 和 Firefox 上的行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65966021/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com