Skip to content

Instantly share code, notes, and snippets.

@clinyong
Created March 9, 2016 23:17
Show Gist options
  • Select an option

  • Save clinyong/a171b1b78b3b83a744ef to your computer and use it in GitHub Desktop.

Select an option

Save clinyong/a171b1b78b3b83a744ef to your computer and use it in GitHub Desktop.
清除浮动两种方法的差异
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>clear与margin重叠</title>
</head>
<body>
1.
<div style="background-color: #f5f5f5;">
<img src="http://img.mukewang.com/53d60af3000171a002560191.jpg" style="float:left;">
<div style="clear:both; margin-bottom:100px;">clear:both;</div>
</div>
<div style="margin-top: 100px;">本文字离图片的距离是?</div>
<br><br><br><br>2.
<div style="background-color: #f5f5f5; overflow: hidden;">
<img src="http://img.mukewang.com/53d60af3000171a002560191.jpg" style="float:left; margin-bottom: 100px;">
</div>
<div style="margin-top: 100px;">本文字离图片的距离是?</div>
</body>
</html>
@clinyong
Copy link
Author

clinyong commented Mar 9, 2016

第一种是在浮动元素后面加上 clear:both 的元素,这时候的 margin 会与外部产生重叠。

第二种是在浮动元素的父元素加上 overflow:hidden,也就是形成 BFC,这样与外部就隔绝了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment