Align Div Center

Source: Internet
Author: User

Use margin-left: auto; margin-Right: auto; to align your Div center.
. Style {margin-left: auto; margin-Right: auto ;}
Abbreviated form:
. Style {margin: 0 auto ;}
The value 0 indicates that the top and bottom margins are 0. You can set different values as needed.

View the following example:
Run codeNo coloring mode printing?

  1. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "">
  2. <HTML xmlns = "" lang = "ZH-CN">
  3. <Head>
  4. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  5. <Title> center Div demonstration effect </title>
  6. <Style type = "text/CSS">
  7. . Align-center {
  8. Margin: 0 auto;/* center. Other attributes are not required */
  9. Width: 500px;/* The center effect cannot be seen when the width is pushed to either side of the browser */
  10. Background: red;/* Background Color */
  11. Text-align: center;/* center text and other content */
  12. }
  13. </Style>
  14. </Head>
  15. <Body>
  16. <Div class = "align-center"> center Div demo </div>
  17. </Body>
  18. </Html>

Note: The above sentence must be added.

To take effect. If you do not want this sentence, you can add an attribute to the body:
Body {text-align: center ;}

In addition, the code that centers the content (including text and images) in the DIV is text-align: center;

For further explanation, see:

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.