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,796評論 1 92
  • 簡介 CSS Grid布局 (又名"網格"),是一個基于二維網格布局的系統,旨在改變我們基于網格設計的用戶界面方式...
    咕咚咚bells閱讀 2,553評論 0 4
  • 前言 溫馨提示:本文較長,圖片較多,本來是想寫一篇 CSS 布局方式的,但是奈何 CSS 布局方式種類太多并且實現...
    sunshine小小倩閱讀 3,173評論 0 59
  • 人生中最大的趣味,莫過于讀過了萬卷書之后,從容不迫地行萬里路,更加深刻地閱人無數。 ----題記 每一...
    Stephanie_yi閱讀 175評論 0 1
  • 心是一匹馬,疾馳北上。 用這樣的忙碌來抵抗流年。抵抗流年里的種種不如意。和愛而不得的傷感。 “流年”是個寬容雍厚的...
    銘玥詠全閱讀 245評論 0 1