Home  >  Article  >  Web Front-end  >  How to set text center alignment in css

How to set text center alignment in css

王林
王林Original
2021-03-05 16:04:2212704browse

How to set the center alignment of text in css: You can use the text-align attribute to achieve this, such as [p{text-align:center;}]. We can also set the text to the left or right, such as [text-align:left;] or [text-align:right;].

How to set text center alignment in css

The operating environment of this article: windows10 system, css 3, thinkpad t480 computer.

There is a text attribute text-align in css, which allows us to specify the horizontal alignment of the text of the element, such as center, left, right, aligned at both ends, etc.

Attribute value:

  • left Arrange the text to the left. Default: determined by the browser.

  • #right Arrange the text to the right.

  • #center Arrange the text to the center.

  • #justify Achieve the effect of aligning text on both ends.

  • #inherit Specifies that the value of the text-align attribute should be inherited from the parent element.​

For example, if we want to center align text, what should we do?

Specific implementation code:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php.cn</title> 
<style>
p{
    width:500px;
    border:2px solid red;
    text-align:center;
}
</style>
</head>

<body>
<h2>文本居中对齐</h2>
<p>居中的文本</p>
</body>
</html>

(Learning video sharing: css video tutorial)

Running results:

How to set text center alignment in css

Related recommendations: CSS tutorial

The above is the detailed content of How to set text center alignment in css. 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