Contents
  1. 1. 在两个盒子嵌套时候,内部的盒子设置的margin-top会加到外边的盒子上,导致内部的盒子margin-top设置失败,解决方法如下:

在两个盒子嵌套时候,内部的盒子设置的margin-top会加到外边的盒子上,导致内部的盒子margin-top设置失败,解决方法如下:

  1. 外部盒子设置一个边框
  2. 外部盒子设置 overflow:hidden
  3. 使用伪元素类:”:before” 伪元素可以在元素的内容前面插入新内容.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<style type="text/css">
/* 第三种方法*/
.clearfix:before{
content: '';
display:table;
}
.box{
width: 200px;
height: 200px;
background-color: skyblue;
margin: 50px auto 0;
/* 第一种方法
order: 1px solid #000; */
/* 第二种方法
overflow:hidden */
}
.box_in{
width: 100px;
height: 100px;
background-color: pink;
border: 1px solid #000;
margin: 50px auto 0;
}
</style>
----------------------------------------------
<body>
<div class="box clearfix">
<div class="box_in"></div>
</div>
</body>