前端网页学习6(css单行文字垂直居中)

css不提供单行文字垂直居中功能
但是可以用小技巧使它垂直居中。
让高度和文字行号相同。

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

居中,上,下

猜你喜欢

转载自blog.csdn.net/qq_40551957/article/details/113474894