前端面試系列:頁(yè)面布局之三欄布局

假設(shè)高度已知,請(qǐng)寫(xiě)出三欄布局,其中左欄、右欄寬度各為300px;中間自適應(yīng)。
  • 浮動(dòng)
  <section class="layout float">
    <style media="screen">
      * {
        margin: 0;
        padding: 0;
      }
      .left-center-right div {
        min-height: 100px;
      }
      .left {
        float: left;
        background: red;
        width: 300px;
      }
      .right {
        float: right;
        background: blue;
        width: 300px;
      }
      .center {
        background: yellow;
      }
    </style>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="right"></div>
      <div class="center">
        <h2>浮動(dòng)布局</h2>
        1、浮動(dòng)布局左右固定寬度,中間自適應(yīng)
        2、浮動(dòng)布局左右固定寬度,中間自適應(yīng)
      </div>
    </article>
  </section>
  • 絕對(duì)定位
  <section class="layout position">
    <style media="screen">
      html * {
        margin: 0;
        padding: 0;
      }
      .left-center-right div {
        position: absolute;
        min-height: 100px;
      }
      .left {
        left: 0;
        width: 300px;
        background: red;
      }
      .right {
        right: 0;
        width: 300px;
        background: blue;
      }
     .center {
        left: 300px;
        right: 300px;
        background: yellow;
      }
    </style>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="center">
        <h2>絕對(duì)定位</h2>
        1、定位布局左右固定寬度,中間自適應(yīng)
        2、定位布局左右固定寬度,中間自適應(yīng)
      </div>
      <div class="right"></div>
    </article>
  </section>
  • flex布局
<section class="layout flex">
    <style>
      html * {
        margin: 0;
        padding: 0;
      }
      .left-center-right div{
        min-height: 100px;
      }
      .left-center-right {
        display: flex;
      }
      .left {
        width: 300px;
        background: red;
        order: -1;
        /* order屬性定義項(xiàng)目的排列順序 數(shù)值越小 排列越靠前 默認(rèn)為0 */     
      }
      .center {
        flex: 1;
        background: yellow;
      }
      .right {
        width: 300px;
        background: blue;
      }
    </style>
    <article class="left-center-right">
      <div class="center">
        <h2>flex布局</h2>
        1、flex布局左右固定寬度,中間自適應(yīng)
        2、flex布局左右固定寬度,中間自適應(yīng)
      </div>
      <div class="left"></div>
      <div class="right"></div>
    </article>
  </section>
  • 表格布局
  <section class="layout table">
    <style>
      html * {
        margin: 0;
        padding: 0;
      }
      .left-center-right {
        display: table;
        width: 100%;
        height: 100px;
      }
      .left-center-right div {
        display: table-cell;
        min-height: 100px;
      }
      .left {
        background: red;
        width: 300px;
      }
      .center {
        background: yellow;
      }
      .right {
        background: blue;
        width: 300px;
      }
    </style>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="center">
        <h2>tabale布局</h2>
        1、table布局左右固定寬度,中間自適應(yīng)
        2、table布局左右固定寬度,中間自適應(yīng)
      </div>
      <div class="right"></div>
    </article>
  </section>
  • 網(wǎng)格布局
 <section class="layout grid">
    <style media="screen">
      html * {
        margin: 0;
        padding: 0;
      }
      .left-center-right {
        display: grid;
        width: 100%;
        grid-template-rows: 100px; 
        grid-template-columns: 300px auto 300px;
      }
      .left-center-right div {
        min-height: 100px;
      }
      .left {
        background: red;
      }
      .center {
        background: yellow;
      }
      .right {
        background: blue;
      }
    </style>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="center">
        1、網(wǎng)格布局左右固定寬度,中間自適應(yīng)
        2、網(wǎng)格布局左右固定寬度,中間自適應(yīng)
      </div>
      <div class="right"></div>
    </article>
  </section>
總結(jié):

1、這五種解決方案各自的優(yōu)缺點(diǎn)

  • 浮動(dòng):缺點(diǎn)是脫離文檔流的,需要做清浮動(dòng)處理;優(yōu)點(diǎn)是兼容性很好。
  • 絕對(duì)定位:缺點(diǎn)是布局脫離文檔流,子元素也必須脫離文檔流,可使用性比較差。優(yōu)點(diǎn)是快捷,不容易出問(wèn)題。
  • flex布局:為解決以上兩種布局方式的不足而出現(xiàn)的,比較完美,目前移動(dòng)端基本都屬于flex布局。
  • 表格布局:缺點(diǎn)是操作繁瑣,對(duì)seo也不友好,當(dāng)其中一個(gè)單元格的高度變大時(shí),其他單元格的高度也會(huì)隨之變大。優(yōu)點(diǎn)是兼容性非常好。
  • 網(wǎng)格布局:缺點(diǎn)是新出的技術(shù),低版本瀏覽器兼容性不是很好,優(yōu)點(diǎn)是可以做許多復(fù)雜的事情,但是代碼量要簡(jiǎn)化的多。

2、當(dāng)高度不定時(shí),兩側(cè)的高度也跟這中間的高度撐開(kāi),以上五種有哪幾種可以繼續(xù)用,哪幾種不能用了?
通過(guò)看效果:浮動(dòng)和絕對(duì)定位是不能用的,flex布局、表格布局和網(wǎng)格布局可以繼續(xù)使用。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 《曹全碑》譯文 [原文]:君諱全,字景完,敦煌效穀人也。其先蓋周之冑,武王秉乾之機(jī),翦伐殷商,既定爾勳,福祿攸同,...
    許昌小語(yǔ)閱讀 29,906評(píng)論 6 17
  • 了解一個(gè)人很難,了解自己也并不容易。更何況,心中常常大霧彌漫。 你以為你無(wú)可救藥地愛(ài)上了一個(gè)人,...
    山花爛漫sxy閱讀 326評(píng)論 0 2
  • Mysql 支持3中鎖結(jié)構(gòu) 表級(jí)鎖,開(kāi)銷(xiāo)小,加鎖快,不會(huì)出現(xiàn)死鎖,鎖定的粒度大,沖突概率高,并發(fā)度最低 行級(jí)鎖,開(kāi)...
    Tim在路上閱讀 657評(píng)論 1 1

友情鏈接更多精彩內(nèi)容