1. 程式人生 > 其它 >相對於視窗水平垂直居中的3種方法

相對於視窗水平垂直居中的3種方法

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            * {
                margin: 0;
            }

            .a {
                width: 300px;
                height: 300px;
                background-color
: aquamarine; position: absolute; left: calc(50% - 150px); top: calc(50% - 150px); } .b { width: 200px; height: 200px; background-color: pink; position: absolute; left
: 50%; top: 50%; margin-top: -100px; margin-left: -100px; } .c { width: 100px; height: 100px; background-color: orange; margin: auto; position: absolute; top
: 0; right: 0; bottom: 0; left: 0; } </style> </head> <body> <div class="a"> 方法1 </div> <div class="b"> 方法2 </div><div class="c"> 方法3 </div> </body> </html>

固定定位同樣適用!!!