在說到這個問題的時候,也許有人會問CSS中不是有vertical-align屬性來設置垂直居中的嗎?即使是某些瀏覽器不支持我只需做少許的CSS Hack技術就可以啊!所以在這里我還要啰嗦兩句,CSS中的確是有vertical-align屬性,但是它只對(X)HTML元素中擁有valign特性的元素才生效,例如表格元素中的<td>、<th>、<caption>等,而像<div>、<span>這樣的元素是沒有valign特性的,因此使用vertical-align對它們不起作用。
Tips: 完美解決方案在文末!
一、單行垂直居中
如果一個容器中只有一行文字,對它實現居中相對比較簡單,我們只需要設置它的實際高度height和所在行的高度line-height相等即可。
|
1
2
3
4
5
6
7
|
如:
div {
height
:
25px
;
line-height
:
25px
;
overflow
:
hidden
;
}
|
這段代碼很簡,后面使用overflow:hidden的設置是為了防止內容超出容器或者產生自動換行,這樣就達不到垂直居中效果了。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!
DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
head
>
<
title
> 單行文字實現垂直居中 </
title
>
<
meta
http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
style
type="text/css">
body { font-size:12px;font-family:tahoma;}
div {
height:25px;
line-height:25px;
border:1px solid #FF0099;
background-color:#FFCCFF;
}
</
style
>
</
head
>
<
body
>
<
div
>現在我們要使這段文字垂直居中顯示!</
div
>
</
body
>
</
html
>
|
二、多行未知高度文字的垂直居中
如果一段內容,它的高度是可變的那么我們就可以使用上一節講到的實現水平居中時使用到的最后一種方法,就是設定Padding,使上下的padding值相同即可。同樣的,這也是一種“看起來”的垂直居中方式,它只不過是使文字把<div>完全填充的一種訪求而已。可以使用類似下面的代碼:
|
1
2
3
|
div {
padding:25px;
}
|
這種方法的優點就是它可以在任何瀏覽器上運行,並且代碼很簡單,只不過這種方法應用的前提就是容器的高度必須是可伸縮的。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!
DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
head
>
<
title
> 多行文字實現垂直居中 </
title
>
<
meta
http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
style
type="text/css">
body { font-size:12px;font-family:tahoma;}
div {
padding:25px;
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
}
</
style
>
</
head
>
<
body
>
<
div
><
br
> <
pre
>現在我們要使這段文字垂直居中顯示!
</
pre
><
br
> </
div
>
</
body
>
</
html
>
|
三、多行文本固定高度的居中
在本文的一開始,我們已經說過CSS中的vertical-align屬性只會對擁有valign特性的(X)HTML標簽起作用,但是在CSS中還有一個display屬性能夠模擬<table>,所以我們可以使用這個屬性來讓<div>模擬<table>就可以使用vertical-align了。注意,display:table和display:table-cell的使用方法,前者必須設置在父元素上,后者必須設置在子元素上,因此我們要為需要定位的文本再增加一個<div>元素:
|
1
2
3
4
5
6
7
8
9
10
11
|
div#wrap {
height:400px;
display:table;
}
div#content {
vertical-align:middle;
display:table-cell;
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<!
DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
head
>
<
title
> 多行文字實現垂直居中 </
title
>
<
meta
http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
style
type="text/css">
body { font-size:12px;font-family:tahoma;}
div#wrap {
height:400px;
display:table;
}
div#content {
vertical-align:middle;
display:table-cell;
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
}
</
style
>
</
head
>
<
body
>
<
div
id="wrap">
<
div
id="content"><
br
> <
pre
><
br
> 現在我們要使這段文字垂直居中顯示!
div#wrap {
height:400px;
display:table;
}
div#content {
vertical-align:middle;
display:table-cell;
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
}
</
pre
><
br
> </
div
>
</
div
>
</
body
>
</
html
>
|
這個方法應該是很理想了,但是不幸的是Internet Explorer 6 並不能正確地理解display:table和display:table-cell,因此這種方法在Internet Explorer 6及以下的版本中是無效的。嗯,這讓人很郁悶!不過我們還其它的辦法。
四、在Internet Explorer中的解決方案
在Internet Explorer 6及以下版本中,在高度的計算上存在着缺陷的。在Internet Explorer 6中對父元素進行定位后,如果再對子元素進行百分比計算時,計算的基礎似乎是有繼承性的(如果定位的數值是絕對數值沒有這個問題,但是使用百分比計算的基礎將不再是該元素的高度,而從父元素繼承來的定位高度)。
例如,我們有下面這樣一個(X)HTML代碼段:
|
1
2
3
4
5
6
|
<
div
id="wrap">
<
div
id="subwrap">
<
div
id="content">
</
div
>
</
div
>
</
div
>
|
如果我們對subwrap進行了絕對定位,那么content也會繼承了這個這個屬性,雖然它不會在頁面中馬上顯示出來,但是如果再對content進行相對定位的時候,你使用的100%分比將不再是content原有的高度。例如,我們設定了subwrap的position為40%,我們如果想使content的上邊緣和wrap重合的話就必須設置top:-80%;那么,如果我們設定subwrap的top:50%的話,我們必須使用100%才能使content回到原來的位置上去,但是如果我們把content也設置50%呢?那么它就正好垂直居中了。所以我們可以使用這中方法來實現Internet Explorer 6中的垂直居中:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
div#wrap {
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
height:400px;
position:relative;
}
div#subwrap {
position:absolute;
border:1px solid #000;
top:50%;
}
div#content {
border:1px solid #000;
position:relative;
top:-50%;
}
|
當然,這段代碼只能在Internet Exlporer 6等計算存在問題的瀏覽器中才會有作用。(不過我不解,我查閱了很多文章,不知道是因為出處相同還是什么原因,似乎很多人都不願意去解釋Internet Exlporer 6中這這個Bug的原理,我也只是了解了一點皮毛,還要再研究)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<!
DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
head
>
<
title
> 多行文字實現垂直居中 </
title
>
<
meta
http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
style
type="text/css">
body { font-size:12px;font-family:tahoma;}
div#wrap {
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
height:400px;
position:relative;
}
div#subwrap {
position:absolute;
top:50%;
}
div#content {
position:relative;
top:-50%;
}
</
style
>
</
head
>
<
body
>
<
div
id="wrap">
<
div
id="subwrap">
<
div
id="content"><
pre
>現在我們要使這段文字垂直居中顯示!
div#wrap {
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
height:500px;
position:relative;
}
div#subwrap {
position:absolute;
border:1px solid #000;
top:50%;
}
div#content {
border:1px solid #000;
position:relative;
top:-50%;
}<
br
> </
pre
>
</
div
>
</
div
>
</
div
>
</
body
>
</
html
>
|
五、完美的解決方案
那么我們綜合上面兩種方法就可以得到一個完美的解決方案,不過這要用到CSS hack的知識。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
div#wrap {
display:table;
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
height:400px;
_position:relative;
overflow:hidden;
}
div#subwrap {
vertical-align:middle;
display:table-cell;
_position:absolute;
_top:50%;
}
div#content {
_position:relative;
_top:-50%;
}
|
至此,一個完美的居中方案就產生了。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<!
DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
head
>
<
title
> 多行文字實現垂直居中 </
title
>
<
meta
http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
style
type="text/css">
body { font-size:12px;font-family:tahoma;}
div#wrap {
display:table;
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
height:400px;
_position:relative;
overflow:hidden;
}
div#subwrap {
vertical-align:middle;
display:table-cell;
_position:absolute;
_top:50%;
}
div#content {
_position:relative;
_top:-50%;
}
</
style
>
</
head
>
<
body
>
<
div
id="wrap">
<
div
id="subwrap">
<
div
id="content"><
br
> <
pre
>現在我們要使這段文字垂直居中顯示!
div#wrap {
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
height:500px;
position:relative;
}
div#subwrap {
position:absolute;
border:1px solid #000;
top:50%;
}
div#content {
border:1px solid #000;
position:relative;
top:-50%;
}<
br
> </
pre
>
</
div
>
</
div
>
</
div
>
</
body
>
</
html
>
|
PS:垂直居中vertical-align的值是middle,而水平居中align的值是center,雖然同是居中但關鍵字不同。
六、實測可以完美實現各種瀏覽器兼容的居中方案
下面這段代碼經過實測,可以完美兼容IE7以上的IE瀏覽器,其它標准瀏覽器如火狐、谷歌等也沒有問題。
說明:盡管有CSS的vertical-align特性,但是並不能有效解決未知高度的垂直居中問題(在一個DIV標簽里有未知高度的文本或圖片的情況下)。標准瀏覽器如Mozilla, Opera, Safari等.,可將父級元素顯示方式設定為TABLE(display: table;) ,內部子元素定為table-cell (display: table-cell),通過vertical-align特性使其垂直居中,但非標准瀏覽器是不支持的。非標准瀏覽器只能在子元素里設距頂部50%,里面再套個元素距頂部-50% 來抵消。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<!
doctype
html>
<
html
>
<
head
>
<
meta
charset="utf-8">
<
title
>水平垂直居中</
title
>
<
style
type="text/css">
body {padding: 0; margin: 0;}
body,html{height: 100%;}
#outer {height: 100%; overflow: hidden; position: relative;width: 100%;}
#outer[id] {display: table; position: static;}
#middle {position: absolute; top: 50%;} /* for explorer only*/
#middle[id] {display: table-cell; vertical-align: middle; position: static;}
#inner {position: relative; top: -50%;margin: 0 auto;} /* for explorer only */
div.greenBorder {width:500px;height:584px;background:#333;}
*+html #outer[id]{position: relative;}
*+html #middle[id]{position: absolute; }
</
style
>
</
head
>
<
body
>
<
div
id="outer">
<
div
id="middle">
<
div
id="inner" class="greenBorder">
</
div
>
</
div
>
</
div
>
</
body
>
</
html
>
|
以上CSS代碼的優點是沒有hacks,采用了IE不支持的CSS2選擇器#value[id]。
CSS2選擇器#value[id]相當於選擇器#value,但是Internet Explorer不支持這種類型的選擇器。同樣地.value[class],相當於.value,這些只有標准瀏覽器能讀懂。
測試:Firefox1.5、Opera9.0、IE6.0、IE5.0通過。上面的代碼不支持IE7,還需要在最下面加二句:
|
1
2
3
|
*+html #outer[id]{position: relative;}
*+html #middle[id]{position: absolute; }
|
在說到這個問題的時候,也許有人會問CSS中不是有vertical-align屬性來設置垂直居中的嗎?即使是某些瀏覽器不支持我只需做少許的CSS Hack技術就可以啊!所以在這里我還要啰嗦兩句,CSS中的確是有vertical-align屬性,但是它只對(X)HTML元素中擁有valign特性的元素才生效,例如表格元素中的<td>、<th>、<caption>等,而像<div>、<span>這樣的元素是沒有valign特性的,因此使用vertical-align對它們不起作用。
Tips: 完美解決方案在文末!
一、單行垂直居中
如果一個容器中只有一行文字,對它實現居中相對比較簡單,我們只需要設置它的實際高度height和所在行的高度line-height相等即可。
|
1
2
3
4
5
6
7
|
如:
div {
height
:
25px
;
line-height
:
25px
;
overflow
:
hidden
;
}
|
這段代碼很簡,后面使用overflow:hidden的設置是為了防止內容超出容器或者產生自動換行,這樣就達不到垂直居中效果了。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!
DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
head
>
<
title
> 單行文字實現垂直居中 </
title
>
<
meta
http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
style
type="text/css">
body { font-size:12px;font-family:tahoma;}
div {
height:25px;
line-height:25px;
border:1px solid #FF0099;
background-color:#FFCCFF;
}
</
style
>
</
head
>
<
body
>
<
div
>現在我們要使這段文字垂直居中顯示!</
div
>
</
body
>
</
html
>
|
二、多行未知高度文字的垂直居中
如果一段內容,它的高度是可變的那么我們就可以使用上一節講到的實現水平居中時使用到的最后一種方法,就是設定Padding,使上下的padding值相同即可。同樣的,這也是一種“看起來”的垂直居中方式,它只不過是使文字把<div>完全填充的一種訪求而已。可以使用類似下面的代碼:
|
1
2
3
|
div {
padding:25px;
}
|
這種方法的優點就是它可以在任何瀏覽器上運行,並且代碼很簡單,只不過這種方法應用的前提就是容器的高度必須是可伸縮的。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!
DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
head
>
<
title
> 多行文字實現垂直居中 </
title
>
<
meta
http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
style
type="text/css">
body { font-size:12px;font-family:tahoma;}
div {
padding:25px;
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
}
</
style
>
</
head
>
<
body
>
<
div
><
br
> <
pre
>現在我們要使這段文字垂直居中顯示!
</
pre
><
br
> </
div
>
</
body
>
</
html
>
|
三、多行文本固定高度的居中
在本文的一開始,我們已經說過CSS中的vertical-align屬性只會對擁有valign特性的(X)HTML標簽起作用,但是在CSS中還有一個display屬性能夠模擬<table>,所以我們可以使用這個屬性來讓<div>模擬<table>就可以使用vertical-align了。注意,display:table和display:table-cell的使用方法,前者必須設置在父元素上,后者必須設置在子元素上,因此我們要為需要定位的文本再增加一個<div>元素:
|
1
2
3
4
5
6
7
8
9
10
11
|
div#wrap {
height:400px;
display:table;
}
div#content {
vertical-align:middle;
display:table-cell;
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<!
DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
head
>
<
title
> 多行文字實現垂直居中 </
title
>
<
meta
http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
style
type="text/css">
body { font-size:12px;font-family:tahoma;}
div#wrap {
height:400px;
display:table;
}
div#content {
vertical-align:middle;
display:table-cell;
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
}
</
style
>
</
head
>
<
body
>
<
div
id="wrap">
<
div
id="content"><
br
> <
pre
><
br
> 現在我們要使這段文字垂直居中顯示!
div#wrap {
height:400px;
display:table;
}
div#content {
vertical-align:middle;
display:table-cell;
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
}
</
pre
><
br
> </
div
>
</
div
>
</
body
>
</
html
>
|
這個方法應該是很理想了,但是不幸的是Internet Explorer 6 並不能正確地理解display:table和display:table-cell,因此這種方法在Internet Explorer 6及以下的版本中是無效的。嗯,這讓人很郁悶!不過我們還其它的辦法。
四、在Internet Explorer中的解決方案
在Internet Explorer 6及以下版本中,在高度的計算上存在着缺陷的。在Internet Explorer 6中對父元素進行定位后,如果再對子元素進行百分比計算時,計算的基礎似乎是有繼承性的(如果定位的數值是絕對數值沒有這個問題,但是使用百分比計算的基礎將不再是該元素的高度,而從父元素繼承來的定位高度)。
例如,我們有下面這樣一個(X)HTML代碼段:
|
1
2
3
4
5
6
|
<
div
id="wrap">
<
div
id="subwrap">
<
div
id="content">
</
div
>
</
div
>
</
div
>
|
如果我們對subwrap進行了絕對定位,那么content也會繼承了這個這個屬性,雖然它不會在頁面中馬上顯示出來,但是如果再對content進行相對定位的時候,你使用的100%分比將不再是content原有的高度。例如,我們設定了subwrap的position為40%,我們如果想使content的上邊緣和wrap重合的話就必須設置top:-80%;那么,如果我們設定subwrap的top:50%的話,我們必須使用100%才能使content回到原來的位置上去,但是如果我們把content也設置50%呢?那么它就正好垂直居中了。所以我們可以使用這中方法來實現Internet Explorer 6中的垂直居中:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
div#wrap {
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
height:400px;
position:relative;
}
div#subwrap {
position:absolute;
border:1px solid #000;
top:50%;
}
div#content {
border:1px solid #000;
position:relative;
top:-50%;
}
|
當然,這段代碼只能在Internet Exlporer 6等計算存在問題的瀏覽器中才會有作用。(不過我不解,我查閱了很多文章,不知道是因為出處相同還是什么原因,似乎很多人都不願意去解釋Internet Exlporer 6中這這個Bug的原理,我也只是了解了一點皮毛,還要再研究)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<!
DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
head
>
<
title
> 多行文字實現垂直居中 </
title
>
<
meta
http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
style
type="text/css">
body { font-size:12px;font-family:tahoma;}
div#wrap {
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
height:400px;
position:relative;
}
div#subwrap {
position:absolute;
top:50%;
}
div#content {
position:relative;
top:-50%;
}
</
style
>
</
head
>
<
body
>
<
div
id="wrap">
<
div
id="subwrap">
<
div
id="content"><
pre
>現在我們要使這段文字垂直居中顯示!
div#wrap {
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
height:500px;
position:relative;
}
div#subwrap {
position:absolute;
border:1px solid #000;
top:50%;
}
div#content {
border:1px solid #000;
position:relative;
top:-50%;
}<
br
> </
pre
>
</
div
>
</
div
>
</
div
>
</
body
>
</
html
>
|
五、完美的解決方案
那么我們綜合上面兩種方法就可以得到一個完美的解決方案,不過這要用到CSS hack的知識。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
div#wrap {
display:table;
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
height:400px;
_position:relative;
overflow:hidden;
}
div#subwrap {
vertical-align:middle;
display:table-cell;
_position:absolute;
_top:50%;
}
div#content {
_position:relative;
_top:-50%;
}
|
至此,一個完美的居中方案就產生了。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<!
DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
head
>
<
title
> 多行文字實現垂直居中 </
title
>
<
meta
http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
style
type="text/css">
body { font-size:12px;font-family:tahoma;}
div#wrap {
display:table;
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
height:400px;
_position:relative;
overflow:hidden;
}
div#subwrap {
vertical-align:middle;
display:table-cell;
_position:absolute;
_top:50%;
}
div#content {
_position:relative;
_top:-50%;
}
</
style
>
</
head
>
<
body
>
<
div
id="wrap">
<
div
id="subwrap">
<
div
id="content"><
br
> <
pre
>現在我們要使這段文字垂直居中顯示!
div#wrap {
border:1px solid #FF0099;
background-color:#FFCCFF;
width:760px;
height:500px;
position:relative;
}
div#subwrap {
position:absolute;
border:1px solid #000;
top:50%;
}
div#content {
border:1px solid #000;
position:relative;
top:-50%;
}<
br
> </
pre
>
</
div
>
</
div
>
</
div
>
</
body
>
</
html
>
|
PS:垂直居中vertical-align的值是middle,而水平居中align的值是center,雖然同是居中但關鍵字不同。
六、實測可以完美實現各種瀏覽器兼容的居中方案
下面這段代碼經過實測,可以完美兼容IE7以上的IE瀏覽器,其它標准瀏覽器如火狐、谷歌等也沒有問題。
說明:盡管有CSS的vertical-align特性,但是並不能有效解決未知高度的垂直居中問題(在一個DIV標簽里有未知高度的文本或圖片的情況下)。標准瀏覽器如Mozilla, Opera, Safari等.,可將父級元素顯示方式設定為TABLE(display: table;) ,內部子元素定為table-cell (display: table-cell),通過vertical-align特性使其垂直居中,但非標准瀏覽器是不支持的。非標准瀏覽器只能在子元素里設距頂部50%,里面再套個元素距頂部-50% 來抵消。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<!
doctype
html>
<
html
>
<
head
>
<
meta
charset="utf-8">
<
title
>水平垂直居中</
title
>
<
style
type="text/css">
body {padding: 0; margin: 0;}
body,html{height: 100%;}
#outer {height: 100%; overflow: hidden; position: relative;width: 100%;}
#outer[id] {display: table; position: static;}
#middle {position: absolute; top: 50%;} /* for explorer only*/
#middle[id] {display: table-cell; vertical-align: middle; position: static;}
#inner {position: relative; top: -50%;margin: 0 auto;} /* for explorer only */
div.greenBorder {width:500px;height:584px;background:#333;}
*+html #outer[id]{position: relative;}
*+html #middle[id]{position: absolute; }
</
style
>
</
head
>
<
body
>
<
div
id="outer">
<
div
id="middle">
<
div
id="inner" class="greenBorder">
</
div
>
</
div
>
</
div
>
</
body
>
</
html
>
|
以上CSS代碼的優點是沒有hacks,采用了IE不支持的CSS2選擇器#value[id]。
CSS2選擇器#value[id]相當於選擇器#value,但是Internet Explorer不支持這種類型的選擇器。同樣地.value[class],相當於.value,這些只有標准瀏覽器能讀懂。
測試:Firefox1.5、Opera9.0、IE6.0、IE5.0通過。上面的代碼不支持IE7,還需要在最下面加二句:
|
1
2
3
|
*+html #outer[id]{position: relative;}
*+html #middle[id]{position: absolute; }
|
