绝对定位来实现 block 水平垂直居中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Document</title> <style> .big{ position: relative; width: 1000px; height: 1000px; border: 1px solid red; } .sml{ /*通过定位来达到水平垂直居中*/ position: absolute; top: 0; left: 0; right: 0; bottom: 0; width: 400px; height: 400px; border: 1px solid green; margin: auto; } </style> </head> <body> <div class="big"> <div class="sml"> </div> </div> </body> </html>