CSS布局面試題

頁(yè)面布局

題目:假設(shè)高度已知,請(qǐng)寫出三欄布局,其中左欄、右欄寬度各為300px,中間自適應(yīng)

image.png

頁(yè)面布局的變通

兩欄布局

  1. 左邊定寬,右邊自適應(yīng)方案:
  /*方案1  float + margin */
  .left{
      width:120px;
      float:left;
  }
  .right{
      margin-left:120px;
  }
  /*方案2  float + calc*/
  .left{
      width:120px;
      float:left;
  }
  .right{
      width:calc(100% - 120px);
      float:left;
  }
  1. 左右兩邊定寬,中間自適應(yīng)
  .wrap {
      width: 100%;
      height: 200px;
  }
  /*這里涉及到一個(gè)知識(shí)點(diǎn), > 是子類選擇器
  下面是有關(guān)子類選擇器的教程
  (http://www.w3school.com.cn/css/css_selector_adjacent_sibling.asp)
  */
  .wrap > div {
      height: 100%;
  }
  /* 方案1 */
  .left {
      width: 120px;
      float: left;
  }
  .right {
    float: right;
    width: 120px;
  }
  .center {
    margin: 0 120px; 
  }
  /* 方案2 */
  .left{
     width:120px;
     float:left;
    }
  .right{
      width:120px;
      float:right;  
    }
  .center{
      width:calc(100% - 240px);
      margin-left:120px;
  }
  /*方案3*/
  .wrap{
      display:flex;
   }
  .left{
      width:120px;
  }
  .right{
      width:120px;
  }
  /*要注意的一點(diǎn)是flex 是 flex-grow、flex-sharink、flex-basis的縮寫
  具體詳細(xì)解釋( https://blog.csdn.net/aliven1/article/details/78853725 )
  */
  .center{
      flex:1;
  }
  1. 左右居中
  • 行內(nèi)元素:text-align:center
  • 定寬塊元素:左右 margin 值為 auto
  • 不定寬塊狀元素:table布局,position + transform
  /*方案1*/
   .wrap{
      text-align:center;
   }
   .center{
      display:inline;
    }
    /*or*/
    /* display:inline-block; */
    /*方案2*/
   .center{
      width:100px;
      margin: 0 auto;
   }  
    /*方案3*/
   .wrap{
      position:relative;
   }
   .center{
      position:absolute;
      left:50%;
      transform:translateX(-50%);  /*元素往左位移自身寬度50%的距離*/
   }
  1. 上下垂直居中:
  • 不定高:position + transform、flex、IFC + vertical-align:middle
  /*不定高方案1*/
  .center{
     display:flex;
     align-items:center;
  }
  /*不定高方案2*/
  .center{
      position:absolute;
      top:50%;
      transform:translateY(-50%);
   }
   /*不定高方案3*/
   /*設(shè)置inline-block 則會(huì)在外層產(chǎn)生 IFC,高度設(shè)為 100% 撐開 wrap 的高度 */

三欄布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html *{
            padding: 0;
            margin: 0;
        }
        .layout{
            margin-top: 20px;
        }
        .layout article div{
            min-height: 100px;
        }
    </style>
</head>
<body>
    <!-- 浮動(dòng)布局解決方案 -->
    <!-- 缺點(diǎn):需要清除浮動(dòng),即處理好與周圍元素的關(guān)系
        優(yōu)點(diǎn):有好的兼容性 -->
    <section class="layout float">
        <style media="screen">
            .layout.float .left{
                float: left;
                width: 300px;
                background: red;
            }
            .layout.float .right{
                float: right;
                width: 300px;
                background:blue;
            }
            .layout.float .center{
                background: yellow;
            }
        </style>
        <article class="left-right-center">
            <div class="left"></div>
            <div class="right"></div>
            <div class="center">
                <h1>浮動(dòng)解決方案</h1>
                1. 這是三欄布局中間部分
                2. 這是三欄布局中間部分
            </div>
        </article>
    </section>

    <!-- 絕對(duì)定位解決方案 -->
    <!-- 優(yōu)點(diǎn):實(shí)現(xiàn)簡(jiǎn)單,不容易出問題
    缺點(diǎn):布局脫離了文檔流了,那么所有的子元素也脫離的文檔流,可使用性比較差 -->
    <section class="layout absolute">
        <style>
            .layout.absolute .left-center-right>div{
                position: absolute;
            }
            .layout.absolute .left{
                left:0;
                width: 300px;
                background: red;
            }
            .layout.absolute .center{
                left:300px;
                right: 300px;
                background: yellow;
            }
            .layout.absolute .right{
                right: 0;
                width: 300px;
                background: blue;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h2>絕對(duì)定位解決方案</h2>
                1. 這是三欄布局絕對(duì)定位中間部分
                2. 這是三欄布局絕對(duì)定位中間部分
            </div>
            <div class="right"></div>
        </article>
    </section>

    <!-- flexbox解決方案 -->
    <!-- 優(yōu)點(diǎn):彌補(bǔ)了 float布局與絕對(duì)布局的缺點(diǎn),是現(xiàn)在比較流行的布局方式 -->
    <section class="layout flexbox">
        <style>
            .layout.flexbox{
                margin-top: 140px;
            }
            .layout.flexbox .left-center-right{
                display: flex;
            }
            .layout.flexbox .left{
                width: 300px;
                background: red;
            }
            .layout.flexbox .center{
                flex: 1;
                background: yellow;
            }
            .layout.flexbox .right{
                width: 300px;
                background: blue;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h2>flexbox解決方案</h2>
                1. 這是三欄布局flexbox中間部分
                2. 這是三欄布局flexbox中間部分
            </div>
            <div class="right"></div>
        </article>
    </section>

    <!-- 表格布局解決方案 -->
    <!-- 優(yōu)點(diǎn):有好的兼容性,flex布局不兼容時(shí)可以考慮 表格布局 -->
    <section class="layout table">
        <style>
            .layout.table .left-center-right{
                width: 100%;
                display: table;
                height: 100px;
            }
            .layout.table .left-center-right>div{
                display: table-cell;
            }
            .layout.table .left{
                width: 300px;
                background: red;
            }
            .layout.table .center{
                background: yellow;
            }
            .layout.table .right{
                width: 300px;
                background: blue;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h2>表格布局解決方案</h2>
                1. 這是三欄布局表格中間部分
                2. 這是三欄布局表格中間部分
            </div>
            <div class="right"></div>
        </article>
    </section>

    <!-- 網(wǎng)格布局的解決方案 -->
    <section class="layout grid">
        <style>
            .layout.grid .left-center-right{
                display: grid;
                width: 100%;
                grid-template-rows: 100px;
                grid-template-columns: 300px auto 300px;
            }
            .layout.grid .left{
                background: red;
            }
            .layout.grid .center{
                background: yellow;
            }
            .layout.grid .right{
                background: blue;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h2>網(wǎng)格布局解決方案</h2>
                1. 這是三欄布局網(wǎng)格中間部分
                2. 這是三欄布局網(wǎng)格中間部分
            </div>
            <div class="right"></div>
        </article>
    </section>
</body>
</html>

三欄布局(練習(xí))

  • 左又寬度固定,中間自適應(yīng)
  • 上下高度固定,中間自適應(yīng)
    兩欄布局
  • 左寬度固定,右自適應(yīng)
  • 右寬度固定,左自適應(yīng)
  • 上高度固定,下自適應(yīng)
  • 下高度固定,上自適應(yīng)
最后編輯于
?著作權(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)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 1、常用那幾種瀏覽器測(cè)試?有哪些內(nèi)核(Layout Engine)?- 瀏覽器:IE,Chrome,F(xiàn)ireFox...
    編碼夢(mèng)想家閱讀 1,759評(píng)論 0 1
  • 課程思維導(dǎo)圖 Q:三欄布局,高度已知,左右兩欄固定,中間自適應(yīng)的三欄布局有幾種實(shí)現(xiàn)方式,各自的優(yōu)缺點(diǎn)是什么? Q:...
    黃律AI法研室閱讀 737評(píng)論 0 0
  • 題目:假設(shè)高度已知,請(qǐng)寫出三欄布局,其中左欄、右欄寬度各為300px;中間自適應(yīng)。 我這邊總結(jié)了五種方法,如有不正...
    言歌不言歌閱讀 2,234評(píng)論 0 3
  • 一、垂直居中,多行文本垂直居中? 1、單行文本垂直居中設(shè)置高度height和行高line-height,使高度he...
    迷人的洋蔥蔥閱讀 698評(píng)論 0 0
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)。 注意:講述HT...
    kismetajun閱讀 28,809評(píng)論 1 45

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