HTMLでボックスを水平方向と垂直方向の中央に配置する方法

方法 1: 位置決めを使用する (一般的な方法、推奨)

    <style>
        div {
            width: 100px;
            height: 100px;
            border: 1px solid #999;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -50px;
            margin-left: -50px;
            
        }
    </style>

方法 2: margin:auto を使用します。

  div {
            width: 100px;
            height: 100px;
            border: 1px solid #999;
            position: absolute;
            margin: auto;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;


        }

方法 3: display:table-cell を使用する

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .parent {
            width: 500px;
            height: 500px;
            border: 1px solid #000;
            display: table-cell;
            vertical-align: middle;
            text-align: center;//文字居中
        }

        .child {
            width: 100px;
            height: 100px;
            line-height: 100px; //给line-height让文字居中
            border: 1px solid #999;
            display: inline-block;
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="child">我是子元素</div>
    </div>
</b

方法 4: display: flex; を使用して垂直方向と水平方向の両方のセンタリングを設定する

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .parent {
            width: 500px;
            height: 500px;
            border: 1px solid #000;
            display: flex;
            justify-content: center;
            align-items: center;
            text-align: center;
        }

        .child {
            width: 100px;
            height: 100px;
            line-height: 100px;
            border: 1px solid #999;

        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="child">我是子元素</div>
    </div>
</body>


</html>

方法 5: 親ボックスと子ボックスの間の空間距離を計算します (これは方法 1 と同じですが、高さと幅を知る必要があります)

       .parent {
            width: 500px;
            height: 500px;
            border: 1px solid #000;
        }

        .child {
            width: 100px;
            height: 100px;
            border: 1px solid #999;
            margin-top: 200px;
            margin-left: 200px;


        }

方法 6: 変換を使用する (原理は方法 1 と同じですが、違いはボックスの高さと幅を知る必要がないことです)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .parent {
            width: 500px;
            height: 500px;
            border: 2px solid #000;
            position: relative;

        }

        .child {
            width: 100px;
            height: 100px;
            border: 2px solid palevioletred;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="child">我是子元素</div>
    </div>
</body>


</html>

 

 

おすすめ

転載: blog.csdn.net/H_hl2021/article/details/121898435