Front-end web page learning 6 (css single line text is vertically centered)

CSS does not provide vertical centering of single-line text,
but you can use small tricks to center it vertically.
Make the height the same as the text line number.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .div1{
    
    
            height: 40px;
            line-height: 40px;
            background-color: red;
            /* 相同居中 */
        }
        .div2{
    
    
            height: 40px;
            line-height: 30px; 
            background-color: red;
            /* 小于居上 */
        }
        .div3{
    
    
            height: 40px;
            line-height: 50px;
            background-color: red;
            /* 大于居下 */
        }
    </style>
</head>
<body>
    <div class="div1">1</div>
    <br/>
    <div class="div2">2</div>
    <br/>
    <div class="div3">3</div>
</body>
</html>

Center, top, bottom

Guess you like

Origin blog.csdn.net/qq_40551957/article/details/113474894