美文网首页
面试必备 - 5种方式实现三栏布局

面试必备 - 5种方式实现三栏布局

作者: 桃花谷主V | 来源:发表于2021-03-11 20:30 被阅读0次
<!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>

相关文章

网友评论

      本文标题:面试必备 - 5种方式实现三栏布局

      本文链接:https://www.haomeiwen.com/subject/sfvoqltx.html