首页 > 代码库 > 两种方法实现盒子居中----以“回”字为例

两种方法实现盒子居中----以“回”字为例

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
padding:0;
margin:0;
}
.box1 {
position: absolute;
top: 50%;
left: 50%;
width: 400px;
height: 300px;
border: 50px solid #000;
margin: -200px auto auto -250px;
}
.box2{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
width: 150px;
height: 100px;
border: 50px solid #000;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2">
</div>
</div>
</body>
</html>

两种方法实现盒子居中----以“回”字为例