avatar

目录
css3之BFC、IFC、GFC和FFC

css3之BFC、IFC、GFC和FFC

BFC

块级格式化上下文:Block Formatting Contexts
页面上的一个隔离的渲染区域,容器里面的子元素不会再布局上影响到外面的元素。
那BFC一般有什么用呢?比如常见的多栏布局,结合块级别元素浮动,里面的元素则是在一个相对隔离的环境里运行。

常见的会生成BFC的元素:
根元素
float不为none
position为absolute或fixed
display为inline-block, table-cell, table-caption, flex,inline-flex
overflow不为visible

自适应两栏布局

html
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
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>自适应两栏布局title>
head>
<style>
body {
width: 300px;
position: relative;
}

.aside {
width: 100px;
height: 150px;
float: left;
background: #f66;
}

.main {
height: 200px;
background: #ccd1ff;
}
style>

<body>
<div class="aside">div>
<div class="main">div>
body>

html>

每个元素的margin box的左边, 与包含块border box的左边相接触:因aside 存在float ,所以导致main左边对齐。

由于BFC的区域不会与float box重叠。此时,如果想要两个元素不再重叠,只需要让第二个元素形成BFC就可以了:

html
1
2
3
4
5
.main {
height: 200px;
background: #fcc;
overflow: hidden;
}

清除内部浮动

html
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
html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>清除浮动title>
head>
<style>
.par {
border: 5px solid #fcc;
width: 300px;
}

.child {
border: 5px solid #f66;
width: 100px;
height: 100px;
float: left;
}
style>

<body>
<div class="par">
<div class="child">div>
<div class="child">div>
div>
body>

html>

由于内部元素产生了浮动,所以父级元素会高度丢失,产生下面的效果:

由于计算BFC的高度时,浮动元素也参与计算,为达到清除内部浮动,我们可以触发par生成BFC,那么par在计算高度时,par内部的浮动元素child也会参与计算。

防止垂直 margin 重叠

html
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
html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>防止垂直 margin 重叠title>
head>
<style>
p {
color: #f55;
background: #ccd1ff;
width: 200px;
line-height: 100px;
text-align: center;
margin: 100px;
}
style>

<body>
<p>Hahap>
<p>Hehep>
body>

html>
html
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
html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>防止垂直 margin 重叠title>
head>
<style>
p {
color: #f55;
background: #ccd1ff;
width: 200px;
line-height: 100px;
text-align: center;
margin: 100px;
}
style>

<body>
<p>Hahap>
<p>Hehep>
body>

html>

Box垂直方向的距离由margin决定。由于属于同一个BFC的两个相邻Box的margin会发生重叠,因此,上面的两个p元素就会产生margin重叠。

html
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
34
html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>防止垂直 margin 重叠title>
head>
<style>
p {
color: #f55;
background: #ccd1ff;
width: 200px;
line-height: 100px;
text-align: center;
margin: 100px;
}

.wrap {
overflow: hidden;
}
style>

<body>

<div class="wrap">
<p>Hahap>

div>
<p>Hehep>
body>

html>

为了避免这种margin重叠,我们可以在p外面包裹一层容器,并触发该容器生成一个BFC。那么两个P便不属于同一个BFC,就不会发生margin重叠了。

IFC

IFC(Inline Formatting Contexts)直译为”内联格式化上下文”,IFC的linebox(线框)高度由其包含行内元素中最高的实际高度计算而来(不受到竖直方向的padding/margin影响)

FFC

FFC(Flex Formatting Contexts)直译为”自适应格式化上下文”,display值为flex或者inline-flex的元素将会生成自适应容器(flex container)。

GFC

GFC(GridLayout Formatting Contexts)直译为”网格布局格式化上下文”,当为一个元素设置display值为grid的时候,此元素将会获得一个独立的渲染区域,我们可以通过在网格容器(grid container)上定义网格定义行(grid definition rows)和网格定义列(grid definition columns)属性各在网格项目(grid item)上定义网格行(grid row)和网格列(grid columns)为每一个网格项目(grid item)定义位置和空间。

文章作者: 会吃鱼的猫
文章链接: https://lovecatdog.github.io/2020/04/13/css3%E4%B9%8BBFC%E3%80%81IFC%E3%80%81GFC%E5%92%8CFFC/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 LoveCatDog
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论
简体中文