How to write the code to center text in html? The following article will introduce it to you. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
The text is horizontally centered
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>水平居中</title> <style> .box { width: 300px; height: 200px; background: palegoldenrod; text-align:center; } </style> </head> <body> <div class="box">水平居中了--文本文字</div> </body> </html>
Rendering:
Instructions : text-align
text-align is a basic attribute that affects how lines of text in an element are aligned with each other. The values left, right, and center will cause the text in the element to be left-aligned, right-aligned, and centered respectively. If you want to center the text, just use center.
This attribute sets the centering of some inline objects (or similar elements) such as text and img tags.
The text is vertically centered
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>垂直居中</title> <style> .box { width: 300px; height: 300px; background: paleturquoise; line-height:300px; } </style> </head> <body> <div class="box">垂直居中了--文本文字</div> </body> </html>
Rendering:
The line-height property sets the distance between lines ( row height). Negative values are not allowed.
This 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.
Recommended tutorial: html tutorial
The above is the detailed content of How to write text centering code in html?. For more information, please follow other related articles on the PHP Chinese website!