css 文字水平垂直居中(父元素div已知宽高和未知宽高)

一、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>

二、css 子元素div(已知宽高、未知宽高)垂直居中父元素div

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

猜你喜欢

转载自blog.csdn.net/qq_40015157/article/details/113844403