DIV+CSS如何實現(xiàn)兩列布局-創(chuàng)新互聯(lián)

這篇文章主要介紹了DIV+CSS如何實現(xiàn)兩列布局,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)公司作為成都網(wǎng)站建設(shè)公司,專注網(wǎng)站建設(shè)、網(wǎng)站設(shè)計,有關(guān)成都企業(yè)網(wǎng)站建設(shè)方案、改版、費用等問題,行業(yè)涉及人造霧等多個領(lǐng)域,已為上千家企業(yè)服務(wù),得到了客戶的尊重與認可。

1、寬度自適應(yīng)兩列布局

兩列布局可以使用浮動來完成,左列設(shè)置左浮動,右列設(shè)置右浮動,這樣就省的再設(shè)置外邊距了。

當(dāng)元素使用了浮動之后,會對周圍的元素造成影響,那么就需要清除浮動,通常使用兩種方法??梢越o受到影響的元素設(shè)置 clear:both,即清除元素兩側(cè)的浮動,也可以設(shè)置具體清除哪一側(cè)的浮動:clear:left 或 clear:right,但必須清楚的知道到底是哪一側(cè)需要清除浮動的影響。也可以給浮動的父容器設(shè)置寬度,或者為 100%,同時設(shè)置 overflow:hidden,溢出隱藏也可以達到清除浮動的效果。

同理,兩列寬度自適應(yīng),只需要將寬度按照百分比來設(shè)置,這樣當(dāng)瀏覽器窗口調(diào)整時,內(nèi)容會根據(jù)窗口的大小,按照百分比來自動調(diào)節(jié)內(nèi)容的大小。

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="UTF-8">  
    <title>寬度自適應(yīng)兩列布局</title>  
<style>  
*{margin:0;padding:0;}   
#herder{   
    height:50px;   
    background:blue;   
}   
.main-left{   
    width:30%;   
    height:800px;   
    background:red;   
    float:left;   
}   
.main-right{   
    width:70%;   
    height:800px;   
    background:pink;   
    float:right;   
}   
#footer{   
    clear:both;   
    height:50px;   
    background:gray;   
}   
</style>  
</head>  
<body>  
<div id="herder">頁頭</div>  
<div class="main-left">左列</div>  
<div class="main-right">右列</div>  
<div id="footer">頁腳</div>  
</body>  
</html>

2、固定寬度兩列布局

寬度自適應(yīng)兩列布局在網(wǎng)站中一般很少使用,最常使用的是固定寬度的兩列布局。

要實現(xiàn)固定寬度的兩列布局,也很簡單,只需要把左右兩列包裹起來,也就是給他們增加一個父容器,然后固定父容器的寬度,父容器的寬度固定了,那么這兩列就可以設(shè)置具體的像素寬度了,這樣就實現(xiàn)了固定寬度的兩列布局。


<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="UTF-8">  
    <title>固定寬度兩列布局</title>  
<style>  
*{margin:0;padding:0;}   
#herder{   
    height:50px;   
    background:blue;   
}   
#main{   
    width:960px;   
    margin:0 auto;   
    overflow:hidden;   
}   
#main .main-left{   
    width:288px;   
    height:800px;   
    background:red;   
    float:left;   
}   
#main .main-right{   
    width:672px;   
    height:800px;   
    background:pink;   
    float:right;   
}   
#footer{   
    width:960px;   
    height:50px;   
    background:gray;   
    margin:0 auto;   
}   
</style>  
</head>  
<body>  
<div id="herder">頁頭</div>  
<div id="main">  
    <div class="main-left">左列</div>  
    <div class="main-right">右列</div>  
</div>  
<div id="footer">頁腳</div>  
</body>  
</html>

3、兩列居中自適應(yīng)布局

同理,只需要給定父容器的寬度,然后讓父容器水平居中。

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="UTF-8">  
    <title>兩列居中自適應(yīng)布局</title>  
<style>  
*{margin:0;padding:0;}   
#herder{   
    height:50px;   
    background:blue;   
}   
#main{   
    width:80%;   
    margin:0 auto;   
    overflow:hidden;   
}   
#main .main-left{   
    width:20%;   
    height:800px;   
    background:red;   
    float:left;   
}   
#main .main-right{   
    width:80%;   
    height:800px;   
    background:pink;   
    float:right;   
}   
#footer{   
    width:80%;   
    height:50px;   
    background:gray;   
    margin:0 auto;   
}   
</style>  
</head>  
<body>  
<div id="herder">頁頭</div>  
<div id="main">  
    <div class="main-left">左列</div>  
    <div class="main-right">右列</div>  
</div>  
<div id="footer">頁腳</div>  
</body>  
</html>

4、固定寬度橫向兩列布局

和單列布局相同,可以把所有塊包含在一個容器中,這樣做方便設(shè)置,但增加了無意義的代碼,固定寬度就是給定父容器的寬度,然后中間主體使用浮動。

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="UTF-8">  
    <title>橫向兩列布局</title>  
<style>  
*{margin:0;padding:0;}   
#warp{   
    width:960px;   
    margin:0 auto;   
    margin-top:10px;   
}   
#herder{   
    height:50px;   
    background:blue;   
}   
#nav{   
    height:30px;   
    background:orange;   
    margin:10px 0;   
}   
#main{   
    width:100%;   
    margin-bottom:10px;   
    overflow:hidden;   
}   
#main .main-left{   
    width:640px;   
    height:200px;   
    background:yellow;   
    float:left;   
}   
#main .main-right{   
    width:300px;   
    height:200px;   
    background:pink;   
    float:right;   
}   
#content{   
    width:100%;   
    overflow:hidden;   
}   
#content .content-left{   
    width:640px;   
    height:800px;   
    background:lightgreen;   
    float:left;   
}   
#content .content-right-sup{   
    width:300px;   
    height:500px;   
    background:lightblue;   
    float:right;   
}   
#content .content-right-sub{   
    width:300px;   
    height:240px;   
    background:purple;   
    margin-top:20px;   
    float:right;   
}   
#footer{   
    height:50px;   
    background:gray;   
    margin-top:10px;   
}   
</style>  
</head>  
<body>  
<div id="warp">  
    <div id="herder">頁頭</div>  
    <div id="nav">導(dǎo)航</div>  
    <div id="main">  
        <div class="main-left">左-上</div>  
        <div class="main-right">右-上</div>  
    </div>  
    <div id="content">  
        <div class="content-left">左-下</div>  
        <div class="content-right-sup">右-上</div>  
        <div class="content-right-sub">右-下</div>  
    </div>  
    <div id="footer">頁腳</div>  
</div>  
</body>  
</html>

感謝你能夠認真閱讀完這篇文章,希望小編分享的“DIV+CSS如何實現(xiàn)兩列布局”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

當(dāng)前名稱:DIV+CSS如何實現(xiàn)兩列布局-創(chuàng)新互聯(lián)
文章起源:http://muchs.cn/article46/egihg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、搜索引擎優(yōu)化、網(wǎng)站建設(shè)做網(wǎng)站、微信小程序、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名