Home >Web Front-end >Front-end Q&A >How to vertically center css text

How to vertically center css text

藏色散人
藏色散人Original
2021-04-29 09:15:5048036browse

How to vertically center text in css: 1. Use the line-height attribute to vertically center text; 2. Format external blocks into table cells; 3. Vertically center text through CSS3 flex layout.

How to vertically center css text

The operating environment of this article: Windows7 system, HTML5&&CSS3 version, Dell G3 computer.

Method:

Method 1: Use the line-height property to vertically center the text

The line-height property sets the distance between lines (line height ); negative values ​​are not allowed for this attribute.

The line-height property affects the layout of the line box. When applied to a block-level element, it defines the minimum distance between baselines in that element rather than the maximum distance.

The calculated difference between line-height and font-size (known as "line spacing" in CSS) is divided into two halves and added to the top and bottom of a line of text content. The smallest box that can contain this content is a line box.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>css 垂直居中</title>
        <style>
            .box{
                width: 300px;
                height: 300px;
                background: #ddd;
                line-height:300px;
            }
        </style>
    </head>
    <body>
        <div class="box">css 垂直居中了--文本文字</div>
    </body>
</html>

Rendering:

How to vertically center css text

Method 2: Format outer block into table cell

The content of table cell can be vertical Center, format the outer block as a table cell to vertically center the text.

Example: Placing a paragraph inside a block with a specific given height

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>css 垂直居中</title>
        <style>
            .box{
                width: 400px;
                height: 200px;
                background: #ddd;
display: table-cell;
vertical-align: middle;
            }
        </style>
    </head>
    <body>
        <div class="box">css 垂直居中了--文本文字</div>
    </body>
</html>

Rendering:

How to vertically center css text

Method 3: Using CSS3 The flex layout makes the text vertically centered

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>css 垂直居中</title>
        <style>
            .box{
                width: 300px;
                height: 200px;
                background: #ddd;
                 /*设置为伸缩容器*/
                display: -webkit-box;
                display: -moz-box;
                display: -ms-flexbox;
                display: -webkit-flex;
                display: flex;
                /*垂直居中*/
                -webkit-box-align: center;/*旧版本*/
                -moz-box-align: center;/*旧版本*/
                -ms-flex-align: center;/*混合版本*/
                -webkit-align-items: center;/*新版本*/
                align-items: center;/*新版本*/
            }
        </style>
    </head>
    <body>
        <div class="box">css 垂直居中--文本文字(弹性布局)</div>
    </body>
</html>

Rendering:

How to vertically center css text

For more detailed HTML/CSS knowledge, please visit CSS Video TutorialColumn!

The above is the detailed content of How to vertically center css text. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn