2011-09-22 42 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

在塊格式化上下文中,每個框的左外邊緣與包含塊的左邊緣接觸(用於從右到左格式化,右邊緣接觸)。即使在漂浮物存在的情況下也是如此(儘管由於漂浮物,盒子的線盒可能收縮)