<!--html结构-->
<div class="box1">文本垂直水平居中</div>
<div class="father">
<div class="son"></div>
</div>
方法一:
.box1{
width: 100%;
height: 100px;
background-color: #f00;
text-align: center;
line-height: 100px;
}
方法二:
.father{
position: relative;
width: 200px;
height: 200px;
background-color: #00f;
}
.son{
position: absolute;
top: 50%;
left: 50%;
margin-top: -25px;
margin-left: -25px;
width: 50px;
height: 50px;
background-color: #0f0;
}
方法三:
.father {
position: relative;
width: 200px;
height: 200px;
background-color: #f00;
}
.son {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 50%;
height: 50%;
background-color: yellow;
}
方法四:
.father {
position: relative;
width: 200px;
height: 200px;
background-color: #f00;
display: flex;
}
.son {
margin: auto;
width: 50%;
height: 50%;
background-color: yellow;
}
方法五:
.father {
position: relative;
width: 200px;
height: 200px;
background-color: #00f;
}
.son {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
width: 50%;
height: 50%;
background-color: #0f0;
}
方法六:
.father {
position: relative;
width: 200px;
height: 200px;
background-color: #f00;
display: flex;
justify-content:center;
align-items: center;
}
.son {
width: 50%;
height: 50%;
background-color: yellow;
}