面試必備 - 5種方式實現三欄布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>三欄布局</title>
    <style media="screen">
        html *{
          padding: 0;
          margin: 0;
        }
        .layout {
            margin-bottom: 20px;
        }
        .layout article div{
          min-height: 100px;
        }
      </style>
</head>
<body>
    <!-- 需求: 實現一個三欄布局,左邊200px,右邊300px,中間自適應-->
    <!-- 方式一:浮動布局 -->
    <section class="layout float">
        <style media="screen">
            .layout.float .left{
                float:left;
                width:200px;
                background: red;
            }
            .layout.float .center{
                background: yellow;
            }
            .layout.float .right{
                float:right;
                width:300px;
                background: blue;
            }
        </style>
        <h1>三欄布局-浮動解決方案</h1>
        <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;
            }
            .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>
        <h1>三欄布局-絕對定位解決方案</h1>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center"></div>
            <div class="right"></div>
        </article>
    </section>

    <!-- 方式三: flex布局 -->
    <section class="layout flexbox">
        <style>
            .flexbox {
                margin-top: 110px;
            }
            .flexbox .left-center-right{
                display: flex;
            }
            .flexbox .left-center-right .left{
                width: 200px;
                background: red;
            }
            .flexbox .left-center-right .center {
                flex: 1;
                background: yellow;
            }
            .flexbox .left-center-right .right {
                width: 300px;
                background: blue;
            }
        </style>
        <h1>三欄布局-彈性布局解決方案</h1>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center"></div>
            <div class="right"></div>
        </article>
    </section>

    <!-- 方式四: table布局 -->
    <section class="layout table">
        <style>
            .table .left-center-right{
                width: 100%;
                height: 100px;
                display: table;
            }
            .table .left-center-right div{
                display: table-cell;
            }
            .table .left {
                width: 200px;
                background: red;
            }
            .table .center {
                background: yellow;
            }
            .table .right {
                width: 300px;
                background: blue;
            }
        </style>
        <h1>三欄布局-表格布局解決方案</h1>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center"></div>
            <div class="right"></div>
        </article>
    </section>

    <!-- 方式五:網格布局 -->
    <section class="layout grid">
        <style>
            .grid .left-center-right{
                width: 100%;
                display: grid;
                grid-template-rows: 100px;
                grid-template-columns: 200px auto 300px;
            }
            .grid .left {
                background:red;
            }       
            .grid .center {
                background: yellow;
            }
            .grid .right {

                background: blue;
            }
        </style>
        <h1>三欄布局-網格布局解決方案</h1>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center"></div>
            <div class="right"></div>
        </article>
    </section>
</body>
</html>
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容