面试必备 - 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>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容