hooyantsing's Blog

P78_8_OVERFLOW触发BFC清除浮动影响

字数统计: 104阅读时长: 1 min
2020/04/14

P78 8 OVERFLOW触发BFC清除浮动影响

实战案例

触发BFC,让父元素感知子元素占用空间。

代码:

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
CSS:
main {
    border: solid 3px black;
    margin: 0 auto;
    padding: 20px;
    height: auto;
    overflow: hidden;
}
div {
    width: 300px;
    height: 300px;
    box-sizing: border-box;
}
div:nth-of-type(1) {
    border: solid 3px red;
    float:left;
}
div:nth-of-type(2) {
    background: blue;
    float:right;
}

HTML:
<main>
    <div></div>
    <div></div>
</main>
CATALOG