话不多说先上要实现的效果:(很简单水平居中)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <style type="text/css"> /* 方式一 */ .box{ width: 100px; height: 100px; color: #fff; background: #000000; position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; } </style> <body> <div class="box">方式一</div> </body> </html>
/* 方式二 */ .box{ width: 100px; height: 100px; color: #fff; background: #000000; position: absolute; left: 50%; top: 50%; margin-left: -50px; margin-top: -50px; }
/* 方式三 */ .box{ width: 100px; height: 100px; color: #fff; background: #000000; position: absolute; top:50%; left:50%; transform:translate(-50%,-50%); }