This is a centered text.
```If you want the text to be vertically centered, you can use the line-height attribute and the height attribute."/> This is a centered text. ```If you want the text to be vertically centered, you can use the line-height attribute and the height attribute.">Home >Web Front-end >Front-end Q&A >html center setting
HTML center setting
In web design, it is a very common requirement to center text, pictures and other elements. The following will introduce several HTML centering settings.
1. Text centering
You can use the text-align attribute to achieve the effect of horizontal centering of text, as shown in the following code:
<p style="text-align:center;">这是一段居中的文本。</p>
If you want the text to be vertically centered, you can Use the line-height attribute and height attribute, as shown in the following code:
<style> .container { height: 300px; line-height: 300px; text-align: center; } </style> <div class="container">这是一段居中的文本。</div>
2. Image centering
You can use the margin attribute in CSS to achieve the effect of horizontally centering the image, as shown in the following code :
<style> .container { text-align: center; } img { margin: 0 auto; } </style> <div class="container"> <img src="image.jpg" alt="图片"> </div>
If you want the image to be vertically centered, you can use the position attribute and top attribute, as shown in the following code:
<style> .container { position: relative; height: 400px; } img { position: absolute; top: 50%; transform: translateY(-50%); } </style> <div class="container"> <img src="image.jpg" alt="图片"> </div>
3. The container is centered
If you want the entire container to be centered For centering, you can use the display attribute and margin attribute in CSS, as shown in the following code:
<style> .container { display: flex; justify-content: center; align-items: center; height: 400px; } </style> <div class="container"> <p>这是一个居中的容器。</p> </div>
The above are several commonly used HTML centering settings, which can be used flexibly according to needs.
The above is the detailed content of html center setting. For more information, please follow other related articles on the PHP Chinese website!