1、使用padding改变:padding:10px;
相对应的父级高度也要改变
2、父元素使用overflow属性的hidden、auto、scroll其中一个,可以触发父级的bfc,从而解决外边距溢出的问题
3、给父元素增加上边框
4、在HTML中添加table
5、在css中添加伪元素,添加到父元素中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
width: 300px;
height: 300px;
background-color: green;
/* overflow: hidden; 2.给父元素添加overflow:hidle;*/
/* border-top: 1px solid green; 1.给父元素增加上边框*/
}
.one{
width: 150px;
height: 150px;
margin-top: 100px;
background-color: red;
}
.m::before{
content: '';
display: table;
}
/* 4.在css中添加伪元素,添加到父元素中, .m::before{
content: '';
display: table;
} */
</style>
</head>
<body>
<div class="box m">
<!-- <table></table> 3.在HTML中添加table-->
<div class="one"></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
width: 500px;
border: 2px solid red;
}
.one{
width: 200px;
height: 200px;
background-color: pink;
float: left;
}
.two{
width: 200px;
height: 200px;
background-color: green;
float: left;
}
/* .clearfix{
clear: both;
} 第一种方法*/
.clearfix::after{
content: '';
/* display: block; */
display: table;
clear: both;
}/*第二种方法*/
</style>
</head>
<body>
<div class="box clearfix">
<div class="one"></div>
<div class="two"></div>
<!-- <div class="clearfix"></div>第一种方法 -->
</div>
</body>
</html>