首页 > 其他 > 详细

让DIV垂直居中的几种办法

时间:2015-10-16 18:56:54      阅读:255      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!