DIV水平,垂直方向都居中于浏览器

    xiaoxiao2021-03-25  26

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>overflow: auto</title> <style type="text/css"> div { position: absolute; top: 50%; left: 50%; margin: -150px 0 0 -200px; width: 400px; height: 300px; border: 1px solid #008800; } </style> </head> <body> <div>让层垂直居中于浏览器窗口</div> </body> </html>

    其实解决的思路是这样的:首们需要position:absolute;绝对定位.,使用外边距margin负值的方法。负值的大小为层自身宽度高度除以二。 如:一个层宽度是400,高度是300。使用绝对定位距离上部与左部都设置成50%。而margin-top的值为-150。margin-left的值为-200。这样我们就实现了层垂直居中于浏览器的样式编写。

    转载请注明原文地址: https://ju.6miu.com/read-150410.html

    最新回复(0)