Home >Web Front-end >CSS Tutorial >Using CSS to achieve horizontal centering of DIV
How to set the centering of a div using css: first set "text-align:center" on the body; then set the css style to "margin:0 auto" for the div box that needs to be centered to center the corresponding div horizontally .
#How to use CSS to center a DIV? I have made an easy-to-understand code for everyone, using CSS to realize the horizontal center display of DIV. Friends in need can refer to it.
There are two main css codes required, one is text-align:center (content is centered), the other is margin:0 auto; the other two Styles need to be used together to achieve centered display layout of the div box.
First we set text-align:center on the body, and then set the css style margin:0 auto on the div box that needs to be centered, so that the corresponding div can be horizontally centered.
div centering code application, in order to observe the div centering effect, we set a div named ".div" for the div, use class="div" in the div tag in the html, and set its width to 400px; height to 100px with red border. So that we can observe the effect.
1. Complete html+css code
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style> body{ text-align:center} .div{ margin:0 auto; width:400px; height:100px; border:1px solid #F00} /* css注释:为了观察效果设置宽度 边框 高度等样式 */ </style> </head> <body> <div class="div"> DIVCSS5实例 </div> </body> </html>
2. Screenshot of div+css centering example
div css screenshot to achieve centering effect
div to achieve centering effect Screenshot
3. Application features of div centering code
This centering method is to make the div centering effect perfectly compatible with all major platforms and browsers, no matter how high or low Both versions of IE and higher versions of IE are compatible.
This is how to use CSS to achieve horizontal centering of DIV.
Related reading:
The above is the detailed content of Using CSS to achieve horizontal centering of DIV. For more information, please follow other related articles on the PHP Chinese website!