Using CSS to achieve horizontal centering of DIV

php中世界最好的语言
Release: 2020-09-05 11:36:05
Original
24135 people have browsed it

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 .

Using CSS to achieve horizontal centering of DIV

#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

      
DIVCSS5实例
Copy after login

2. Screenshot of div+css centering example

div css screenshot to achieve centering effect

div to achieve centering effect Screenshot

Using CSS to achieve horizontal centering of DIV

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:

How to convert html to txt file


How to convert table and td Set the background to realize the table border


How to skillfully set the table border for table and td

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!