一、基於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; }
二、基於定位flex
.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%); }
三、基於表格樣式spa
.big{ width: 300px; height: 300px; background-color: red; display: table-cell; vertical-align: middle; } .small{ width: 100px; height: 100px; background-color: green; margin: auto; }