让div居中的3种方式

2020-11-26  本文已影响0人  Ray_afab
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="parent">
        <div class="child">

        </div>
    </div>
    ------
    <div class="parent1">
        <div class="child1">

        </div>
    </div>
    ------
    <div class="parent2">
        <div class="child2">

        </div>
    </div>
    <style>
        .parent {
            width: 500px;
            height: 500px;
            border: 1px solid red;
            position: relative;
        }

        .child {
            width: 400px;
            height: 400px;
            border: 1px solid green;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }

        .parent1 {
            width: 500px;
            height: 500px;
            border: 2px solid red;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .child1 {
            width: 400px;
            height: 400px;
            border: 2px solid green;
        }
        .parent2{
            width: 500px;
            height: 500px;
            border: 2px solid red;
            display: table-cell;
            vertical-align: middle;
            
        }
        .child2{
            width: 200px;
            height: 200px;
            border: 2px solid green;
            margin: auto;
        }
    </style>
</body>

</html>

这三种在工作中完全够用了 至于还有其他的 懒得了解了

上一篇 下一篇

猜你喜欢

热点阅读