天天看點

html5多行布局,css常用布局多行多列This is the main content.

CSS布局常用的方法:float : none | left | right

取值:

none :  預設值。對象不飄浮

left :  文本流向對象的右邊

right :  文本流向對象的左邊

它是怎樣工作的,看個一行兩列的例子

xhtml:

這裡是第一列 這裡是第二列

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 :  預設值。無特殊定位,對象遵循HTML定位規則

absolute :  将對象從文檔流中拖出,使用 left , right , top , bottom 等屬性相對于其最接近的一個最有定位設定的父對象進行絕對定位。如果不存在這樣的父對象,則依據 body 對象。而其層疊通過 z-index 屬性定義

fixed :  未支援。對象定位遵從絕對(absolute)方式。但是要遵守一些規範

relative :  對象不可層疊,但将依據 left , right , top , bottom 等屬性在正常文檔流中偏移位置

它來實作一行兩列的例子

xhtml:

這裡是第一列 這裡是第二列

CSS:

#wrap{ position:relative;width:770px;}

#column1{ position:absolute; top:0; left:0; width:300px;}

#column2{position:absolute; top:0; right:0; width:470px;}

他們的差別在哪?

顯然,float是相對定位的,會随着浏覽器的大小和分辨率的變化而改變,而position就不行了,是以一般情況下還是float布局!

CSS常用布局執行個體

一列

單行一列

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; }

三列

單行三列

絕對定位

#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:

這裡是第一列 這裡是第二列 這裡是第三列

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:

This is the main content.

This is the left sidebar.

This is the right sidebar.

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:

這裡是頂行 這裡是第一列 這裡是第二列 這裡是第三列

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:

這裡是頂行 這裡是第一列 這裡是第二列 這裡是第三列 這裡是底部一行

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:這裡列出的是常用的例子,而非研究之用,對一每個盒子,我都沒有設定margin,padding,boeder等屬性,是因為我個人覺得,含有寬度定位的時候,最好不好用到他們,除非必不得已,因為如果不是這樣的話,解決浏覽器相容問題,會讓你頭疼,而且産生一系列CSS代碼,我覺得這樣的效率和效果都不好!

CSS布局進階技巧

margin和padding總是有可能要用到,而産生的問題如何解決呢?由于浏覽器解釋容器寬度的方法不同:

IE 6.0 Firefox Opera等是

真實寬度=width+padding+border+margin

IE5.X

真實寬度=width-padding-border-margin

很明顯,第一種下很完美的布局在第二種情況下後果是很凄慘的!

解決的方法是 hack

div.content {

width:400px; //這個是錯誤的width,所有浏覽器都讀到了

voice-family: "\"}\""; //IE5.X/win忽略了"\"}\""後的内容

voice-family:inherit;

width:300px; //包括IE6/win在内的部分浏覽器讀到這句,新的數值(300px)覆寫掉了舊的

}

html>body .content { //html>body是CSS2的寫法

width:300px; //支援CSS2該寫法的浏覽器(非IE5)有幸讀到了這一句

}

div.content {

width:300px !important; //這個是正确的width,大部分支援!important标記的浏覽器使用這裡的數值

width(空格):400px; //IE6/win不解析這句,是以IE6/win仍然認為width的值是300px;而IE5.X/win讀到這句,新的數值(400px)覆寫掉了舊的,因為!important标記對他們不起作用

}

html>body .content { //html>body是CSS2的寫法

width:300px; //支援CSS2該寫法的浏覽器有幸讀到了這一句

}

列等高技巧

n行n列布局,每列高度(事先并不能确定哪列的高度)的相同,是每個設計師追求的目标,做法有:背景圖填充、加JS腳本的

方法和容器溢出部分隐藏和列的負底邊界和正的内更新檔相結合的方法。

背景圖填充法:

xhtml:

這是第一列 這是第二列

css:

#wrap{ width:776px; background:url(bg.gif) repeat-y 300px;}

#column1{ float:left; width:300px;}

#column2{ float:right; width:476px;}

.clear{ clear:both;}

就是将一個npx寬的一張圖檔在外部容器縱向重複,定位到兩列交錯的位置縱向重複,在視覺上産生了兩列高度一樣的錯覺