2011-09-22 28 views
0

我有一个问题,其中应用于div元素的CSS边界正在它上面延伸的标记 上方(不在div标记内的span标记)。现在,我已经有了这个可以在下面的例子中可以找到一个解决办法,但我仍想知道为什么发生这种情况:CSS显示问题,其中div应用于其他元素的边界

<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> 

回答

0

它看起来像发生这种情况,因为span s的浮起。

这意味着它们被取出文档流程。

在它们下面的div然后推起来,如果透明,看起来像“包含”span

0

如果您向跨度下方的div添加“clear:left”,它将解决此问题。

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

1

请参阅w3.org上的block formatting contexts

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

相关问题