代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>div居中</title> <style> * { margin:0; padding:0; } .content { width:400px; height:400px; background:orange; border: 1px solid green; position:relative; margin: 100px auto; } .nav { width:80px; height:80px; line-height:80px; text-align:center; background:green; margin: auto; position:absolute; left: 0; top: 0; bottom:0; right: 0; } </style> </head> <body> <div class="content"> <div class="nav">这是内容</div> </div> </body> </html>
效果: