CSS完成DIV垂直居中的3种方式

2021-03-17 13:27 jianzhan

下面给大伙儿共享div垂直居中的完成编码,实际编码以下所示:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF⑻">
        <title>demo</title>
    </head>
    <body>
        <style type="text/css">
            .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
            .div2{ width:40px ; height: 40px; background-color: green;}
        </style>
        <div class="div1">
            <div class="div2">
            </div>
        </div>

    </body>
</html>

如上的两个div,完成div2在div1里边是垂直居中显示信息

1、方式1

  运用margin,div1的宽减去div2的宽便是div2margin-left的标值:(100⑷0)/2=30

  div1的高减去div2的高便是div2margin-top的标值:(100⑷0)/2=30

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF⑻">
        <title>demo</title>
    </head>
    <body>
        <style type="text/css">
            .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
            .div2{ width:40px ; height: 40px; background-color: green;}

            .div22{
                margin-left: 30px;margin-top: 30px;
            }
        </style>
        <div class="div1">
            <div class="div2 div22">
            </div>
        </div>
    </body>
</html>

2、方式2

  运用css的 position特性,把div2相对div1的top、left都设定为50%,随后再用margin-top设定为div2的高宽比的负1半拉回家,用marg-left设定为宽度的负1半拉回家,css以下设定

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF⑻">
        <title>demo</title>
    </head>
    <body>
        <style type="text/css">
            .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
            .div2{ width:40px ; height: 40px; background-color: green;}
            .div11{
                position: relative;
            }
            .div22{
                position: absolute;top:50%;left: 50%;margin-top: ⑵0px;margin-left: ⑵0px;
            }
        </style>
        <div class="div1 div11">
            <div class="div2 div22">
            </div>
        </div>
    </body>
</html>

3、方式3

  還是用css的position特性,以下的html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF⑻">
        <title>demo</title>
    </head>
    <body>
        <style type="text/css">
            .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
            .div2{ width:40px ; height: 40px; background-color: green;}
            .div11{
                position: relative;
            }
            .div22{
                position: absolute;margin:auto; top: 0;left: 0;right: 0;bottom: 0;
            }
        </style>
        <div class="div1 div11">
            <div class="div2 div22">
            </div>
        </div>
    </body>
</html>

4、方式4

  运用css3的新增特性table-cell

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF⑻">
        <title>demo</title>
    </head>
    <body>
        <style type="text/css">
            .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
            .div2{ width:40px ; height: 40px; background-color: green;}
            .div11{
                display: table-cell;vertical-align: middle;
            }
            .div22{
                margin: auto;
            }
        </style>
        <div class="div1 div11">
            <div class="div2 div22">
            </div>
        </div>
    </body>
</html>

这个方式也有1个益处便是,div2的高宽比能够不固定不动,以下
 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF⑻">
        <title>demo</title>
    </head>
    <body>
        <style type="text/css">
            .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
            .div2{ width:40px ; background-color: green;}
            .div11{
                display: table-cell;vertical-align: middle;
            }
            .div22{
                margin: auto;
            }
        </style>
        <div class="div1 div11">
            <div class="div2 div22">
               div垂直居中方式
            </div>
        </div>
    </body>
</html>

总结

以上所述是网编给大伙儿详细介绍的CSS完成DIV垂直居中的3种方式,期待对大伙儿有一定的协助,假如大伙儿有任何疑惑请给我留言,网编会立即回应大伙儿的。在此也十分谢谢大伙儿对脚本制作之家网站的适用!