Home >Web Front-end >CSS Tutorial >How to set horizontal alignment in css
How to set horizontal alignment in css: 1. Use the "text-align: center;" style to set the horizontal center alignment of text elements; 2. Use the "margin: auto;" style to set the horizontal center alignment of block elements.
The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.
1. Use the text-align: center;
text-align attribute to specify the horizontal alignment of the element text.
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.
Example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> .center { text-align: center; border: 3px solid green; } </style> </head> <body> <h2>文本居中对齐</h2> <div class="center"> <p>文本居中对齐。</p> </div> </body> </html>
Rendering:
CSS video tutorial】
2. Use margin: auto;
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> .center { margin: auto; width: 60%; border: 3px solid #73AD21; padding: 10px; } </style> </head> <body> <h2>元素居中对齐</h2> <div class="center"> <p>div 元素是居中的</p> </div> </body> </html>Rendering:
For more programming related knowledge, please visit:
Programming Video! !
The above is the detailed content of How to set horizontal alignment in css. For more information, please follow other related articles on the PHP Chinese website!