0%

​ 在详细介绍圣杯双飞翼布局前,我们先要了解两种布局方式具体要实现什么效果:

  • 三列布局:left、center、right
  • left、right 宽度固定
  • center 宽度自适应窗口大小

圣杯双飞翼布局就是用来实现上述效果的,但其对应的实现形式却不相同,因此被划分为两类。

圣杯布局

圣杯布局实现效果的主要思路为:

HTML

将left、center、right 统一放在一个box中。

1
2
3
4
5
6
7
8
9
<body>
<header><h1>header</h1></header>
<main>
<div class="center float">center</div>
<div class="left float">left</div>
<div class="right float">right</div>
</main>
<footer><h1>footer</h1></footer>
</body>

CSS

  1. center 宽度设为 100%,并设置 paddingLeftpaddingRight,为两侧预留位置
  2. left、center、right 都要设置浮动 float:left
  3. 设置 left 位置
    • 设置 margin-left: -100% 相对于父级宽度距离移动,从而移动到center的起始位置
    • 再设置 position: relative; left: -selfWidth 相对于自身向左移动自身宽度距离,达到最左侧
  4. 设置 right 位置
    1. 方法1
      • 设置margin-left: -selfWidth,移动到center末尾处
      • 再设置 position: relative; left: selfWidth 相对于自身向右移动自身宽度距离,达到最右侧
    2. 方法二
      • 直接设置margin-right: -selfWidth,相对于右侧向左移动自身宽度,即可达到正确位置。
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
39
40
41
42
43
44
45
46
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
width: 100vw;
height: 100vh;
color: white;
}
header,
footer {
text-align: center;
background-color: #636e72;
}
main {
padding-left: 200px;
padding-right: 150px;
}
.float {
float: left;
}
footer {
clear: left;
}
.center {
width: 100%;
background-color: #d63031;
}
.left {
width: 200px;
position: relative;
left: -200px;
margin-left: -100%;
background-color: #00b894;
}
.right {
width: 150px;
margin-right: -150px;
/*
margin-left: -150px;
position: relative;
left: 150px;
*/
background-color: #fdcb6e;
}

双飞翼布局

双飞翼布局实现效果的主要思路为:

HTML

将center单独使用一个盒子包裹起来。

1
2
3
4
5
6
7
8
9
<body>
<header><h1>header</h1></header>
<main class="float">
<div class="center">center</div>
</main>
<div class="left float">left</div>
<div class="right float">right</div>
<footer><h1>footer</h1></footer>
</body>

CSS

  1. 设置 center 的父级盒子main:宽度设为 100%
  2. 设置 center 的 marginLeftmarginRight为两侧预留位置
  3. left、main、right 都要设置浮动 float:left
  4. 设置 left 位置
    • 设置 margin-left: -100%相对于父级宽度距离移动,从而移动到最左侧
  5. 设置 right 位置
    • 设置margin-left: -selfWidth,相对于左侧向左移动自身宽度,即可达到正确位置。
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
39
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
width: 100vw;
height: 100vh;
color: white;
}
header,
footer {
text-align: center;
background-color: #636e72;
}
main {
width: 100%;
}
.float {
float: left;
}
footer {
clear: left;
}
.center {
margin-left: 200px;
margin-right: 150px;
background-color: #d63031;
}
.left {
width: 200px;
margin-left: -100%;
background-color: #00b894;
}
.right {
width: 150px;
margin-left: -150px;
background-color: #fdcb6e;
}

效果

圣杯双飞翼布局

二者对比

圣杯布局实现关键:

HTML:main 包裹 left、center、right

CSS

  • main: paddingLeft、paddingRight
  • left、center、right三者浮动
  • left: marginLeft、relative、left
  • right: marginRight

优点: HTML结构更加清晰,但css代码较多一点

双飞翼布局实现关键:

HTML:main 包裹 left、center、right

CSS

  • main设置 width:100%
  • left、center、right三者浮动
  • center 设置左右margin
  • left不用定位移动,直接margin-left:-100%
  • right设置margin-left,不同于圣杯的margin-right

优点:css代码较少,但 HTML结构不如圣杯布局清晰。

问题

有的小伙伴就要问了,这种布局的形式我是用其他形式也能够实现,上述圣杯、双飞翼布局到底有什么优势呢?

杯布局与双飞翼布局的优点:利用布局,可优先渲染主要部分

​ 细心的同学可能已经发现了,这两种布局形式都将 center也就是主要内容部分放在了当前结构的最开始位置。这样的好处就是能够优先加载center部分,如果遇到网络不佳情况也能够优先加载主要内容。

​ 如果使用其他形式,比如flex,展示顺序和html结构是相关的,就做不到优先展示center部分。

但我们可以使用grid布局实现,下面贴出代码:

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
39
40
41
42
43
44
45
46
47
48
49
50
51
<style>
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
body {
width: 100vw;
height: 100vh;
text-align: center;
}
header,
footer {
width: 100%;
background-color: #95a5a6;
}
main {
width: 100%;
height: 300px;
display: grid;
grid-template-rows: 100%;
grid-template-columns: 100px auto 200px;
position: relative;
}
.center {
grid-column: 2/3;
/*
grid-column-start: 2;
grid-column-end: 3;
*/
grid-row: 1/2;
background-color: #e74c3c;
}
.left {
grid-column: 1/2;
background-color: #1abc9c;
}
.right {
grid-column: 3/4;
background-color: #3498db;
}
</style>
<body>
<header>header</header>
<main>
<div class="center">center</div>
<div class="left">left</div>
<div class="right">right</div>
</main>
<footer>footer</footer>
</body>

最后

本文到此结束,希望对你有所帮助,我是 Ashun ,在校大学生,立志成为资深前端工程师,欢迎大家一起交流、学习。后续更新更多文章,请持续关注哦~

原创文章,文笔有限,才疏学浅,文中若有不正之处,速速告知。