11

我有一个浮动<div>在另一个里面<div>,包含的 div 有一个黑色边框......

问题是浮动<div>实际上并没有占据它的高度(大约 600 像素)左右,因此<div>带有边框的包含最终像 20 像素一样高,边框直接穿过内部 div。

我如何让内部 div 占据它应该占据的空间,同时仍然让它浮动?

这是我的来源:

<div style="border:1px solid black">
    <div style="float:left;height:200px;"></div>
</div>
4

1 回答 1

22

使用micro-clearfix方法:

<div style="border:1px solid black" class="cf"> 
  <div style="float:left;height:200px;"> 
  </div> 
</div>

CSS

.cf:before,
.cf:after {
  content: " "; /* 1 */
  display: table; /* 2 */
}

.cf:after {
  clear: both;
}

/**
* For IE 6/7 only
* Include this rule to trigger hasLayout and contain floats.
*/
.cf {
  *zoom: 1;
}
于 2013-04-16T04:07:52.460 回答