CSS三栏布局5种解决方案

浮动布局

    <!-- 浮动布局 -->
    <section class="layout float">
        <style>
            .layout.float .left {
                width: 100px;
                float: left;
                background: red;
            }

            .layout.float .right {
                width: 100px;
                float: right;
                background: blue;
            }

            .layout.float .center {
                background: yellow;
            }
        </style>
        <article class="container">
            <div class="left">左边</div>
            <div class="right">右边</div>
            <div class="center">
                <h2>浮动布局</h2>
                浮动布局浮动布局 浮动布局浮动布局 
                浮动布局浮动布局 浮动布局浮动布局
                浮动布局浮动布局 浮动布局浮动布局 
            </div>
        </article>
    </section>

缺点: 需要清除浮动.
优点: 兼容性好
注: center 盒子必须放在最后

绝对定位布局


    <!-- 绝对定位布局 -->
    <section class="layout absolute">
        <style>
            .layout.absolute .left {
                position: absolute;
                left: 0;
                width: 100px;
                background: red;
            }

            .layout.absolute .right {
                width: 100px;
                right: 0;
                position: absolute;
                background: blue;
            }

            .layout.absolute .center {
                position: absolute;
                left: 100px;
                right: 100px;
                background: yellow;
            }
        </style>
        <article class="container">
            <div class="left">左边</div>
            <div class="center">
                    <h2>绝对定位布局</h2>
                    绝对定位布局 绝对定位布局 
                    绝对定位布局 绝对定位布局
                    绝对定位布局 绝对定位布局 
                    绝对定位布局 绝对定位布局
                </div>
            <div class="right">右边</div>
        </article>
    </section>

注: 这种办法会让整个布局脱离文档流.

flexbox布局

    <!-- flexbox布局 -->
    <section class="layout flexbox">
        <style>
            .layout.flexbox .container {
                display: flex;
            }

            .layout.flexbox .left {
                width: 100px;
                background: red;
            }

            .layout.flexbox .right {
                width: 100px;
                background: blue;
            }

            .layout.flexbox .center {
                flex: 1;
                background: yellow;
            }
        </style>
        <article class="container">
            <div class="left">左边</div>
            <div class="center">
                <h2>flexbox布局</h2>
                flexbox布局 flexbox布局 
                flexbox布局 flexbox布局 
                flexbox布局 flexbox布局 
                flexbox布局 flexbox布局
            </div>
            <div class="right">右边</div>
        </article>
    </section>

缺点: 兼容性问题
优点: 解决以上两种布局存在的问题
注:目前比较主流的移动端布局方式

表格布局

    <!-- 表格布局 -->
    <section class="layout table">
        <style>
            .layout.table .container {
                display: table;
                width: 100%;
                height: 100px;
            }

            .layout.table .container>div {
                display: table-cell;
            }

            .layout.table .left {
                width: 100px;
                background: red;
            }

            .layout.table .right {
                width: 100px;
                background: blue;
            }

            .layout.table .center {
                background: yellow;
            }
        </style>
        <article class="container">
            <div class="left">左边</div>
            <div class="center">
                <h2>表格布局</h2>
                表格布局表格布局表格布局
                表格布局表格布局表格布局
                表格布局表格布局表格布局
            </div>
            <div class="right">右边</div>
        </article>
    </section>

注:该方式在实际项目开发中已经淘汰.

网格布局

    <!-- 网格布局 -->
    <section class="layout grid">
        <style>
            .layout.grid .container {
                display: grid;
                width: 100%;
                /* 设置高度 */
                grid-template-rows: 100px;
                /* 设置行数 */
                grid-template-columns: 100px auto 100px;
            }

            .layout.grid .left {
                background: red;
            }

            .layout.grid .right {
                background: blue;
            }

            .layout.grid .center {
                background: yellow;
            }
        </style>
        <article class="container">
            <div class="left">左边</div>
            <div class="center">
                <h2>网格布局</h2>
                网格布局网格布局网格布局
                网格布局网格布局网格布局
                网格布局网格布局网格布局
                网格布局网格布局网格布局
            </div>
            <div class="right">右边</div>
        </article>
    </section>

注:黑科技

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,801评论 1 92
  • 简介 CSS Grid布局 (又名"网格"),是一个基于二维网格布局的系统,旨在改变我们基于网格设计的用户界面方式...
    咕咚咚bells阅读 2,553评论 0 4
  • 前言 温馨提示:本文较长,图片较多,本来是想写一篇 CSS 布局方式的,但是奈何 CSS 布局方式种类太多并且实现...
    sunshine小小倩阅读 3,173评论 0 59
  • 人生中最大的趣味,莫过于读过了万卷书之后,从容不迫地行万里路,更加深刻地阅人无数。 ----题记 每一...
    Stephanie_yi阅读 179评论 0 1
  • 心是一匹马,疾驰北上。 用这样的忙碌来抵抗流年。抵抗流年里的种种不如意。和爱而不得的伤感。 “流年”是个宽容雍厚的...
    铭玥咏全阅读 245评论 0 1