<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>
</head>
<style>
/* 第一种用transform */
.father{
position: relative;
width: 400px;
height: 400px;
background-color: pink;
margin: 20px auto;
}
.son{
position: absolute;
left: 50%;
top: 50%;
color: red;
transform: translate(-50%,-50%);
}
第二种用flex
.father{
display: flex;
/* 主轴居中对齐 */
justify-content: center;
/* 侧轴居中对齐 */
align-items: center;
width: 400px;
height: 400px;
background-color: pink;
margin: 20px auto;
}
.son{
color: red;
}
第三种table-cell
.father{
display: table;
width: 400px;
height: 400px;
background-color: pink;
margin: 20px auto;
}
.son{
display: table-cell;
vertical-align: middle;
text-align: center;
color: red;
font-size: 16px;
}
</style>
<body>
<div class="father">
<div class="son">未知宽高元素垂直水平居中</div>
</div>
</body>
</html>
为定义宽高盒子的垂直水平居中的方法
最新推荐文章于 2022-08-09 18:34:28 发布