1.给父元素设置overflow:hidden,清除浮动带来的影响
2.设置高度
3.在父元素最后加一个子元素,clear:both
4.单伪元素清除浮动
.clearfix::after{ content: ''; display: block; clear: both; height: 0; visibility: hidden; }
5.双伪元素,可解决外边距塌陷
.clearfix::before, .clearfix::after{ content: ''; display: table; } .clearfix::after{ clear: both; }