Code for horizontal center of div in css

Source: Internet
Author: User

The principle is simple: Set left and top to 50% to locate the div in the center of the browser, and then set the margin-left and margin-top values to half width and height.

To center the div.

Add this:

The code is as follows: Copy code


1 text-align: center;

2 margin-left: auto;

3 margin-right: auto;

Or:


1 text-align: center;

2 margin: 0 auto;

Both of them have passed tests in Chrome, FireFox, IE7, 8, and 9. Note that the document format in IE cannot be "Quirks ".

Example 1

The code is as follows: Copy code

<Style>
# Warp {
Position: absolute;
Width: 500px;
Height: 200px;
Left: 50%;
Top: 50%;
Margin-left:-250px;
Margin-top:-100px;
Border: solid 3px red;
}
</Style>
<Body>
<Div id = warp> Test </div>
</Body>

Related Article

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.