前言
在做頁面時,我們往往會碰到頁面布局相關的內容,面試時也經常會被問到,那么今天我就來總結一下關于頁面布局的內容。
問題:如何實現(xiàn)三欄布局(高度固定,左中右的結構)
假設高度已知,請寫出三欄布局,其中左右寬度均為300px,中間自適應。
看了上面的題目,有經驗的人也許會覺得很簡單,仔細想想,如果我們來寫,能寫出幾種方案呢?一般都會想到兩種吧,float和position定位,其實除了這兩種外,還有3種可以寫,下面我就來一一介紹一下:
方案一(float浮動)
<section class='layout float'> <style> .layout.float .left-right-center{ height: 100px; } .layout.float .left{ float: left; width: 300px; background-color: red; } .layout.float .right{ float: right; width: 300px; background-color: blue; } .layout.float .center{ background-color: yellow; } </style> <article class="left-right-center"> <div class="left"></div> <div class="right"></div> <div class="center">我是中間的自適應元素--浮動</div> </article> </section>
方案二(絕對定位)
<section class="layout absolute"> <style> .layout.absolute .left-center-right div{ position: absolute; height: 100px; } .layout.absolute .left{ left: 0; width: 300px; background-color: red; } .layout.absolute .center{ left: 300px; right: 300px; background-color: yellow; } .layout.absolute .right{ right: 0; width: 300px; background-color: blue; } </style> <article class="left-center-right"> <div class="left"></div> <div class="center"> 我是中間的自適應元素--絕對定位 </div> <div class="right"></div> </article> </section>
方案三(flex布局)
<section class="layout flex"> <style> .layout.flex .left-center-right{ display: flex; height: 100px; } .layout.flex .left{ width: 300px; background-color: red; } .layout.flex .center{ flex: 1; background-color: yellow; } .layout.flex .right{ width: 300px; background-color: blue; } </style> <article class="left-center-right"> <div class="left"></div> <div class="center"> 我是中間的自適應元素--flex布局 </div> <div class="right"></div> </article> </section>
方案四(table布局)
<section class="layout table"> <style> .layout.table .left-center-right{ display: table; height: 100px; width: 100%; } .layout.table .left{ display: table-cell; width: 300px; background-color: red; } .layout.table .center{ display: table-cell; background-color: yellow; } .layout.table .right{ display: table-cell; width: 300px; background-color: blue; } </style> <article class="left-center-right"> <div class="left"></div> <div class="center"> 我是中間的自適應元素--table </div> <div class="right"></div> </article> </section>
方案五(網格布局)
<section class="layout grid"> <style> .layout.grid .left-center-right{ display: grid; width: 100%; grid-template-rows: 100px;/*每一格都是100px高*/ grid-template-columns: 300px auto 300px;/*左右兩格都是300px,中間是自適應*/ } .layout.grid .left{ background-color: red; } .layout.grid .center{ background-color: yellow; } .layout.grid .right{ background-color: blue; } </style> <article class="left-center-right"> <div class="left"></div> <div class="center"> 我是中間的自適應元素--grid布局 </div> <div class="right"></div> </article> </section>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答