css text is centered horizontally and vertically (the parent element div has known width and height and unknown width and height)

1. div Chinese text is centered horizontally and vertically

1. Know the width and height of the parent element

<!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. The width and height of the unknown parent element

<!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>

 

Two, css child element div (known width and height, unknown width and height) vertically center the parent element div

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

 

Guess you like

Origin blog.csdn.net/qq_40015157/article/details/113844403