css 元素水平垂直居中方法介绍。

<!doctype html>
<html>
<head>
<meta charset=’utf-8′ />
<title></title>
<style type=”text/css”>
.out{width:400px;height:300px;margin:20px auto;display:table-cell;text-align:center;vertical-align:middle;background:#ccc;}
.out img{width:100px;height:100px;background:#fcc;}
</style>
</head>
<body>
<div class=’out’>
<img src=”" alt=”" />
</div>
</body>
</html>
其他的CSS实现垂直居中的方法
#floater{float:left; height:50%; margin-bottom:-120px;}
#content{clear:both; height:240px; position:relative;}
<div id=”floater”></div>
<div id=”content”>
Content here
</div>
<!doctype html>
<html>
<head>
<title></title>
<meta charset=’utf-8′ />
<style type=”text/css”>
.outer{height:240px;width:500px;margin:20px auto 0;position:relative;background:#ccf;}
.inner{width:300px;height:100px;background:#9f9;position:absolute;top:0;bottom:0;left:0;right:0;margin:auto;text-align: center;}
.inner img{height:100px;}
</style>
</head>
<body>
<div class=”outer”>
<div class=”inner”>
<img src=”bokan.png” alt=”" />
</div>
</div>
</body>
</html>
C
.content {
position:absolute;
top:50%;
height:240px;
margin-top:-120px; /* negative half of the height */
}
<div class=”content”>
Content goes here
</div>