1. 定位+margin
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 400px;
height: 400px;
border: 1px solid;
position: relative;
}
.son{
position: absolute;
width: 200px;
height: 200px;
background-color: red;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
</style>
<body>
<div class="father">
<div class="son"></div>
</div>
<body>
2. 定位+transform
<style>
.father{
width: 400px;
height: 400px;
border: 1px solid;
position: relative;
}
.son{
position: absolute;
width: 200px;
height: 200px;
background-color: blue;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<body>
<div class="father">
<div class="son"></div>
</div>
<body>
3. flex
<style>
.father{
width: 400px;
height: 400px;
border: 1px solid;
display: flex;
align-item: center;
justify-content: center;
}
.son{
width: 200px;
height: 200px;
background-color: pink;
}
</style>
<body>
<div class="father">
<div class="son"></div>
</div>
<body>
4. grid布局
标签:居中,50%,元素,height,垂直,width,position,400px,200px From: https://blog.csdn.net/m0_66706006/article/details/1400890125. table布局