码迷,mamicode.com
首页 > 其他好文 > 详细

让DIV垂直居中的几种办法

时间:2015-10-16 19:00:09      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

1.使用CSS3 的伸缩盒布局

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
    #container {
        height: 400px;
        width: 100%;
        background-color: gray;

display:-webkit-flex; display: flex; flex-direction: row; -webkit-flex-direction: row; -webkit-justify-content: center; justify-content: center; -webkit-align-items: center; align-items: center; } #container > div{ height: 200px; width: 200px; background-color: red; } </style> </head> <body> <div id="container"> <div></div> </div> </body> </html>

2.position:absolute 和 margin 联合使用

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
    #container {
        height: 400px;
        width: 100%;
        background-color: gray;
        
        position: relative;
    }
    #container > div{
        height: 200px;
        width: 200px;
        background-color: red;
        
        position: absolute;
        top: 50%;
        left: 50%;
        margin: -100px 0 0 -100px;
    }
</style>
</head>

<body>
<div id="container">
    <div></div>
</div>
</body>
</html>

3.position:absolute 和 margin: auto联合使用

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
    #container {
        height: 400px;
        width: 100%;
        background-color: gray;
        
        position: relative;
    }
    #container > div{
        height: 200px;
        width: 200px;
        background-color: red;
        
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
    }
</style>
</head>

<body>
<div id="container">
    <div></div>
</div>
</body>
</html>

 

让DIV垂直居中的几种办法

标签:

原文地址:http://www.cnblogs.com/chen8840/p/4886009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!