作业要求:使十字架在页面的垂直水平居中显示
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.box {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
width: 300px;
height: 100px;
background-color: #FF00FE;
}
.box1 {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
width: 100px;
height: 300px;
background-color: #FF00FE;
}
</style>
</head>
<body>
<div class="box"></div>
<div class="box1"></div>
</body>
</html>
代码里面使用到的 position: absolute;就是绝对定位,它外面没有包含框,就相当于浏览器定位。
而绝对定位+marign,就可以让明确宽高的盒子水平垂直居中于浏览器。