css常见的水平垂直居中

来源:互联网 发布:人工智能 时时彩 编辑:程序博客网 时间:2024/06/14 11:57
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>水平垂直居中</title>
</head>
<style>
.outBox {
position: relative;
width: 300px;
height: 300px;
background: #ff8300;
border: 1pxsolid#ff8300;
border-radius: 10px;
margin: auto
}
.innerBox {
position: absolute;
width: 100px;
height: 100px;
background: rgba(0,0,0, .7);
border: 1pxsolid#ff8300;
border-radius: 6px;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
}
</style>

<body>
<div class="outBox">
<div class="innerBox">

</div>
</div>
</body>

</html>


外层相对定位,内层绝对定位。看要居中的物体大小top,left统统50%,然后margin-left,margin-top 负的物体大小的一半,
比如一个内层100px*100px的,那就是margin-left:-50px;margin-top:-50px

原创粉丝点击