All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
用CSS解决未知高度的DIV垂直居中的问题_[Html教程]

Writer: 归海一刀 Article type: Programming skills(编程技巧) Time: 2014/3/30 5:35:37 Browse times: 328 Comment times: 0

用CSS解决未知高度的DIV垂直居中的问题_[Html教程]


Head photo

Go homepage
Upload pictures
Write articles

用CSS解决未知高度的DIV垂直居中的问题_[Html教程] 本篇详细介绍了用CSS解决未知高度的DIV垂直居中的问题的文章主题。

原文标题:Vertical Centering in CSS
副标题:Yuhu's Definitive Solution
with Unknown Height

尽管有CSS的vertical-align特性,但是并不能有效解决未知高度的垂直居中问题(在一个DIV标签里有未知高度的文本或图片的情况下)。

标准浏览器如Mozilla, Opera, Safari等.,可将父级元素显示方式设定为TABLE(display: table;) ,内部子元素定为table-cell (display: table-cell),通过vertical-align特性使其垂直居中,但非标准浏览器是不支持的。

非标准浏览器只能在子元素里设距顶部50%,里面再套个元素距顶部-50% 来抵消。

CSS

body {padding: 0; margin: 0;}
body,html{height: 100%;}
#outer {height: 100%; overflow: hidden; position: relative;width
: 100%; background:ivory;}
#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%;width: 400px;margin: 0 auto;}
/* for explorer only */
div.greenBorder {border: 1px solid green; background-color: ivory;}

xhtml






以上CSS代码的优点是没有hacks,采用了IE不支持的CSS2选择器#value[id]。

CSS2选择器#value[id]相当于选择器#value,但是Internet Explorer不支持这种类型的选择器。同样地.value[class],相当于.value,这些只有标准浏览器能读懂。

测试:Firefox1.5、Opera9.0、IE6.0、IE5.0通过。


         




There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.