css常用布局多行多列
發(fā)布時(shí)間:2008-12-03 18:01:28 作者:佚名
我要評(píng)論

CSS布局常用的方法:float : none | left | right
取值:
none : 默認(rèn)值。對(duì)象不飄浮
left : 文本流向?qū)ο蟮挠疫?
right : 文本流向?qū)ο蟮淖筮?
它是怎樣工作的,看個(gè)一行兩列的例子
xhtml:
CSS布局常用的方法:float : none | left | right
取值:
none : 默認(rèn)值。對(duì)象不飄浮
left : 文本流向?qū)ο蟮挠疫?
right : 文本流向?qū)ο蟮淖筮?
它是怎樣工作的,看個(gè)一行兩列的例子
xhtml:
<div id="warp">
<div id="column1">這里是第一列</div>
<div id="column2">這里是第二列</div>
<div class="clear"></div>
</div>
CSS:
#wrap{ width:100%; height:auto;}
#column1{ float:left; width:40%;}
#column2{ float:right; width:60%;}
.clear{ clear:both;}
position : static | absolute | fixed | relative
取值:
static : 默認(rèn)值。無(wú)特殊定位,對(duì)象遵循HTML定位規(guī)則
absolute : 將對(duì)象從文檔流中拖出,使用 left , right , top , bottom 等屬性相對(duì)于其最接近的一個(gè)最有定位設(shè)置的父對(duì)象進(jìn)行絕對(duì)定位。如果不存在這樣的父對(duì)象,則依據(jù) body 對(duì)象。而其層疊通過 z-index 屬性定義
fixed : 未支持。對(duì)象定位遵從絕對(duì)(absolute)方式。但是要遵守一些規(guī)范
relative : 對(duì)象不可層疊,但將依據(jù) left , right , top , bottom 等屬性在正常文檔流中偏移位置
它來實(shí)現(xiàn)一行兩列的例子
xhtml:
<div id="warp">
<div id="column1">這里是第一列</div>
<div id="column2">這里是第二列</div>
</div>
CSS:
#wrap{ position:relative;/*相對(duì)定位*/width:770px;}
#column1{ position:absolute; top:0; left:0; width:300px;}
#column2{position:absolute; top:0; right:0; width:470px;}
他們的區(qū)別在哪?
顯然,float是相對(duì)定位的,會(huì)隨著瀏覽器的大小和分辨率的變化而改變,而position就不行了,所以一般情況下還是float布局!
CSS常用布局實(shí)例
一列
單行一列
body { margin: 0px; padding: 0px; text-align: center; }
#content { margin-left:auto; margin-right:auto; width: 400px; width: 370px; }
兩行一列
body { margin: 0px; padding: 0px; text-align: center;}
#content-top { margin-left:auto; margin-right:auto; width: 400px; width: 370px;}
#content-end {margin-left:auto; margin-right:auto; width: 400px; width: 370px;}
三行一列
body { margin: 0px; padding: 0px; text-align: center; }
#content-top { margin-left:auto; margin-right:auto; width: 400px; width: 370px; }
#content-mid { margin-left:auto; margin-right:auto; width: 400px; width: 370px; }
#content-end { margin-left:auto; margin-right:auto; width: 400px; width: 370px; }
兩列
單行兩列
#bodycenter { width: 700px;margin-right: auto; margin-left: auto;overflow: auto; }
#bodycenter #dv1 {float: left;width: 280px;}
#bodycenter #dv2 {float: right;width: 410px;}
兩行兩列
#header{ width: 700px; margin-right: auto;margin-left: auto; overflow: auto;}
#bodycenter { width: 700px; margin-right: auto; margin-left: auto; overflow: auto; }
#bodycenter #dv1 { float: left; width: 280px;}
#bodycenter #dv2 { float: right;width: 410px;}
三行兩列
#header{ width: 700px;margin-right: auto; margin-left: auto; }
#bodycenter {width: 700px; margin-right: auto; margin-left: auto; }
#bodycenter #dv1 { float: left;width: 280px;}
#bodycenter #dv2 { float: right; width: 410px;}
#footer{ width: 700px; margin-right: auto; margin-left: auto; overflow: auto; }
三列
單行三列
絕對(duì)定位
#left { position: absolute; top: 0px; left: 0px; width: 120px; }
#middle {margin: 20px 190px 20px 190px; }
#right {position: absolute;top: 0px; right: 0px; width: 120px;}
float定位
xhtml:
<div id="warp">
<div id="column">
<div id="column1">這里是第一列</div>
<div id="column2">這里是第二列</div>
<div class="clear"></div>
</div>
<div id="column3">這里是第三列</div>
<div class="clear"></div>
</div>
CSS:
#wrap{ width:100%; height:auto;}
#column{ float:left; width:60%;}
#column1{ float:left; width:30%;}
#column2{ float:right; width:30%;}
#column3{ float:right; width:40%;}
.clear{ clear:both;}
float定位二
xhtml:
<div id="center" class="column">
<h1>This is the main content.</h1>
</div>
<div id="left" class="column">
<h2>This is the left sidebar.</h2>
</div>
<div id="right" class="column">
<h2>This is the right sidebar.</h2>
</div>
CSS:
body {margin: 0;padding-left: 200px;padding-right: 190px;min-width: 240px;}
.column {position: relative;float: left;}
#center {width: 100%;}
#left {width: 180px; right: 240px;margin-left: -100%;}
#right {width: 130px;margin-right: -100%;}
兩行三列
xhtml:
<div id="header">這里是頂行</div>
<div id="warp">
<div id="column">
<div id="column1">這里是第一列</div>
<div id="column2">這里是第二列</div>
<div class="clear"></div>
</div>
<div id="column3">這里是第三列</div>
<div class="clear"></div>
</div>
CSS:
#header{width:100%; height:auto;}
#wrap{ width:100%; height:auto;}
#column{ float:left; width:60%;}
#column1{ float:left; width:30%;}
#column2{ float:right; width:30%;}
#column3{ float:right; width:40%;}
.clear{ clear:both;}
三行三列
xhtml:
<div id="header">這里是頂行</div>
<div id="warp">
<div id="column">
<div id="column1">這里是第一列</div>
<div id="column2">這里是第二列</div>
<div class="clear"></div>
</div>
<div id="column3">這里是第三列</div>
<div class="clear"></div>
</div>
<div id="footer">這里是底部一行</div>
CSS:
#header{width:100%; height:auto;}
#wrap{ width:100%; height:auto;}
#column{ float:left; width:60%;}
#column1{ float:left; width:30%;}
#column2{ float:right; width:30%;}
#column3{ float:right; width:40%;}
.clear{ clear:both;}
#footer{width:100%; height:auto;}
PS:這里列出的是常用的例子,而非研究之用,對(duì)一每個(gè)盒子,我都沒有設(shè)置margin,padding,boeder等屬性,是因?yàn)槲覀€(gè)人覺得,含有寬度定位的時(shí)候,最好不好用到他們,除非必不得已,因?yàn)槿绻皇沁@樣的話,解決瀏覽器兼容問題,會(huì)讓你頭疼,而且產(chǎn)生一系列CSS代碼,我覺得這樣的效率和效果都不好!
CSS布局高級(jí)技巧
margin和padding總是有可能要用到,而產(chǎn)生的問題如何解決呢?由于瀏覽器解釋容器寬度的方法不同:
IE 6.0 Firefox Opera等是
真實(shí)寬度=width+padding+border+margin
IE5.X
真實(shí)寬度=width-padding-border-margin
很明顯,第一種下很完美的布局在第二種情況下后果是很凄慘的!
解決的方法是 hack
div.content {
width:400px; //這個(gè)是錯(cuò)誤的width,所有瀏覽器都讀到了
voice-family: "\"}\""; //IE5.X/win忽略了"\"}\""后的內(nèi)容
voice-family:inherit;
width:300px; //包括IE6/win在內(nèi)的部分瀏覽器讀到這句,新的數(shù)值(300px)覆蓋掉了舊的
}
html>body .content { //html>body是CSS2的寫法
width:300px; //支持CSS2該寫法的瀏覽器(非IE5)有幸讀到了這一句
}
div.content {
width:300px !important; //這個(gè)是正確的width,大部分支持!important標(biāo)記的瀏覽器使用這里的數(shù)值
width(空格)/**/:400px; //IE6/win不解析這句,所以IE6/win仍然認(rèn)為width的值是300px;而IE5.X/win讀到這句,新的數(shù)值(400px)覆蓋掉了舊的,因?yàn)?important標(biāo)記對(duì)他們不起作用
}
html>body .content { //html>body是CSS2的寫法
width:300px; //支持CSS2該寫法的瀏覽器有幸讀到了這一句
}
列等高技巧
n行n列布局,每列高度(事先并不能確定哪列的高度)的相同,是每個(gè)設(shè)計(jì)師追求的目標(biāo),做法有:背景圖填充、加JS腳本的
方法和容器溢出部分隱藏和列的負(fù)底邊界和正的內(nèi)補(bǔ)丁相結(jié)合的方法。
背景圖填充法:
xhtml:
<div id="wrap">
<div id="column1">這是第一列</div>
<div id="column1">這是第二列</div>
<div class="clear"></div>
</div>
css:
#wrap{ width:776px; background:url(bg.gif) repeat-y 300px;}
#column1{ float:left; width:300px;}
#column2{ float:right; width:476px;}
.clear{ clear:both;}
就是將一個(gè)npx寬的一張圖片在外部容器縱向重復(fù),定位到兩列交錯(cuò)的位置縱向重復(fù),在視覺上產(chǎn)生了兩列高度一樣的錯(cuò)覺
取值:
none : 默認(rèn)值。對(duì)象不飄浮
left : 文本流向?qū)ο蟮挠疫?
right : 文本流向?qū)ο蟮淖筮?
它是怎樣工作的,看個(gè)一行兩列的例子
xhtml:
<div id="warp">
<div id="column1">這里是第一列</div>
<div id="column2">這里是第二列</div>
<div class="clear"></div>
</div>
CSS:
#wrap{ width:100%; height:auto;}
#column1{ float:left; width:40%;}
#column2{ float:right; width:60%;}
.clear{ clear:both;}
position : static | absolute | fixed | relative
取值:
static : 默認(rèn)值。無(wú)特殊定位,對(duì)象遵循HTML定位規(guī)則
absolute : 將對(duì)象從文檔流中拖出,使用 left , right , top , bottom 等屬性相對(duì)于其最接近的一個(gè)最有定位設(shè)置的父對(duì)象進(jìn)行絕對(duì)定位。如果不存在這樣的父對(duì)象,則依據(jù) body 對(duì)象。而其層疊通過 z-index 屬性定義
fixed : 未支持。對(duì)象定位遵從絕對(duì)(absolute)方式。但是要遵守一些規(guī)范
relative : 對(duì)象不可層疊,但將依據(jù) left , right , top , bottom 等屬性在正常文檔流中偏移位置
它來實(shí)現(xiàn)一行兩列的例子
xhtml:
<div id="warp">
<div id="column1">這里是第一列</div>
<div id="column2">這里是第二列</div>
</div>
CSS:
#wrap{ position:relative;/*相對(duì)定位*/width:770px;}
#column1{ position:absolute; top:0; left:0; width:300px;}
#column2{position:absolute; top:0; right:0; width:470px;}
他們的區(qū)別在哪?
顯然,float是相對(duì)定位的,會(huì)隨著瀏覽器的大小和分辨率的變化而改變,而position就不行了,所以一般情況下還是float布局!
CSS常用布局實(shí)例
一列
單行一列
body { margin: 0px; padding: 0px; text-align: center; }
#content { margin-left:auto; margin-right:auto; width: 400px; width: 370px; }
兩行一列
body { margin: 0px; padding: 0px; text-align: center;}
#content-top { margin-left:auto; margin-right:auto; width: 400px; width: 370px;}
#content-end {margin-left:auto; margin-right:auto; width: 400px; width: 370px;}
三行一列
body { margin: 0px; padding: 0px; text-align: center; }
#content-top { margin-left:auto; margin-right:auto; width: 400px; width: 370px; }
#content-mid { margin-left:auto; margin-right:auto; width: 400px; width: 370px; }
#content-end { margin-left:auto; margin-right:auto; width: 400px; width: 370px; }
兩列
單行兩列
#bodycenter { width: 700px;margin-right: auto; margin-left: auto;overflow: auto; }
#bodycenter #dv1 {float: left;width: 280px;}
#bodycenter #dv2 {float: right;width: 410px;}
兩行兩列
#header{ width: 700px; margin-right: auto;margin-left: auto; overflow: auto;}
#bodycenter { width: 700px; margin-right: auto; margin-left: auto; overflow: auto; }
#bodycenter #dv1 { float: left; width: 280px;}
#bodycenter #dv2 { float: right;width: 410px;}
三行兩列
#header{ width: 700px;margin-right: auto; margin-left: auto; }
#bodycenter {width: 700px; margin-right: auto; margin-left: auto; }
#bodycenter #dv1 { float: left;width: 280px;}
#bodycenter #dv2 { float: right; width: 410px;}
#footer{ width: 700px; margin-right: auto; margin-left: auto; overflow: auto; }
三列
單行三列
絕對(duì)定位
#left { position: absolute; top: 0px; left: 0px; width: 120px; }
#middle {margin: 20px 190px 20px 190px; }
#right {position: absolute;top: 0px; right: 0px; width: 120px;}
float定位
xhtml:
<div id="warp">
<div id="column">
<div id="column1">這里是第一列</div>
<div id="column2">這里是第二列</div>
<div class="clear"></div>
</div>
<div id="column3">這里是第三列</div>
<div class="clear"></div>
</div>
CSS:
#wrap{ width:100%; height:auto;}
#column{ float:left; width:60%;}
#column1{ float:left; width:30%;}
#column2{ float:right; width:30%;}
#column3{ float:right; width:40%;}
.clear{ clear:both;}
float定位二
xhtml:
<div id="center" class="column">
<h1>This is the main content.</h1>
</div>
<div id="left" class="column">
<h2>This is the left sidebar.</h2>
</div>
<div id="right" class="column">
<h2>This is the right sidebar.</h2>
</div>
CSS:
body {margin: 0;padding-left: 200px;padding-right: 190px;min-width: 240px;}
.column {position: relative;float: left;}
#center {width: 100%;}
#left {width: 180px; right: 240px;margin-left: -100%;}
#right {width: 130px;margin-right: -100%;}
兩行三列
xhtml:
<div id="header">這里是頂行</div>
<div id="warp">
<div id="column">
<div id="column1">這里是第一列</div>
<div id="column2">這里是第二列</div>
<div class="clear"></div>
</div>
<div id="column3">這里是第三列</div>
<div class="clear"></div>
</div>
CSS:
#header{width:100%; height:auto;}
#wrap{ width:100%; height:auto;}
#column{ float:left; width:60%;}
#column1{ float:left; width:30%;}
#column2{ float:right; width:30%;}
#column3{ float:right; width:40%;}
.clear{ clear:both;}
三行三列
xhtml:
<div id="header">這里是頂行</div>
<div id="warp">
<div id="column">
<div id="column1">這里是第一列</div>
<div id="column2">這里是第二列</div>
<div class="clear"></div>
</div>
<div id="column3">這里是第三列</div>
<div class="clear"></div>
</div>
<div id="footer">這里是底部一行</div>
CSS:
#header{width:100%; height:auto;}
#wrap{ width:100%; height:auto;}
#column{ float:left; width:60%;}
#column1{ float:left; width:30%;}
#column2{ float:right; width:30%;}
#column3{ float:right; width:40%;}
.clear{ clear:both;}
#footer{width:100%; height:auto;}
PS:這里列出的是常用的例子,而非研究之用,對(duì)一每個(gè)盒子,我都沒有設(shè)置margin,padding,boeder等屬性,是因?yàn)槲覀€(gè)人覺得,含有寬度定位的時(shí)候,最好不好用到他們,除非必不得已,因?yàn)槿绻皇沁@樣的話,解決瀏覽器兼容問題,會(huì)讓你頭疼,而且產(chǎn)生一系列CSS代碼,我覺得這樣的效率和效果都不好!
CSS布局高級(jí)技巧
margin和padding總是有可能要用到,而產(chǎn)生的問題如何解決呢?由于瀏覽器解釋容器寬度的方法不同:
IE 6.0 Firefox Opera等是
真實(shí)寬度=width+padding+border+margin
IE5.X
真實(shí)寬度=width-padding-border-margin
很明顯,第一種下很完美的布局在第二種情況下后果是很凄慘的!
解決的方法是 hack
div.content {
width:400px; //這個(gè)是錯(cuò)誤的width,所有瀏覽器都讀到了
voice-family: "\"}\""; //IE5.X/win忽略了"\"}\""后的內(nèi)容
voice-family:inherit;
width:300px; //包括IE6/win在內(nèi)的部分瀏覽器讀到這句,新的數(shù)值(300px)覆蓋掉了舊的
}
html>body .content { //html>body是CSS2的寫法
width:300px; //支持CSS2該寫法的瀏覽器(非IE5)有幸讀到了這一句
}
div.content {
width:300px !important; //這個(gè)是正確的width,大部分支持!important標(biāo)記的瀏覽器使用這里的數(shù)值
width(空格)/**/:400px; //IE6/win不解析這句,所以IE6/win仍然認(rèn)為width的值是300px;而IE5.X/win讀到這句,新的數(shù)值(400px)覆蓋掉了舊的,因?yàn)?important標(biāo)記對(duì)他們不起作用
}
html>body .content { //html>body是CSS2的寫法
width:300px; //支持CSS2該寫法的瀏覽器有幸讀到了這一句
}
列等高技巧
n行n列布局,每列高度(事先并不能確定哪列的高度)的相同,是每個(gè)設(shè)計(jì)師追求的目標(biāo),做法有:背景圖填充、加JS腳本的
方法和容器溢出部分隱藏和列的負(fù)底邊界和正的內(nèi)補(bǔ)丁相結(jié)合的方法。
背景圖填充法:
xhtml:
<div id="wrap">
<div id="column1">這是第一列</div>
<div id="column1">這是第二列</div>
<div class="clear"></div>
</div>
css:
#wrap{ width:776px; background:url(bg.gif) repeat-y 300px;}
#column1{ float:left; width:300px;}
#column2{ float:right; width:476px;}
.clear{ clear:both;}
就是將一個(gè)npx寬的一張圖片在外部容器縱向重復(fù),定位到兩列交錯(cuò)的位置縱向重復(fù),在視覺上產(chǎn)生了兩列高度一樣的錯(cuò)覺
相關(guān)文章
- 本文主要介紹了css九宮格布局的五種方法,內(nèi)容包括grid布局、flex布局、table布局、float浮動(dòng)定位、inline-block+letter-spacing屬性這五種方法的實(shí)現(xiàn),感興趣的可以了解下2023-09-18
- 在Web開發(fā)中,經(jīng)常會(huì)遇到需要將元素水平和垂直居中的情況,今天,將為大家分享幾種CSS方法,讓你的元素輕松居中,讓頁(yè)面更美觀吸引人,感興趣的小伙伴可以自己動(dòng)手試一試2023-09-08
- 相信大家在面試的時(shí)候也會(huì)經(jīng)常碰到css實(shí)現(xiàn)元素居中的方法,下面我介紹6種方法給大家,歡迎大家評(píng)論區(qū)交流2023-09-07
flex布局中使用flex-wrap實(shí)現(xiàn)換行的項(xiàng)目實(shí)踐
最近需要做個(gè)換行的布局,本文主要介紹了flex布局中使用flex-wrap實(shí)現(xiàn)換行的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需2022-06-16CSS布局之浮動(dòng)(float)和定位(position)屬性的區(qū)別
今天看到有朋友留言問浮動(dòng)和定位有什么區(qū)別,如何使用?今天找了篇文章,講的比較通俗易懂,供大家參考2021-09-23- 這篇文章主要介紹了css實(shí)現(xiàn)元素居中的N種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
div水平布局兩邊對(duì)齊的三種實(shí)現(xiàn)方法
這篇文章主要介紹了div水平布局兩邊對(duì)齊的三種實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起2021-01-21waterfall瀑布流布局+動(dòng)態(tài)渲染的實(shí)現(xiàn)
這篇文章主要介紹了waterfall瀑布流布局+動(dòng)態(tài)渲染的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起2021-01-19頁(yè)面中有間隔的方格布局如何完美實(shí)現(xiàn)方法
這篇文章主要介紹了頁(yè)面中有間隔的方格布局如何完美實(shí)現(xiàn)方法。文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來2020-11-27css實(shí)現(xiàn)六種自適應(yīng)兩欄布局方式
這篇文章主要介紹了css實(shí)現(xiàn)六種自適應(yīng)兩欄布局方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)2020-10-28
最新評(píng)論
大家感興趣的內(nèi)容
- 1DIV+CSS通用樣式布局實(shí)例代碼
- 2bootstrap3.0教程之表單(form)使用詳解
- 3創(chuàng)造100% 自適應(yīng)css布局的行之有效的方法
- 4兩個(gè)div并排的實(shí)現(xiàn)代碼
- 5bootstrap3.0教程之柵格系統(tǒng)原理(布局)
- 6詳解解決flex布局的justify-content: space-between對(duì)
- 7css網(wǎng)頁(yè)布局中文字排版的屬性和用法
- 8css實(shí)現(xiàn)的漂亮的分頁(yè)效果代碼(橘黃色與藍(lán)色)
- 9div+css table布局實(shí)現(xiàn)代碼
- 10CSS實(shí)現(xiàn)頁(yè)面九宮格布局的簡(jiǎn)單示范
最近更新的內(nèi)容
- css九宮格布局的五種方法
- CSS實(shí)現(xiàn)元素水平垂直居中的幾種方法
- css實(shí)現(xiàn)元素居中的6種方法
- flex布局中使用flex-wrap實(shí)現(xiàn)換行的項(xiàng)目實(shí)踐
- CSS布局之浮動(dòng)(float)和定位(position)屬性的區(qū)別
- css實(shí)現(xiàn)元素居中的N種方法
- div水平布局兩邊對(duì)齊的三種實(shí)現(xiàn)方法
- waterfall瀑布流布局+動(dòng)態(tài)渲染的實(shí)現(xiàn)
- 頁(yè)面中有間隔的方格布局如何完美實(shí)現(xiàn)方法
- css實(shí)現(xiàn)六種自適應(yīng)兩欄布局方式