1.浮动始终上下左右居中
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
</head>
<style type="text/css">
body{
background: yellow;
padding:0;
margin: 0;
}
#fixed-div{
width: 200px;
height: 200px;
background: slateblue;
position: fixed;
margin:auto;
left:0;
right:0;
top:0;
bottom:0;
}
</style>
<body>
<div id="fixed-div">
浮动居中
</div>
</body>
</html>
效果如下
滚动条下拉依然上下左右居中
left:0;和right:0;控制左右居中 不需要左右居中可以移除这两个
同理上下居中也是一个道理
2.普通居中 会随着滚动条运动而变化位置
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
</head>
<style type="text/css">
body{
background: yellow;
padding:0;
margin: 0;
}
#center-div{
width: 200px;
height: 200px;
background: slateblue;
margin: auto;
}
</style>
<body>
<div id="center-div">
居中
</div>
</body>
</html>
效果图如上 滚动条滚动就会随着一起运动 显示不完全