JavaScript圣杯布局與雙飛翼布局實現(xiàn)案例詳解
一、圣杯布局和雙飛翼布局的功能介紹
- 圣杯布局和雙飛翼布局是三欄布局中的兩種布局方式,他們實現(xiàn)的效果是相同的,區(qū)別就是實現(xiàn)方法。
- header和footer各自占領屏幕所有寬度,高度固定;
- 中間的outer是一個三欄布局;
- 三欄布局中l(wèi)eft和right不變,center填充其他地方;
- 中間部分的高度是三欄中最高的區(qū)域的高度。

二、圣杯布局
高度如何自適應屏幕高度
- 其實這個并不是圣杯布局中的要求,圣杯布局是可以指定高度的,但是可以作為一個思考;
- 方法就是使用flex布局,將主軸設置為縱軸,再將outer的flex設為1,意思就是填充多余空白,即可達到自適應屏幕高度的效果。
body{
display: flex;
width: 100%;
height: 100%;
flex-direction: column;
}
.header{
background-color:grey;
height: 50px;
}
.footer{
background-color:grey;
height: 50px;
}
.outer{
flex:1;
}圣杯布局思路
- 將center盒子寫在最前面,保證center盒子最先渲染;
- 給outer盒子指定padding-left 和 padding-right值,留出left和right的位置;
- 三個盒子都設置float:left,這時left和right就會被擠到下一行;
- left設置margin-left:-100%;相對定位+left:-left的寬度;right設置margin-left=-right的寬度;相對定位+right:-right的寬度即可將兩個盒子歸位。
圣杯布局代碼
<style>
* {
padding: 0;
margin: 0;
text-align: center;
}
html{
height: 100%;
}
body{
display: flex;
flex-direction: column;
height: 100%;;
}
.header{
width: 100%;
height: 50px;
background-color:dimgray;
}
.footer{
width: 100%;
height: 50px;
background-color:dimgray;
}
.outer{
flex:1;
padding-left: 100px;
padding-right: 200px;
}
.center{
float: left;
background-color: darkslateblue;
height: 100%;
width: 100%;
}
.left{
float: left;
width: 100px;
margin-left: -100%;
background-color: burlywood;
height: 100%;
position: relative;
left: -100px;
}
.right{
float: left;
width: 200px;
margin-left: -200px;
height: 100%;
position: relative;
right: -200px;
background-color: cyan;
}
</style>
<body>
<div class="header">header</div>
<div class="outer">
<div class="center">center</div>
<div class="left">left</div>
<div class="right">right</div>
</div>
<div class="footer">footer</div>
</body>三、雙飛翼布局
雙飛翼布局的思路
- 給三個盒子都設置為左浮動;
- center的寬度設為100%;
- left設置margin-left:-100%;right設置margin-left=-right的寬度即可將兩個盒子歸位,但是將center的兩端擋住了;
- 在center盒子中再寫一個content盒子,設置margin-left和margin-right為兩側的寬度,content盒子作為內容。
雙飛翼布局的代碼
<style>
*{
padding: 0;
margin: 0;
}
html{
width: 100%;
height: 100%;
text-align: center;
}
body{
display: flex;
width: 100%;
height: 100%;
flex-direction: column;
}
.header{
background-color:grey;
height: 50px;
}
.footer{
background-color:grey;
height: 50px;
}
.outer{
flex:1;
}
.center{
float: left;
width: 100%;
background-color: darkslateblue;
height: 100%;
}
.left{
float: left;
margin-left: -100%;
width: 100px;
background-color: burlywood;
height: 100%;
}
.right{
float: left;
width: 200px;
background-color: cyan;
margin-left: -200px;
height: 100%;
}
.content{
margin-left: 100px;
margin-right: 200px;
height: 100%;
}
</style>
<body>
<div class="header">header</div>
<div class="outer">
<div class="center">
<div class="content">content</div>
</div>
<div class="left">left</div>
<div class="right">right</div>
</div>
<div class="footer">footer</div>
</body>圣杯布局和雙飛翼布局的區(qū)別
- 圣杯布局是利用padding將中間部分留出,再利用定位、margin的方式將左右盒子歸位,因此不需要外層div;
- 雙飛翼布局是先設置中間盒子的寬度為100%,再用margin移動左右盒子覆蓋了中間盒子的兩側,再將outer中間加入一個盒子,留出兩側的margin值,達到三欄布局的效果。
四、圣杯布局和雙飛翼布局面試問題
回答:圣杯布局和雙飛翼布局都可以實現(xiàn)三欄布局,即兩側寬度固定,中間自適應的效果。圣杯布局是先用padding將中間內容留出,再定位左右盒子到相應位置;而雙飛翼布局首先將中間盒子的寬度設為了100%,在定位左右盒子的時候會覆蓋中間盒子的兩端,這樣就需要在中間盒子中在定義一個盒子,并留出margin的兩側值。兩種布局都需要把center盒子寫在left和right前面,為了最先渲染。
到此這篇關于JavaScript圣杯布局與雙飛翼布局實現(xiàn)案例詳解的文章就介紹到這了,更多相關JS圣杯布局與雙飛翼布局內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解BootStrap中Affix控件的使用及保持布局的美觀的方法
Affix是BootStrap中的一個很有用的控件,他能夠監(jiān)視瀏覽器的滾動條的位置并讓你的導航始終都在頁面的可視區(qū)域。本文重點給大家介紹BootStrap中Affix控件的使用及保持布局的美觀的方法,感興趣的朋友一起看看吧2016-07-07
zTree獲取當前節(jié)點的下一級子節(jié)點數(shù)實例
下面小編就為大家?guī)硪黄獄Tree獲取當前節(jié)點的下一級子節(jié)點數(shù)實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
基于BootStrap與jQuery.validate實現(xiàn)表單提交校驗功能
學習前臺后臺最開始接觸的業(yè)務都是用戶注冊和登錄,下面通過本文給大家介紹BootStrap與jQuery.validate實現(xiàn)表單提交校驗功能,感興趣的朋友一起學習吧2016-12-12

