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 istext-align:center (content is centered), the other ismargin: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
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!