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

1、寬度自適應(yīng)兩列布局
兩列布局可以使用浮動(dòng)來(lái)完成,左列設(shè)置左浮動(dòng),右列設(shè)置右浮動(dòng),這樣就省的再設(shè)置外邊距了。
當(dāng)元素使用了浮動(dòng)之后,會(huì)對(duì)周?chē)脑卦斐捎绊懀敲淳托枰宄?dòng),通常使用兩種方法。可以給受到影響的元素設(shè)置 clear:both,即清除元素兩側(cè)的浮動(dòng),也可以設(shè)置具體清除哪一側(cè)的浮動(dòng):clear:left 或 clear:right,但必須清楚的知道到底是哪一側(cè)需要清除浮動(dòng)的影響。也可以給浮動(dòng)的父容器設(shè)置寬度,或者為 100%,同時(shí)設(shè)置 overflow:hidden,溢出隱藏也可以達(dá)到清除浮動(dòng)的效果。
同理,兩列寬度自適應(yīng),只需要將寬度按照百分比來(lái)設(shè)置,這樣當(dāng)瀏覽器窗口調(diào)整時(shí),內(nèi)容會(huì)根據(jù)窗口的大小,按照百分比來(lái)自動(dòng)調(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">頁(yè)頭</div>
<div class="main-left">左列</div>
<div class="main-right">右列</div>
<div id="footer">頁(yè)腳</div>
</body>
</html>2、固定寬度兩列布局
寬度自適應(yīng)兩列布局在網(wǎng)站中一般很少使用,最常使用的是固定寬度的兩列布局。
要實(shí)現(xiàn)固定寬度的兩列布局,也很簡(jiǎn)單,只需要把左右兩列包裹起來(lái),也就是給他們?cè)黾右粋€(gè)父容器,然后固定父容器的寬度,父容器的寬度固定了,那么這兩列就可以設(shè)置具體的像素寬度了,這樣就實(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">頁(yè)頭</div>
<div id="main">
<div class="main-left">左列</div>
<div class="main-right">右列</div>
</div>
<div id="footer">頁(yè)腳</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">頁(yè)頭</div>
<div id="main">
<div class="main-left">左列</div>
<div class="main-right">右列</div>
</div>
<div id="footer">頁(yè)腳</div>
</body>
</html>4、固定寬度橫向兩列布局
和單列布局相同,可以把所有塊包含在一個(gè)容器中,這樣做方便設(shè)置,但增加了無(wú)意義的代碼,固定寬度就是給定父容器的寬度,然后中間主體使用浮動(dòng)。
<!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">頁(yè)頭</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">頁(yè)腳</div>
</div>
</body>
</html>感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“DIV+CSS如何實(shí)現(xiàn)兩列布局”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
當(dāng)前名稱(chēng):DIV+CSS如何實(shí)現(xiàn)兩列布局-創(chuàng)新互聯(lián)
文章起源:http://chinadenli.net/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)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容