<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水平垂直居中</title>
</head>
<style>
/*子元素在父元素中水平垂直居中的三种方法*/
.one{
height: 200px;
width: 200px;
border: 2px solid #ccc;
position: relative;
background-color: lightblue;
}
.one_content{
height: 100px;
width: 100px;
border: 2px solid #ccc;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin:auto;
background-color: pink;
}
.two{
height: 200px;
width: 200px;
border: 2px solid #ccc;
position: relative;
}
.two_content{
height: 100px;
width: 100px;
border: 2px solid #ccc;
position: absolute;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
background-color: yellow;
}
.three{
height: 200px;
width: 200px;
border: 2px solid #ccc;
display: flex;
justify-content: center;
align-items: center;
}
.three_content{
height: 100px;
width: 100px;
border: 2px solid #ccc;
background-color: red;
}
</style>
<body>
<div class="one">
<div class="one_content"></div>
</div>
<div class="two">
<div class="two_content"></div>
</div>
<div class="three">
<div class="three_content"></div>
</div>
</body>
</html>
三种方法让子元素在父元素中水平垂直居中
最新推荐文章于 2024-07-02 18:00:19 发布