<title>垂直居中模块示例</title>
<style type="text/css">
#center{
background:#999999;
position:absolute;
top:50%;
left:50%;
margin-top:-256px; /*高度的一半 需要是负值*/
margin-left:-50%; /*margin-left:-100px; 刚好为宽度的一半*/
width:100%; /*width:200px;*/
height:513px;
}
</style>
</head>
<body>
<div id="center">垂直居中模块示例</div>
</body>
</html>
<title>垂直居中模块示例</title>
<style type="text/css">
#center{
background:#999999;
position:absolute;
top:50%;
left:50%;
margin-top:-256px;
margin-left:-50%;
width:100%;
height:513px;
}
</style>
</head>
<body>
<div id="center">垂直居中模块示例</div>
</body>
</html>
基本原理:
1、使用CSS中的position:absolute 进行绝对定位,然后用百分比定位。
2、固定居中模块的大小
3、使用CSS中的负边距进行居中,注意负边距一定要固定大小的一半,这样才能刚好的垂直左右居中
<style type="text/css">
#center{
background:#999999;
position:absolute;
top:50%;
left:50%;
margin-top:-256px; /*高度的一半 需要是负值*/
margin-left:-50%; /*margin-left:-100px; 刚好为宽度的一半*/
width:100%; /*width:200px;*/
height:513px;
}
</style>
</head>
<body>
<div id="center">垂直居中模块示例</div>
</body>
</html>
<title>垂直居中模块示例</title>
<style type="text/css">
#center{
background:#999999;
position:absolute;
top:50%;
left:50%;
margin-top:-256px;
margin-left:-50%;
width:100%;
height:513px;
}
</style>
</head>
<body>
<div id="center">垂直居中模块示例</div>
</body>
</html>
基本原理:
1、使用CSS中的position:absolute 进行绝对定位,然后用百分比定位。
2、固定居中模块的大小
3、使用CSS中的负边距进行居中,注意负边距一定要固定大小的一半,这样才能刚好的垂直左右居中