css經典佈局——聖盃佈局

         聖盃佈局和雙飛翼佈局一直是前端面試的高頻考點,聖盃佈局的出現是來自由 Matthew Levine 在 2006 年寫的一篇文章 《In Search of the Holy Grail》。 比起雙飛翼佈局,它的起源不是源於對頁面的形象表達。在西方,聖盃是表達「渴求之物」的意思。而雙飛翼佈局則是源於淘寶的UED,能夠說是靈感來自於頁面渲染。前端

效果圖

本來錄製了一個小視頻,奈何不能上傳到博客中,視頻中經過縮放頁面能夠發現隨着頁面的寬度的變化,這三欄佈局是中間盒子優先渲染,兩邊的盒子框子寬度固定不變,即便頁面寬度變小,也不影響咱們的瀏覽。注意:爲了安全起見,最好仍是給body加一個最小寬度!面試

聖盃佈局要求

  • header和footer各自佔領屏幕全部寬度,高度固定。
  • 中間的container是一個三欄佈局。
  • 三欄佈局兩側寬度固定不變,中間部分自動填充整個區域。
  • 中間部分的高度是三欄中最高的區域的高度。

聖盃佈局的三種實現

【1】浮動安全

  • 先定義好header和footer的樣式,使之橫向撐滿。微信

  • 在container中的三列設爲浮動和相對定位(後面會用到),center要放在最前面,footer清除浮動。markdown

  • 三列的左右兩列分別定寬200px和150px,中間部分center設置100%撐滿佈局

  • 這樣由於浮動的關係,center會佔據整個container,左右兩塊區域被擠下去了flex

  • 接下來設置left的 margin-left: -100%;,讓left回到上一行最左側spa

  • 但這會把center給遮住了,因此這時給外層的container設置 padding-left: 200px;padding-right: 150px;,給left和right空出位置code

  • 這時left並無在最左側,由於以前已經設置過相對定位,因此經過 left: -200px; 把left拉回最左側orm

  • 一樣的,對於right區域,設置 margin-left: -150px; 把right拉回第一行

  • 這時右側空出了150px的空間,因此最後設置 right: -150px;把right區域拉到最右側就好了。

    body { min-width: 550px; font-weight: bold; font-size: 20px; } #header, #footer { background: rgba(29, 27, 27, 0.726); text-align: center; height: 60px; line-height: 60px; } #footer { clear: both; }

    #container { padding-left: 200px;
    padding-right: 150px;
    overflow: hidden; }

    #container .column { position: relative; float: left; text-align: center; height: 300px; line-height: 300px; }

    #center { width: 100%; background: rgb(206, 201, 201); }

    #left { width: 200px;
    right: 200px;
    margin-left: -100%; background: rgba(95, 179, 235, 0.972); }

    #right { width: 150px;
    margin-left: -150px;
    right: -150px; background: rgb(231, 105, 2); }

    #header
    #center
    #left
    #right

【2】flex彈性佈局

  • header和footer設置樣式,橫向撐滿。

  • container中的left、center、right依次排布便可

  • 給container設置彈性佈局 display: flex;

  • left和right區域定寬,center設置 flex: 1; 便可

    body { min-width: 550px; font-weight: bold; font-size: 20px; } #header, #footer { background: rgba(29, 27, 27, 0.726); text-align: center; height: 60px; line-height: 60px; } #container { display: flex; } #container .column { text-align: center; height: 300px; line-height: 300px; } #center { flex: 1; background: rgb(206, 201, 201); } #left { width: 200px; background: rgba(95, 179, 235, 0.972); } #right { width: 150px; background: rgb(231, 105, 2); }
    #header
    #left
    #center
    #right

【3】grid佈局

如上圖所示,咱們把body劃分紅三行四列的網格,其中有5條列網格線

  • 給body元素添加display: grid;屬性變成一個grid(網格)

  • 給header元素設置grid-row: 1; 和 grid-column: 1/5; 意思是佔據第一行網格的從第一條列網格線開始到第五條列網格線結束

  • 給footer元素設置grid-row: 3; 和 grid-column: 1/5; 意思是佔據第三行網格的從第一條列網格線開始到第五條列網格線結束

  • 給left元素設置grid-row: 2; 和 grid-column: 1/2; 意思是佔據第二行網格的從第一條列網格線開始到第二條列網格線結束

  • 給center元素設置grid-row: 2; 和 grid-column: 2/4; 意思是佔據第二行網格的從第二條列網格線開始到第四條列網格線結束

  • 給right元素設置grid-row: 2; 和 grid-column: 4/5; 意思是佔據第二行網格的從第四條列網格線開始到第五條列網格線結束

    body { min-width: 550px; font-weight: bold; font-size: 20px; display: grid; } #header, #footer { background: rgba(29, 27, 27, 0.726); text-align: center; height: 60px; line-height: 60px; } #header { grid-row: 1; grid-column: 1/5; } #footer { grid-row: 3; grid-column: 1/5; } .column { text-align: center; height: 300px; line-height: 300px; } #left { grid-row: 2; grid-column: 1/2; background: rgba(95, 179, 235, 0.972); } #center { grid-row: 2; grid-column: 2/4; background: rgb(206, 201, 201); } #right { grid-row: 2; grid-column: 4/5; background: rgb(231, 105, 2); }
    #header
    #left
    #center
    #right

文章每週持續更新,能夠微信搜索「 前端大集錦 」第一時間閱讀,回覆【視頻】【書籍】領取200G視頻資料和30本PDF書籍資料

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息