cssテキストは水平方向と垂直方向の中央に配置されます(親要素divの幅と高さは既知で、幅と高さは不明です)

1.div中国語のテキストは水平方向と垂直方向の中央に配置されます

1.親要素の幅と高さを知る

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平垂直居中</title>
    <style>
        body {
            width: 500px;
            height: 500px;
        }
        .f1 {
            width: 100px;
            height: 100px;
            background: pink;
            text-align: center;
            line-height: 100px;
        }
    </style>
</head>
<body>
    <div class="f1">
        我是文字
    </div>
</body>
</html>

 

2.不明な親要素の幅と高さ

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平垂直居中</title>
    <style>
        body {
            width: 500px;
            height: 500px;
        }
        .f1 {
            display: flex;  /*flex 布局*/
            width: 50%;
            height: 50%;
            background: pink;
            align-items: center; /*实现垂直居中*/
            justify-content: center; /*实现水平居中*/
        }
    </style>
</head>
<body>
    <div class="f1">
        我是文字
    </div>
</body>
</html>

 

2、css子要素div(既知の幅と高さ、未知の幅と高さ)は、親要素divを垂直方向に中央揃えします

https://blog.csdn.net/qq_40015157/article/details/113845120

 

おすすめ

転載: blog.csdn.net/qq_40015157/article/details/113844403