CSS三欄布局5種解決方案

浮動(dòng)布局

    <!-- 浮動(dòng)布局 -->
    <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>浮動(dòng)布局</h2>
                浮動(dòng)布局浮動(dòng)布局 浮動(dòng)布局浮動(dòng)布局 
                浮動(dòng)布局浮動(dòng)布局 浮動(dòng)布局浮動(dòng)布局
                浮動(dòng)布局浮動(dòng)布局 浮動(dòng)布局浮動(dòng)布局 
            </div>
        </article>
    </section>

缺點(diǎn): 需要清除浮動(dòng).
優(yōu)點(diǎn): 兼容性好
注: center 盒子必須放在最后

絕對(duì)定位布局


    <!-- 絕對(duì)定位布局 -->
    <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>絕對(duì)定位布局</h2>
                    絕對(duì)定位布局 絕對(duì)定位布局 
                    絕對(duì)定位布局 絕對(duì)定位布局
                    絕對(duì)定位布局 絕對(duì)定位布局 
                    絕對(duì)定位布局 絕對(duì)定位布局
                </div>
            <div class="right">右邊</div>
        </article>
    </section>

注: 這種辦法會(huì)讓整個(gè)布局脫離文檔流.

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>

缺點(diǎn): 兼容性問題
優(yōu)點(diǎn): 解決以上兩種布局存在的問題
注:目前比較主流的移動(dòng)端布局方式

表格布局

    <!-- 表格布局 -->
    <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>

注:該方式在實(shí)際項(xiàng)目開發(fā)中已經(jīng)淘汰.

網(wǎng)格布局

    <!-- 網(wǎng)格布局 -->
    <section class="layout grid">
        <style>
            .layout.grid .container {
                display: grid;
                width: 100%;
                /* 設(shè)置高度 */
                grid-template-rows: 100px;
                /* 設(shè)置行數(shù) */
                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>網(wǎng)格布局</h2>
                網(wǎng)格布局網(wǎng)格布局網(wǎng)格布局
                網(wǎng)格布局網(wǎng)格布局網(wǎng)格布局
                網(wǎng)格布局網(wǎng)格布局網(wǎng)格布局
                網(wǎng)格布局網(wǎng)格布局網(wǎng)格布局
            </div>
            <div class="right">右邊</div>
        </article>
    </section>

注:黑科技

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,168評(píng)論 1 92
  • 簡(jiǎn)介 CSS Grid布局 (又名"網(wǎng)格"),是一個(gè)基于二維網(wǎng)格布局的系統(tǒng),旨在改變我們基于網(wǎng)格設(shè)計(jì)的用戶界面方式...
    咕咚咚bells閱讀 2,710評(píng)論 0 4
  • 前言 溫馨提示:本文較長(zhǎng),圖片較多,本來是想寫一篇 CSS 布局方式的,但是奈何 CSS 布局方式種類太多并且實(shí)現(xiàn)...
    sunshine小小倩閱讀 3,266評(píng)論 0 59
  • 人生中最大的趣味,莫過于讀過了萬(wàn)卷書之后,從容不迫地行萬(wàn)里路,更加深刻地閱人無(wú)數(shù)。 ----題記 每一...
    Stephanie_yi閱讀 235評(píng)論 0 1
  • 心是一匹馬,疾馳北上。 用這樣的忙碌來抵抗流年。抵抗流年里的種種不如意。和愛而不得的傷感。 “流年”是個(gè)寬容雍厚的...
    銘玥詠全閱讀 280評(píng)論 0 1

友情鏈接更多精彩內(nèi)容