absolute定位有一个很常用的用途,就是当希望子元素在父元素的正中央时,可以这样操作:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .box{ 500px; height: 500px; background: blue; position: absolute; top: 100px; left: 100px; } .son{ 100px; height: 100px; background: black; position: absolute; left: 0; /* 注意不要漏了上下左右为0,否则不会成功 */ right: 0; top: 0; bottom: 0; margin: auto; } </style> </head> <body> <div class="box"> <div class="son"></div> </div> </body> </html>