1. 使用flex
/*父级元素*/
.parent-div{
width: 100%;
height: 500px;
background: greenyellow;
display: flex;
align-items: center;
justify-content: center;
}
2. 1使用position
/*父级元素*/
.parent-div{
width: 500px;
height: 500px;
background: greenyellow;
position: relative;
}
/*子级级元素*/
.children-div{
width: 100px;
height: 100px;
font-size: 20px;
background: blueviolet;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom:0;
margin: auto;
}
2.2 使用position
/*父级元素*/
.parent-div{
width: 500px;
height: 500px;
background: greenyellow;
position: relative;
}
/*子级级元素*/
.children-div{
width: 100px;
height: 100px;
font-size: 20px;
background: blueviolet;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
3 使用line-height与text-align
/*父级元素*/
.parent-div{
width: 500px;
background: greenyellow;
line-height:500px;
text-align:center;
}
/*子级级元素*/
.children-div{
width: 100px;
font-size: 20px;
display:inline;
}