首页 > Web开发 > 详细

使用css实现div垂直居中的示例

时间:2016-02-08 12:58:07      阅读:155      评论:0      收藏:0      [点我收藏+]


复制代码
代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CSS实现div居中</title>
<style>
/*外层div居中*/
#main {
position: absolute; /*极为重要*/
background-color: blue;
width:400px;
height:200px;
left:50%;
top:50%;
margin-left:-200px;
margin-top:-100px;
border:1px solid #00F;
}

#content {
position: absolute; /*极为重要*/
background-color: yellow;
width: 200px;
height: 100px;
left:50%;
top:50%;
margin-left:-100px;
margin-top:-50px;

/*div内部文字居中*/
text-align: center;
line-height:100px; /*行间距和div宽度相同*/
}
</style>
<body>
<div id="main">
<div id="content">
Sun
</div>
</div>
</body>
</html>

使用css实现div垂直居中的示例

原文:http://www.jb51.net/css/155299.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!