`
ice-cream
  • 浏览: 320973 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

css实现垂直居中

    博客分类:
  • Css
阅读更多

如果是用table写的表格,那么用vertical-align:middle就可以实现垂直居中

 

如果是用div写的表格,那么需要这样写:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
*{padding:0;margin:0;}
#container { width: 500px; height: 600px; display: table; border:1px solid;}
#position { display: table-cell; vertical-align: middle; text-align: center; }
#container { *position: relative; }
#position { *position: absolute; *top: 50%; }
#content { *position: relative; *top: -50%; }
</style>
</head>
<div id="container">
	<div id="position">
		<div id="content">这里是要垂直居中的内容</div>
	</div>
</div>
</html>

 hack写法:在属性前加“*”或者“#”的声明可以仅让IE7及以下版本理解,而其它任何浏览器都无法理解这个”*”和“#”。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics