首先二者都是兩邊寬度固定,中間寬度自適應,而且中間盒子(主題內容)放在最前面,以便優先渲染。html
實現方案:都使用浮動來實現。佈局
聖盃佈局實現以下:flex
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>聖盃佈局</title>
<style>
.box{
padding: 0 100px;
height: 400px;
}
.center,
.left,
.right{
float: left;
height: 100%;
}
.left,
.right{
position: relative;
}
.center{
width: 100%;
background: #333;
}
.left{
width: 100px;
background: #f8f8f8;
margin-left: -100%; /*爲了使元素移到上一行,margin-left設置百分比是相對於父元素寬度的,這個寬度是不包括padding在內*/
left: -100px;
}
.right{
width: 100px;
background: #ccc;
margin-left: -100px;
right: -100px;
}
</style>
</head>
<body>
<div class="box">
<div class="center"></div>
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
flex實現聖盃佈局:
<!DOCTYPE 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>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
overflow: hidden;
display: flex;
/* 改變主軸的排列方式 */
flex-direction: column;
justify-content: space-between;
text-align: center;
font-size: 25px;
}
.footer,
.header {
height: 88px;
background: #c33;
text-align: center;
line-height: 88px;
font-size: 30px;
}
.center {
flex: 1;
background: #ccc;
display: flex;
}
.center>.left,.center>.right {
width: 200px;
height: 100%;
background: yellow;
}
.center>.content {
flex: 1;
background: pink;
}
</style>
</head>
<body>
<div class="header">header</div>
<div class="center">
<div class="left">left</div>
<div class="content">content</div>
<div class="right">right</div>
</div>
<div class="footer">footer</div>
</body>
</html>
雙飛翼佈局實現以下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>雙飛翼佈局</title>
<style>
.box{
height: 400px;
overflow: hidden;
}
.main-box,
.left,
.right{
float: left;
height: 100%;
}
.center{
margin-left: 100px;
margin-right: 100px;
}
.main-box{
width: 100%;
background: #333;
}
.left{
width: 100px;
background: #f8f8f8;
margin-left: -100%;
}
.right{
width: 100px;
background: #ccc;
margin-left: -100px;
}
</style>
</head>
<body>
<div class="box">
<div class="main-box"><div class="center"></div></div>
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>