CSS
coderljw 2024-10-13 大约 2 分钟
# 1. BFC
BFC 全称 Block Formatting Context,译为块级格式化上下文,它是一个独立的渲染区域。
BFC 里面的盒子都会以垂直方向排列,同一个 BFC 里面中相邻的两个盒子的垂直外边距会重叠。
应用:
- 解决垂直外边距重叠。
- 解决浮动导致的高度塌陷。
创建 BFC - MDN (opens new window)
overflow: hidden
:box1 中解决垂直外边距重叠,box2 中解决浮动导致的高度塌陷。<!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> .box1 { width: 200px; height: 200px; overflow: hidden; background: gold; } .box1-inner { margin-top: 50px; } .box2 { width: 200px; background: aqua; overflow: hidden; } .box2-inner { float: left; width: 200px; height: 200px; } </style> </head> <body> <section class="box box1"> <section class="box1-inner">margin</section> </section> <section class="box2"> <section class="box2-inner">float</section> </section> </body> </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
35
36
37
38
# 2. 水平垂直居中
- 行内元素
.parent { line-height: 500px; text-align: center; font-size: 0; /* 消除幽灵空白节点的bug */ } .son { vertical-align: middle; }
1
2
3
4
5
6
7
8- table-cell
.parent { display: table-cell; text-align: center; vertical-align: middle; } .son { }
1
2
3
4
5
6
7- position
.parent { position: relative; } .son { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
1
2
3
4
5
6
7
8
9.parent { position: relative; } .son { position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; }
1
2
3
4
5
6
7
8
9
10
11- flex
.parent { display: flex; } .son { margin: auto; }
1
2
3
4
5
6.parent { display: flex; align-items: center; justify-content: center; } .son { }
1
2
3
4
5
6
7.parent { display: flex; justify-content: center; } .son { align-items: center; /* align-self: center; */ }
1
2
3
4
5
6
7
8
9- grid
.parent { display: grid; } .son { margin: auto; }
1
2
3
4
5
6.parent { display: grid; place-items: center; /* place-content: center; */ /* justify-items: center; align-items: center; */ /* justify-content: center; align-content: center; */ } .son { }
1
2
3
4
5
6
7
8
9
10
11
12
13
14.parent { display: grid; } .son { place-self: center; /* justify-self: center; align-self: center; */ }
1
2
3
4
5
6
7
8
9
# 3. 移动端 1px
.box {
width: 300px;
height: 300px;
position: relative;
}
.box:after {
content: ' ';
position: absolute;
top: 0;
left: 0;
width: 200%;
height: 200%;
transform: scale(0.5);
transform-origin: left top;
box-sizing: border-box;
border: 1px solid #e5e5e5;
border-radius: 50% 10%;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 4. CSS 匹配规则
CSS 读取顺序:从右向左。
css 渲染时会先找 p,然后再找 .box 下的 p(p 的性能要比 .box p 更好)。
.box p {
color: pink;
}
p {
color: pink;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 5. 超越!important,超越最大
- 最终宽度为
700px
,min-width
>max-width
>!important
。
<!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>
.one-piece {
min-width: 700px;
max-width: 600px;
}
</style>
</head>
<body>
<img
src="https://www.coderljw.ga/images/海贼王.png"
style="width: 500px !important"
class="one-piece"
/>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22