<!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>Document</title>
<style>
/* 水平垂直居中 */
section{
width: 500px;
height: 500px;
border:solid 1px red;
position: relative;
}
/* div{
width: 300px;
height: 300px;
background: blueviolet;
position: absolute;
margin: auto;
top:0;
left:0;
right: 0;
bottom:0;
} */
/* 第二种 */
/* div{
position: absolute;
width: 300px;
height: 200px;
top:calc(50% - 100px);
left:calc(50% - 150px);
background: pink;
} */
/* 第三种 */
div{
position: absolute;
width: 300px;
height: 200px;
top:50%;
left: 50%;
background: brown;
transform: translateX(-150px) translateY(-100px);
}
</style>
</head>
<body>
<section>
<div>
</div>
</section>
</body>
</html>
div垂直水平居中的四种方法(更新)
最新推荐文章于 2022-06-10 15:19:40 发布