简体   繁体   English

关于css float为div1,div2的外观?

[英]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. 每条渲染线都包含在一个单独的线框中。

  • You make #sidebar1 floating, so you put it out of the flow 你让#sidebar1浮动,所以你把它放出了流程
  • Now, #sidebar2 box can take its place 现在, #sidebar2框可以取而代之
  • But , #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.

相关问题 Css可见性属性div1悬停在另一个div(div2) - Css visibility property div1 hovering another div(div2) 使用负边距将div1粘附到div2的左侧 - Using negative margin to adhere div1 to the left of div2 在Mobil浏览器的div1下显示div2-CSS:Flex, - Show div2 under div1 on mobil browsers - css: flex, #div1的阴影散布在#div2上 - #div1's shadow spreads on #div2 Fade Div1 In / Out然后Fade Div2 In? - Fade Div1 In/Out Then Fade Div2 In? 为什么将div1位置设置为div2的顶部和左侧偏移+ div2的宽度而不是将其放置在右上角? - Why does setting div1 position to div2's top & left offset + div2's width not position it to the right top corner? 在div2 mousover上突出显示div1和div2,在div1 mouseover上不突出显示任何内容 - highlight div1 and div2 on div2 mousover, highlight nothing on div1 mouseover 如果div1和div2不存在,则CSS样式div3会有所不同 - CSS style div3 differently if div1 and div2 don't exist 左浮动2个div,当div1停止缩小时,div2开始重叠…如何防止? - 2 left floated divs, when div1 stops shrinking, div2 begins overlaying… how to prevent? 保持div2与屏幕的左侧,底部和右侧齐平,但始终位于div1上方400px以下吗? - Keep div2 flush with left, bottom, and right of screen, but always 400px below above div1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM