1-reset css清除浏览器自带的样式
2-浮动解决方案(浮动导致高度塌陷)
方法1
*{
margin:0;
padding:0;
}
方法2.
reset.css
引入。
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
浮动解决方案
1.父元素设置overflow:hidden:没有使用position时使用不能和position配合使用,因为超出的尺寸的会被隐藏
2.添加一个空div:如果页面浮动布局多,就要增加很多空div,不推荐使用
3.使用伪元素:推荐使用,建议定义公共类,以减少CSS代码(目前:大型网站都有使用,如:腾迅,网易,新浪等等)
1.父元素设置
.box{
border:5px soild skyblue;
overflow:hidden;
}
2.添加一个空div
.clear{
clear:both;
}
<div class="box">
<div class="left"></div>
<div class="right"></div>
<div class="clear"></div>
</div>
3 使用伪元素
.clearfix::after{
display:block;
clear:both;
content:"";
}
<div class="box clearfix">
<div class="left"></div>
<div class="right"></div>
</div>