[英]About css float left for div1, the appearance of div2?
This is the html code. 这是html代码。
<div id="sidebar1">
sidebar1
</div>
<div id="sidebar2">
sidebar2
</div>
This is the css code for the html. 这是html的css代码。
div {
width: 100px;
height:100px;
border-style: solid; border-width: 1px 1px 1px 1px;
}
div#sidebar1 {
float: left;
}
The presentation of it looks like below in my latest firefox. 在我最新的Firefox中,它的呈现如下所示。 Why was the text 'sidebar2' not hidden by the div1? 为什么文本'sidebar2'没有被div1隐藏?
The original html looked like below. 原始的html看起来像下面。
In my opinion, due to the float left, the entire div2 will be overlapped by div1 including the text in div2 like below. 在我看来,由于浮动左边,整个div2将与div1重叠,包括div2中的文本,如下所示。
the below picture is the moment when I hover to the div2 in firebug. 下面的图片是我在firebug中盘旋到div2的那一刻。 Obviously, the text 'siderbar2' seems depart from the div2. 显然,文本'siderbar2'似乎偏离了div2。 why? 为什么?
To get the problem, you need to understand that the box is not the content . 要解决问题,您需要了解该框不是内容 。
From W3C wiki : 来自W3C维基 :
Each rendered line is enclosed in a separate line box. 每条渲染线都包含在一个单独的线框中。
#sidebar1
floating, so you put it out of the flow 你让#sidebar1
浮动,所以你把它放出了流程 #sidebar2
box can take its place 现在, #sidebar2
框可以取而代之 #sidebar2
's content (aka. first line box) is different from #sidebar2
box, and was pushed down by #sidebar1
但是 , #sidebar2
的内容(又名第一行框)与#sidebar2
框不同,并被#sidebar1
推倒 To avoid this kind of behavior, you can add overflow: hidden
on #sidebar2
, or better: float: left
. 要避免这种行为,可以在#sidebar2
上添加overflow: hidden
,或者更好: float: left
。
Many people doesn't understand float
and don't think it put the element out of the flow. 许多人不理解float
并且不认为它将元素排除在外。 The way we use it usually makes us to think it simply "puts elements next to each other". 我们使用它的方式通常会让我们认为它只是“将元素放在一起”。 And when we face an "issue", we solve it without understanding it. 当我们面对“问题”时,我们会在不了解它的情况下解决它。
This property's name is float
, not arrange
. 这个属性的名字是float
,不是arrange
。
If you want the two elements to display over each other. 如果您希望两个元素相互显示。 Change the positioning of the first one to absolute. 将第一个的定位更改为绝对。
This will take it out of the ordinary flow rendering and allow any elements to overlap with it. 这将使其脱离常规流渲染并允许任何元素与其重叠。
div {
width: 100px;
height:100px;
border-style: solid; border-width: 1px 1px 1px 1px;
position:absolute;
}
div#sidebar1 {
float: left;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.