Home>Article>Web Front-end> How to vertically center css text

How to vertically center css text

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

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.

    css 垂直居中  
css 垂直居中了--文本文字

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

    css 垂直居中  
css 垂直居中了--文本文字

Rendering:

How to vertically center css text

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

    css 垂直居中  
css 垂直居中--文本文字(弹性布局)

Rendering:

How to vertically center css text

For more detailed HTML/CSS knowledge, please visitCSS 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