给一个方块开启 BFC
<div
>
<div style={{
backgroundColor: 'blue'
}}>
<div
style={{
width: 200,
height: 200,
background: 'rosybrown',
margin: '60px auto',
}}
></div>
</div>
<div
style={{
overflow: 'hidden', // 开启BFC
backgroundColor: 'green',
}}
>
<div
style={{
width: 200,
height: 200,
background: 'rosybrown',
margin: '60px auto',
}}
></div>
</div>
</div>
<div
>
<div style={{
backgroundColor: 'blue'
}}>
<div
style={{
width: 200,
height: 200,
background: 'rosybrown',
margin: '60px auto',
}}
></div>
<div
style={{
width: 200,
height: 200,
background: 'rosybrown',
margin: '60px auto',
}}
></div>
</div>
<div
style={{
backgroundColor: 'green',
}}
>
<div
style={{
width: 200,
height: 200,
background: 'rosybrown',
margin: '60px auto',
}}
></div>
<div
style={{
overflow: 'hidden', // 开启BFC,这里也解决了溢出问题
}}
>
<div
style={{
width: 200,
height: 200,
background: 'rosybrown',
margin: '60px auto',
}}
></div>
</div>
</div>
</div>
<div
style={{
overflow: 'hidden',
background: 'gray',
}}
>
<div
style={{
width: 200,
height: 200,
background: 'rosybrown',
float: 'left', // 浮动会塌陷,绝对定位不会
}}
></div>
</div>