溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在css中實現(xiàn)多列布局

發(fā)布時間:2022-04-28 15:49:44 來源:億速云 閱讀:151 作者:iii 欄目:大數(shù)據(jù)

這篇“如何在css中實現(xiàn)多列布局”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“如何在css中實現(xiàn)多列布局”文章吧。

一. 定寬 + 自適應(yīng)

期望效果: 左側(cè)寬度固定, 右側(cè)寬度自適應(yīng)
公共代碼:
html:

<div class="parent">
    <div class="left">
        <p>left menu</p>
    </div>
    <div class="right">
        <li>right item1</li>
        <li>right item2</li>
        <li>right item3</li>
    </div>
</div>

css:

html, body, p, ul, li {
    margin: 0;
    padding: 0;
}
div.left {
    background: #d2e3e3;
}
div.right {
    background: #77DBDB;
}

方案一: float

.left {
    float: left;
    width: 100px;
}
.right {
    margin-left: 100px; // 或 overflow: hidden
}

方案二: table

.parent {
    display: table;
    width: 100%;
    table-layout: fixed; // https://blog.csdn.net/qq_36699230/article/details/80658742
    .left, .right {
        display: table-cell;
    }
    .left {
        width: 100px;
    }
}

方案三: flex

.parent {
    display: flex;
    .left {
        width: 100px; // 或 flex: 0 0 100px;
    }
    .right {
        flex: 1;
    }
}
  • 兩(多)列定寬 + 自適應(yīng) 布局使用上述方案均可, 對于中間一列的設(shè)置與第一列保持一致即可

  • 不定寬(兩列或多列) + 自適應(yīng) 布局使用上述方案均可, 對于中間一列的設(shè)置與第一列保持一致即可, 不同的是不需要特別設(shè)置寬度, 需要特別注意的是使用table布局時的情況, 如下:

.parent {
    display: table;
    width: 100%;
    // 設(shè)置table-layout: fixed; 會使單元格等寬, 因此此處不設(shè)置
    .left, .right {
        display: table-cell;
    }
    .left {
        width: 0.1%; // 寬度設(shè)置一個極小值, 由于沒有設(shè)置table-layout: fixed; 所以寬度由內(nèi)容決定
        white-space:nowrap;
    }
}

二. 等寬(兩/多列)布局

公共代碼:
html

<div class="parent">
    <div class="column">
        <p>1</p>
    </div>
    <div class="column">
        <p>2</p>
    </div>
    <div class="column">
        <p>3</p>
    </div>
    <div class="column">
        <p>4</p>
    </div>
</div>

css

html, body, div, p {
    margin: 0;
    padding: 0;
}
.parent {
    width: 800px;
    border: 1px solid coral;
    .column {
        height: 30px;
        background: bisque;
        p {
            background: #f0b979;
            height: 30px;
        }
    }
}

方案一: float (個人并不喜歡, 寫法很死, 需要知道有多少列, 而且有邊框的情況下會超出容器)

.parent {
    margin-left: -20px;
    overflow: hidden;
    .column {
        float: left;
        width: 25%;
        padding-left: 20px;
        box-sizing: border-box;
    }
}

方案二: flex (推薦)

.parent {
    display: flex;
    .column {
        flex: 1;
        &+.column {
            margin-left: 10px;
        }
    }
}

三. 等高布局

推薦方案:

.parent {
    display: flex;
}
.left, .right {
    flex: 1;
}

以上就是關(guān)于“如何在css中實現(xiàn)多列布局”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

css
AI