因为专注,所以专业
2005 - 2020

css实现三栏布局的几种方法及优缺点

时间:2020-02-10 01:35:16    作者:八叔模板堂
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layout</title>
    <style media="screen">
        html * {
            padding: 0;
            margin: 0;
        }
        .layout article div {
            min-height: 150px;
        }
    </style>
</head>
<body>
    <!--浮动布局  -->
    <section class="layout float">
        <style media="screen">
            .layout.float .left {
                float: left;
                width: 300px;
                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">
                <h2>浮动解决方案</h2>
                1.这是三栏布局的浮动解决方案; 2.这是三栏布局的浮动解决方案; 3.这是三栏布局的浮动解决方案; 4.这是三栏布局的浮动解决方案; 5.这是三栏布局的浮动解决方案; 6.这是三栏布局的浮动解决方案;
            </div>
        </article>
    </section>
</body>
</html>
<!--绝对布局  -->
 <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 {
             right: 300px;
             left: 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">
             <h2>绝对定位解决方案</h2>
             1.这是三栏布局的浮动解决方案; 2.这是三栏布局的浮动解决方案; 3.这是三栏布局的浮动解决方案; 4.这是三栏布局的浮动解决方案; 5.这是三栏布局的浮动解决方案; 6.这是三栏布局的浮动解决方案;
         </div>
         <div class="right"></div>
     </article>
 </section>
<!--flexbox布局-->
<section class="layout flexbox">
    <style>
        .layout.flexbox .left-center-right{
            display: flex;
        }
        .layout.flexbox .left {
            width: 300px;
            background: red;
        }
        .layout.flexbox .center {
            background: yellow;
            flex: 1;
        }
        .layout.flexbox .right {
            width: 300px;
            background: blue;
        }
    </style>
    <h1>三栏布局</h1>
    <article class="left-center-right">
        <div class="left"></div>
        <div class="center">
            <h2>flexbox解决方案</h2>
            1.这是三栏布局的浮动解决方案; 2.这是三栏布局的浮动解决方案; 3.这是三栏布局的浮动解决方案; 4.这是三栏布局的浮动解决方案; 5.这是三栏布局的浮动解决方案; 6.这是三栏布局的浮动解决方案;
        </div>
        <div class="right"></div>
    </article>
</section>
<!--表格布局-->
    <section class="layout table">
        <style>
            .layout.table .left-center-right {
                display: table;
                height: 150px;
                width: 100%;
            }
            .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>
        <h1>三栏布局</h1>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h2>表格布局解决方案</h2>
                1.这是三栏布局的浮动解决方案; 2.这是三栏布局的浮动解决方案; 3.这是三栏布局的浮动解决方案; 4.这是三栏布局的浮动解决方案; 5.这是三栏布局的浮动解决方案; 6.这是三栏布局的浮动解决方案;
            </div>
            <div class="right"></div>
        </article>
    </section>
<!--网格布局-->
<section class="layout grid">
    <style>
        .layout.grid .left-center-right {
            display: grid;
            width: 100%;
            grid-template-columns: 300px auto 300px;
            grid-template-rows: 150px;//行高
        }
        .layout.grid .left {
            background: red;
        }
        .layout.grid .center {
            background: yellow;
        }
        .layout.grid .right {
            background: blue;
        }
    </style>
    <h1>三栏布局</h1>
    <article class="left-center-right">
        <div class="left"></div>
        <div class="center">
            <h2>网格布局解决方案</h2>
            1.这是三栏布局的浮动解决方案; 2.这是三栏布局的浮动解决方案; 3.这是三栏布局的浮动解决方案; 4.这是三栏布局的浮动解决方案; 5.这是三栏布局的浮动解决方案; 6.这是三栏布局的浮动解决方案;
        </div>
        <div class="right"></div>
    </article>
</section>
赞(0
免费拨打  199-7086-1797
免费拨打  199-7086-1797免费拨打 199-7086-1797
微信二维码
手机二维码
返回顶部
返回顶部返回顶部