若为单行文本,使用 line-height 设为父元素的高度即可居中。
<div class="father">
<div class="child"></div>
</div>
.father{
height: 350px;
width: 350px;
background-color: red;
overflow: hidden;
}
.child{
height: 200px;
width: 200px;
background-color: blue;
margin:75px 0;
}
.father{
height: 350px;
width: 350px;
background-color: red;
position: relative;
}
.child{
height: 200px;
width: 200px;
background-color: blue;
position: absolute;
top: 50%;
margin-top: 100px;
}
.father{
height: 350px;
width: 350px;
background-color: red;
position: relative;
}
.child{
height: 200px;
width: 200px;
background-color: blue;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.father{
height: 350px;
width: 350px;
background-color: red;
position: relative;
}
.child{
height: 200px;
width: 200px;
position: absolute;
top: 50%;
}
.new-child{
height: 200px;
width: 200px;
background-color: blue;
position: relative;
bottom: 50%;
}