不定宽高的div垂直水平居中
1.css3的flex
1 <style> 2 .ss { 3 display: flex; 4 justify-content: center; 5 align-items: center; 6 height:600px; 7 width: 1000px; 8 background-color: #3a9; 9 } 10 .sss { 11 width: 100px; 12 height: 100px; 13 background-color: greenyellow; 14 } 15 </style> 16 <body> 17 <div class="ss"> 18 <div class="sss"></div> 19 </div> 20 </body>
2.position:absolute
.ss { width: 200px; height: 200px; background: #3a9; position:absolute; left:0; top: 0; bottom: 0; right: 0; margin: auto; } <body> <div class="ss"> </div> </body>
3.position:absolute
.ss { width: 40%; height: 200px; background: #3a9; position:absolute; left:50%; top: 50%; transform: translate(-50%, -50%); } <div class="ss"></div>