Home > Article > Web Front-end > How to set html font centering
How to set HTML font centering
In HTML, for the sake of beauty and reading comfort, it is often necessary to center-align the text on the page. This article will show you how to use CSS to center HTML text.
Step one: Select the element that needs to be centered
First you need to select the element that needs to be centered. HTML text centering can be divided into two categories: text centering within a single tag and text centering on the entire page.
Center the text within a single label:
If you only need to center the text within a certain label, you can use the CSS property text-align:center to set it. For example:
<p style="text-align:center;">这是中间对齐的文字</p>
Center the text of the entire page:
If you need the text of the entire page to be centered, you need to set it for the body element. For example:
<style> body { text-align: center; } </style> <body> <h1>这是标题</h1> <p>这是一段正文文字</p> <ul> <li>这是列表项目1</li> <li>这是列表项目2</li> </ul> </body>
Step 2: Select the centering method
After selecting the element that needs to be centered, you need to choose the appropriate centering method. Common centering methods include horizontal centering and vertical centering.
Horizontal centering:
In CSS, you can use the margin property to perform horizontal centering. The specific implementation method is to set the left and right margins of the element to auto, as follows:
<style> .content { margin: 0 auto; text-align: center; } </style> <div class="content"> <h1>这是标题</h1> <p>这是一段正文文字</p> </div>
The .content element here is a div element, and setting its left and right margin properties to auto can achieve horizontal centering.
Vertical centering:
In CSS, vertical centering is more complicated and requires the use of attributes such as Flexbox and table-cell. Here is an introduction to how to use Flexbox to achieve simultaneous vertical and horizontal centering. The specific implementation method is to set the parent element as a flex container, and set its child elements as flex items, and use the align-items and justify-content attributes for center alignment, as follows:
<style> .container { display: flex; align-items: center; justify-content: center; min-height: 100vh; } .content { text-align: center; } </style> <div class="container"> <div class="content"> <h1>这是标题</h1> <p>这是一段正文文字</p> </div> </div>
The .container here is the parent Element, use the display: flex attribute, set its child element .content as a flex item, and use the align-items and justify-content attributes to achieve vertical and horizontal centering at the same time.
Summary:
You can easily center HTML text using CSS. You need to select the elements that need to be centered and the appropriate centering method. Horizontal centering generally uses the margin attribute, while vertical centering requires the use of attributes such as Flexbox and table-cell.
The above is the detailed content of How to set html font centering. For more information, please follow other related articles on the PHP Chinese website!