<body>
<div class="header">header</div>
<div class="main">
<div class="content">content</div>
<div class="left">left</div>
<div class="right">right</div>
</div>
<div class="footer">footer</div>
</body>
<style>
* {
margin: 0;
padding: 0;
}
.header,.footer {
height: 100px;
background: #000;
color:seashell;
}
.main {
height: 400px;
background: #ccc;
}
.content {
height: 400px;
background: #f90;
}
.left {
width: 300px;
height: 400px;
background: purple;
}
.right {
width: 300px;
height: 400px;
background: seagreen;
}
</style>
<style>
* {
margin: 0;
padding: 0;
}
.header,.footer {
height: 100px;
background: #000;
color:seashell;
}
.main {
height: 400px;
background: #ccc;
}
.content {
height: 400px;
background: #f90;
float: left;
width: 100%;
}
.left {
width: 300px;
height: 400px;
background: purple;
float: left;
margin-left: -100%;
}
.right {
width: 300px;
height: 400px;
background: seagreen;
float: left;
margin-left: -300px;
}
</style>
這個方法中首先在main這個父盒子中不按常規的思惟,將content放在最上面(常規思惟應該是left在最上面),這也是這個方法的妙處之一。首先main裏面的三個盒子都設置爲左浮動,而後設置content的width爲100%,最後用margin-left將left和right兩個盒子分列content左右,也就實現了聖盃佈局。css
<style>
* {
margin: 0;
padding: 0;
}
.header,.footer {
height: 100px;
background: #000;
color:seashell;
}
.main {
height: 400px;
background: #ccc;
display: flex;
}
.content {
height: 400px;
background: #f90;
flex: auto;
}
.left {
width: 300px;
height: 400px;
background: purple;
flex: none;
}
.right {
width: 300px;
height: 400px;
background: seagreen;
flex: none;
}
</style>
相比於第一種方法,用flex符合屬性來作就輕鬆不少,就加了四行代碼,main變爲伸縮盒子,content,left,right依次設爲flex:auto; flex:none; flex:none;shell
說明:flex
是複合屬性,是 flex-grow
, flex-shrink
和 flex-basis
的簡寫,默認值爲 0 1 auto
。佈局
若是縮寫爲 flex: 1
, 則其計算值爲 1 1 0%
flex
若是縮寫 flex: auto
, 則其計算值爲 1 1 auto
spa
若是flex: none
, 則其計算值爲0 0 auto
code