圣杯布局(双飞翼布局)

圣杯布局

圣杯布局与双飞翼布局针对的都是三列左右栏固定中间栏边框自适应的网页布局

  • 三列布局,两边固定宽度,中间自适应
  • 中间栏要在浏览器优先渲染
    main元素必须是container的第一个元素
  • 允许任意列的高度最高
1
2
3
4
5
6
<div class="container">
<!--中间栏需要优先渲染-->
<div class="main"></div>
<div class="left"></div>
<div class="right"></div>
</div>

相对布局

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
.container {
/* width: 100%; */
box-sizing: border-box;
min-height: 300px;
padding: 0 60px;
}

.container > div {
position: relative;
float: left;
}

.left, .right {
width: 60px;
height: 100%;
}

.left {
left: -60px;
margin-left: -100%;
}

.right {
right: 0;
margin-right: -100%;
}

.main {
width: 100%;
height: 100%;
}


See the Pen
gJvXpO
by 王小贱 (@wangxiaojian)
on CodePen.

flex布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.container {
width: 100%;
min-height: 300px;
display: flex;
}
.main {
flex-grow: 1;
}
.left {
order: -1;
flex-basis: 60px;
}
.right {
flex-basis: 60px;
}

绝对布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
.container {
width: 100%;
min-height: 300px;
position: relative;
}
.container > div {
position: absolute;
}
.left, .right {
width: 60px;
height: 100%;
}
.main {
width: calc(100% - 120px);
height: 100%;
left: 60px;
}
.left {
left: 0;
}
.right {
right: 0;
}
# CSS

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×