溫馨提示×

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

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

有哪些css布局方法

發(fā)布時(shí)間:2021-06-01 15:44:46 來(lái)源:億速云 閱讀:161 作者:Leah 欄目:web開(kāi)發(fā)

本篇文章為大家展示了有哪些css布局方法,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

1.一列布局:

一般都是固定的寬高,設(shè)置margin : 0 auto來(lái)水平居中,用于界面顯著標(biāo)題的展示等;

        .main{
            width: 200px;
            height: 100px;
            background-color: grey;
            margin: 0 auto;
        }
<p class="main"></p>

2.兩列布局:

說(shuō)起兩列布局,最常見(jiàn)的就是使用float來(lái)實(shí)現(xiàn)。float浮動(dòng)布局的缺點(diǎn)是浮動(dòng)后會(huì)造成文本環(huán)繞等效果,以及需要及時(shí)清除浮動(dòng)。

設(shè)置左左浮動(dòng),或設(shè)置左右浮動(dòng)(這是需要確定父級(jí)元素的寬度)

如何父級(jí)元素沒(méi)有設(shè)置高度,則需要設(shè)置overflow:hidden來(lái)清除浮動(dòng)產(chǎn)生的影響

對(duì)于自己相鄰元素清除浮動(dòng)產(chǎn)生的影響用:clear:both;

有哪些css布局方法

    <p class="main">
        <p class="left">left</p>
        <p class="right">right</p>
    </p>
        .main{
            width: 400px;
            background: red;
            overflow: hidden;
        }

        .left{
            background: yellow;
            float: left;
        }

        .right{
            background: green;
            float: left;
        }

3.三列布局:

兩側(cè)定寬中間自適應(yīng)

首先設(shè)置父級(jí)元素的寬度,可以左左右設(shè)置浮動(dòng)。然后中間設(shè)置margin調(diào)整間距。 也可以都設(shè)置成左浮動(dòng),設(shè)置margin,調(diào)整間距。同樣注意清除浮動(dòng)的影響!

有哪些css布局方法

    <p class="main">
        <p class="left">left</p>
        <p class="middle">middle</p>
        <p class="right">right</p>
    </p>
        .main{
            width: 100%;
            background: red;
            overflow: hidden;
        }
        .left{
            background: yellow;
            float: left;
            width: 100px;
        }
        .middle{
             background: rosybrown;
             float: left;
             width: cacl(100%-200px);
         }
        .right{
            background: green;
            float: right;
            width: 100px%;
        }

或著為父級(jí)元素設(shè)置relative屬性,再為子元素設(shè)置absolute屬性,再分別定位,調(diào)間距。

有哪些css布局方法

<p class="parent" style="">
    <p class="left" style="">
        <p>left</p>
    </p>    
    <p class="center" style="">
        <p>center</p>
        <p>center</p>
    </p>                
    <p class="right"  style="">
        <p>right</p>
    </p>            
</p>
<style>
p{margin: 0;}
.parent{position: relative;height:40px;}
.left,.right,.center{position: absolute;}
.left{left: 0;width:100px;}
.right{right: 0;width: 100px;}
.center{left: 120px; right: 120px;}
</style>

4.混合布局:

在一列布局的基礎(chǔ)上,保留top和foot部分,將中間的main部分改造成兩列或三列布局,小的模塊可以再逐級(jí)同理劃分。

有哪些css布局方法

    <p class="top"></p>
    <p class="main">
        <p class="left">left</p>
        <p class="right">right</p>
    </p>
    <p class="footer"></p>
        .top{
            height: 100px;
            background: teal;
        }
        .footer{
            height: 100px;
            background: wheat;
        }
        .main{
            width: 100%;
            background: red;
            overflow: hidden;
        }
        .left{
            background: yellow;
            float: left;
            width: 50%;
        }
        .right{
            background: green;
            float: right;
            width: 50%;
        }

5.擴(kuò)展(如等分布局等)

有哪些css布局方法

    <p class="parent">
        <p class="child"></p>
        <p class="child"></p>
        <p class="child"></p>
        <p class="child"></p>
    </p>
        body{margin: 0;}
        .parent{
            border: 1px solid red;
            overflow: hidden;
            margin-right: -10px;
        }
        .child {
            width: calc(25% - 10px);
            height: 100px;
            background: green;
            float: left;
            margin-right: 10px;
        }

上述內(nèi)容就是有哪些css布局方法,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

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

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

css
AI