css如何将块级元素中的块级元素同时水平垂直居中显示
将外层块级盒子绝对定位,内层块级盒子相对定位,然后设置内层盒子上下左右全为0,margin:auto;即可
代码如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
width: 200px;
height: 200px;
position: relative;
background-color: pink;
}
.box2{
width: 100px;
height: 100px;
position: absolute;
background-color: green;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2">
程序员
</div>
</div>
</body>
</html>
效果: