1、基于flex布局
.big{ width: 300px; height: 300px; background-color: red; display: flex; /* justify-content: center; align-items: center; */ } .small{ width: 100px; height: 100px; background-color: green; margin: auto; }
2、基于定位
.big{ width: 300px; height: 300px; background-color: red; position: relative; } .small{ width: 100px; height: 100px; background-color: green; position: absolute; left: 50%; top: 50%; /* margin-left: -50px; margin-top: -50px; */ transform: translate(-50%,-50%); }
3、基于表格样式
.big{ width: 300px; height: 300px; background-color: red; display: table-cell; vertical-align: middle; } .small{ width: 100px; height: 100px; background-color: green; margin: auto; }