1、flex
思路:flex原有属性支持
.container {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.center {
width: 100px;
height: 100px;
background-color: red;
}
2、定位
思路:元素自身的拉扯
.container {
height: 100vh;
position: relative;
}
.center {
width: 100px;
height: 100px;
background-color: red;
margin: auto;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;;
}
3、定位50%+自身再移回去50%宽高
.container {
height: 100vh;
position: relative;
}
.center {
width: 100px;
height: 100px;
background-color: red;
margin: auto;
position: absolute;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
}