Home >Web Front-end >HTML Tutorial >How to write center in html
In HTML, center alignment can be achieved using CSS styles: horizontal centering: use the text-align attribute (such as text-align: center;) vertical centering: use the vertical-align attribute (such as vertical-align: middle ;)
Centering in HTML
In HTML, we can control the centering of elements by using CSS styles Center aligned. There are two ways to achieve centering: Horizontal centering and Vertical centering.
Horizontal centering
You can use the text-align
attribute to center the element horizontally. The value of this attribute can be:
For example, to center a paragraph horizontally, you would use the following code:
<code class="html"><p style="text-align: center;">水平居中的段落</p></code>
Center vertically
To vertically center an element, you can use the vertical-align
attribute. The value of this attribute can be:
For example, to vertically center an image, you can use the following code:
<code class="html"><img src="image.jpg" style="vertical-align: middle;"></code>
You can also use the following CSS rules To center both horizontally and vertically:
<code class="css">.centered { text-align: center; vertical-align: middle; }</code>
Applying this class to an element will center it both horizontally and vertically.
The above is the detailed content of How to write center in html. For more information, please follow other related articles on the PHP Chinese website!