1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 8 <style> 9 *{ 10 margin: 0; 11 padding: 0; 12 } 13 .main{ 14 width:300px; 15 height:400px; 16 position: relative; 17 top:0; 18 left:0; 19 background-color: antiquewhite; 20 } 21 .center{ 22 position: absolute; 23 width:100px; 24 height:100px; 25 top:50%; 26 left:50%; 27 background-color: red; 28 transform: translate(-50%,-50%); 29 } 30 31 </style> 32 <body> 33 34 35 <div class="main"> 36 <div class="center"> 37 </div> 38 </div> 39 </body> 40 </html> 效果: 第二种:flex布局垂直居中 1 <!DOCTYPE html> 2