垂直水平居中的几种方式,其他方式还有很多,不再列举
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> /*第一种垂直水平居中*/ .father{ width: 300px; height: 300px; background: red; display: table-cell; vertical-align: middle; } .child{ width: 100px; height: 100px; margin:0 auto; } /*第二种垂直水平居中*/ .father{ display: flex; justify-content: center; align-items: center; width: 300px; height: 300px; background: red; } .child{ width: 100px; height: 100px; } /*第三种垂直水平居中*/ .father{ width: 300px; height: 300px; background: red; position: relative; } .child{ width: 100px; height: 100px; position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: auto; } /*第四种垂直水平居中*/ .father{ width: 300px; height: 300px; background: red; position: relative; } .child{ width: 100px; height: 100px; position: absolute; left: 50%; top: 50%; margin-top: -50px; /* 高度的一半 */ margin-left: -50px; /* 宽度的一半 */ } </style> </head> <body> <div class="father"> <div class="child">123</div> </div> </body> </html>
FUCKING FRONT-END DEVELOPMENT