CSS 显示问题:应用于 div 的边框围绕其他元素拉伸

发布于 2024-12-06 04:49:34 字数 2136 浏览 0 评论 0原文

我有一个问题,应用于 div 元素的 css 边框在跨度周围拉伸 位于其正上方的标签(不在 div 标签内的 span 标签)。现在,我已经有一个解决方法,可以在以下示例中找到,但我仍然想知道为什么会发生这种情况:

<html>
<head></head>

<body>
<span style="float: left;">(Floated Span)</span>
<div style="border: 1px solid black;">
    This is the only text which should have a border around it.
</div>
<span style="float: left;">(Floated Span)</span>
<br />

<br />
I do NOT expect the border from the div tag to stretch around the floated span, but it does.
<br />
Therefore, I would expect the floated span below the div tag to do the same, but it doesn't.
<br />
Happens in FF and IE.
<br />
<br />

<span style="float: left;">(Floated Span)</span>
<br />

<div style="border: 1px solid black;">
    This is the only text which should have a border around it.
</div>

<span style="float: left;">(Floated Span)</span>
<br />

<br />
Apparently BR tags are magical and solve the problem for whatever reason.
<br />
Works in FF and IE.
<br />

<br />
<span>(Span)</span>
<span style="float: left;">(Floated Span)</span>

<div style="border: 1px solid black;">
    This is the only text which should have a border around it.
</div>

<span style="float: left;">(Floated Span)</span>
<br />

<br />
If an unstyled span is added before the floated span, Firefox displays the content the way I expect.
<br />
However, IE still decides to stretch the border from the div tag around the floated span.
<br />

<br />
<span style="float: left;">(Floated Span)</span>    
<span>(Span)</span>

<div style="border: 1px solid black;">
    This is the only text which should have a border around it.
</div>

<span style="float: left;">(Floated Span)</span>

<br />

<br />
Switching the order of the floated span and unstyled span in the code 'fixes' the previous issue with IE.
</p>
</body>
</html>

I have an issue where the css border applied to a div element is stretching around the span
tag directly above it (a span tag which is NOT within the div tag). Now, I already have a workaround for this which can be found in the following example but I would still like to know why this is happening:

<html>
<head></head>

<body>
<span style="float: left;">(Floated Span)</span>
<div style="border: 1px solid black;">
    This is the only text which should have a border around it.
</div>
<span style="float: left;">(Floated Span)</span>
<br />

<br />
I do NOT expect the border from the div tag to stretch around the floated span, but it does.
<br />
Therefore, I would expect the floated span below the div tag to do the same, but it doesn't.
<br />
Happens in FF and IE.
<br />
<br />

<span style="float: left;">(Floated Span)</span>
<br />

<div style="border: 1px solid black;">
    This is the only text which should have a border around it.
</div>

<span style="float: left;">(Floated Span)</span>
<br />

<br />
Apparently BR tags are magical and solve the problem for whatever reason.
<br />
Works in FF and IE.
<br />

<br />
<span>(Span)</span>
<span style="float: left;">(Floated Span)</span>

<div style="border: 1px solid black;">
    This is the only text which should have a border around it.
</div>

<span style="float: left;">(Floated Span)</span>
<br />

<br />
If an unstyled span is added before the floated span, Firefox displays the content the way I expect.
<br />
However, IE still decides to stretch the border from the div tag around the floated span.
<br />

<br />
<span style="float: left;">(Floated Span)</span>    
<span>(Span)</span>

<div style="border: 1px solid black;">
    This is the only text which should have a border around it.
</div>

<span style="float: left;">(Floated Span)</span>

<br />

<br />
Switching the order of the floated span and unstyled span in the code 'fixes' the previous issue with IE.
</p>
</body>
</html>

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

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

发布评论

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

评论(3

池予 2024-12-13 04:49:34

请参阅 w3.org 上的阻止格式化上下文

在块格式化上下文中,每个框的左外边缘接触包含块的左边缘(对于从右到左的格式化,右边缘接触)。即使存在浮动,情况也是如此(尽管盒子的线框可能会由于浮动而缩小)

See block formatting contexts on w3.org.

In a block formatting context, each box's left outer edge touches the left edge of the containing block (for right-to-left formatting, right edges touch). This is true even in the presence of floats (although a box's line boxes may shrink due to the floats)

哆兒滾 2024-12-13 04:49:34

看起来发生这种情况是因为 span 正在浮动。

这意味着它们已从文档流中删除。

然后,它们下面的 div 会向上推,如果是透明的,看起来就像“包含”span

It looks like this is happening because the spans are being floated.

This means they are taken out of the document flow.

The div underneath them then pushes up, and if transparent, would look like it "includes" the span.

围归者 2024-12-13 04:49:34

如果您在跨度下方的 div 添加“clear: left”,则可以解决此问题。

这个问题是因为 span 是浮动的,并且 div 在渲染之前没有清除任何东西,所以 span 浮动在下面的 div 上。

If you add a 'clear: left' to the div below the span it will fix this issue.

This issue is because of the span being floated and the div doesn't clear anything out of the way before rendering so the span floats over the div below.

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