“overflow”是怎么消除浮动元素对环绕行框的影响?

为元素设置“float”样式之后,元素会脱离标准文档流,不再占据原来的空间。后续元素会向前移动,占据这个新的空间。后续的文本会围绕着浮动元素分布,形成一种环绕布局的现象。

<!DOCTYPE html>
<html>
<head>
   <title>test page</title>
   <meta charset="utf-8">
   <style type="text/css">
      #ele-1 {
              width: 100px;
              height: 50px;
              background-color: #3E3;
              float: left;
             }
     #ele-2 {
              width: 500px;
              background-color: #EE3;
            }
   </style>
</head>
<body>
  <div id="ele-1">Div area!</div>
  <p id="ele-2">
     This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!This is a paragrapha!
  </p>
</body>
</html>

显示效果如下:
图片描述

给P元素添加样式“overflow:hidden”之后,显示效果如下:

图片描述

请问这个是什么原理啊?

阅读 5k
3 个回答

这个问题牵扯到BFC(块级格式化上下文)。楼上的说法没有说到这个根本。

1、触发BFC的条件:

    float元素;
    overflow:auto,scroll,hidden,
    display :inline-block,table,table-caotion
    position:absolute,fixed

所以overflow:hidden;是会触发BFC的。
再来看看,触发BFC的结果:

2、布局规则(BFC的性质)

    里面元素不影响外面,完全独立
    从上到下,同一父BFC内margin重叠
    BFC不与浮动元素重叠,浮动参与高度计算、
    内部BFC margin 贴紧 外部border 左或右
    
   

简单来说BFC就是自己内部无论怎么布局都不会影响外面的其他元素。
这样一来答案就清晰了,overflow:hidden触发了BFC,导致自己内部浮动元素高度参与计算,从而避免了高度塌陷。(BFC与清除浮动密不可分的关系)

引申:

IE的类似概念是layout。

3、hasLayout触发条件

 display: inline-block
height: (任何值除了auto) 通常用 _height:1%;解决IE6的问题,height:1%不会改变实际高度
float: (left 或 right)
position: absolute
width: (任何值除了auto)
writing-mode: tb-rl
zoom: (除 normal 外任意值)

另外:BFC与清除浮动有密不可分的联系,与本题不符请自行参考学习。了解BFC布局机制能够解决和避免很多自己觉得“奇怪”的问题。

新手上路,请多包涵

刚刚看到一个,别人实验的看法,还不错,下面是内容
overflow:hidden这个CSS样式是大家常用到的CSS样式,但是大多数人对这个样式的理解仅仅局限于隐藏溢出,而对于清除浮动这个含义不是很了解。一提到清除浮动,我们就会想到另外一个CSS样式:clear:both,我相信对于这个属性的理解大家都不成问题的。但是对于“浮动”这个词到底包含什么样的含义呢?我们下面来详细的阐述一下。

这是一个常用的div写法,下面我们来书写样式。大家可以在DMX中自己做试验

#box{

      width:500px; 

      background:#000; 

      height:500px;

}

content {

      float:left; 

      width:600px; 

      height:600px; 

      background:red;

}

  给box这个div加了一个overflow:hidden这个属性解决了这个问题。我们直到overflow:hidden这个属性的作用是隐藏溢出,给box加上这个属性后,我们的content 的宽高自动的被隐藏掉了。另外,我们再做一个试验,将box这个div的高度值删除后,我们发现,box的高度自动的被content 这个div的高度值给撑开了。说到这里,我们再来理解一下“浮动”这个词的含义。我们原先的理解是,在一个平面上的浮动,但是通过这个试验,我们发现,这不仅仅是一个平面上的浮动,而是一个立体的浮动!也就是说,当content 这个div加上浮动这个属性的时候,在显示器的侧面,它已经脱离了box这个div,也就是说,此时的content 的宽高是多少,对于已经脱离了的box来说,都是不起作用的。当我们全面的理解了浮动这个词的含义的时候,我们就理解overflow:hidden这个属性中的解释,清除浮动是什么意思了。也就是说,当我们给box这个div加上overflow:hidden这个属性的时候,其中的content 等等带浮动属性的div的在这个立体的浮动已经被清除了。这就是overflow:hidden这个属性清除浮动的准确含义。当我们没有给box这个div设置高度的时候,content 这个div的高度,就会撑开box这个div,而在另一个方面,我们要注意到的是,当我们给box这个div加上一个高度值,那么无论content 这个div的高度是多少,box这个高度都是我们设定的值。而当content 的高度超过box的高度的时候,超出的部分就会被隐藏。这就是隐藏溢出的含义!

这个是因为在你设置了overflow后,p元素有了块级上下文,没有明确规定高度的情况下,它会计算内容全部高度,而不是像原来那样计算p元素一行的line boxes高度,从而导致了你所说情况的发生.
个人理解而已,推荐去看看张大大的文章.
http://www.zhangxinxu.com/wor...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题