css佈局實例第一篇:5種方法實現三欄佈局高度一致,左右寬度必定,中間寬度自適應

css面試題:請用盡量多的方法實現css三欄佈局,高度一致,左右寬度必定,中間寬度自適應。
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>css三欄佈局</title>
    <style>
      html * {
        margin: 0;
        padding: 0;
      }

      section {
        height: 100px;
        margin-bottom: 20px;
      }

      section div {
        height: 100px;
      }

      .left {
        background-color: aqua;
      }

      .mid {
        background-color: burlywood;
      }

      .right {
        background-color: coral;
      }
    </style>
  </head>

  <body>
    <p>題目:假設高度已知,請寫出三欄佈局,其中左欄右欄各爲100,中間自適應。</p>
    <!-- 浮動佈局 -->
    <section class="float">
      <style>
        .float-left {
          float: left;
          width: 100px;
        }

        .float-right {
          float: right;
          width: 100px;
        }
      </style>
      <article>
        <div class="left float-left"></div>
        <div class="right float-right"></div>
        <div class="mid float-mid">我是用的浮動</div>
      </article>
    </section>
    <!-- flex佈局 -->
    <section class="flex">
      <style>
        .flex article {
          display: flex;
        }

        .flex-left {
          width: 100px;
        }

        .flex-right {
          width: 100px;
        }

        .flex-mid {
          flex: 1;
        }
      </style>
      <article>
        <div class="left flex-left"></div>
        <div class="mid flex-mid">我是用的flexbox</div>
        <div class="right flex-right"></div>
      </article>
    </section>
    <!-- 絕對定位 -->
    <section class="position">
      <style>
        .position article{
          position: relative;
        }

        .position-left {
          position: absolute;
          left: 0;
          width: 100px;
        }

        .position-right {
          position: absolute;
          right: 0;
          width: 100px;
        }

        .position-mid {
          position: absolute;
          left: 100px;
          right: 100px;
        }
      </style>
      <article>
        <div class="left position-left"></div>
        <div class="right position-right"></div>
        <div class="mid position-mid">
          我是用的絕對定位
        </div>
      </article>
    </section>
    <!-- table佈局 -->
    <section class="table">
      <style>
        .table article {
          display: table;
          width: 100%;
        }

        .table-left {
          display: table-cell;
          width: 100px;
        }

        .table-right {
          display: table-cell;
          width: 100px;
        }

        .table-mid {
          display: table-cell;
        }
      </style>
      <article>
        <div class="left table-left"></div>
        <div class="mid table-mid">我是用的表格佈局</div>
        <div class="right table-right"></div>
      </article>
    </section>
    <!-- 網格佈局 -->
    <section class="grid">
      <style>
        .grid article {
          display: grid;
          width: 100%;
          grid-template-rows: 100px;
          grid-template-columns: 100px auto 100px;
        }
      </style>
      <article>
        <div class="left grid-left"></div>
        <div class="mid grid-mid">我是用的網格佈局</div>
        <div class="right grid-right"></div>
      </article>
    </section>
    後文思考:
    <ul>
      <li>1.各自優缺點</li>
      <li>2.去掉固定高度後有什麼影響?</li>
      <li>3.各自兼容性</li>
    </ul>
  </body>
</html>

效果預覽:
截屏2020-05-31 下午11.13.19.pngcss

相關文章
相關標籤/搜索