A summary of several page elements centering schemes

Source: Internet
Author: User
Web page first to see me a simple xhtml/html file code (part), our goal is to let #container horizontally centered.

<body>
<div id= "Container" >

<p>lorem ipsum dolor sit amet, consectetuer adipiscing. Phasellus varius eleifend.</p>
</div>
</body>

Using adaptive boundaries (auto margin)

The preferred method of horizontally centering any element is to use the boundary (margin) property and set the left and right values to auto. But you have to specify a width for #container.

Div#container {
Margin-left:auto;
Margin-right:auto;
width:168px;
}

This scheme works in any contemporary browser, even if it is IE6, provided that it is in Web Standard compatibility mode (compliance mode). Unfortunately, it does not work in previous versions of Ie/win. Let's make a table for this:
Browse Adaptive Boundary Support List Browser version support
Internet Explorer 6.0, Compliance mode is
Internet Explorer 6.0, quirks mode no
Internet Explorer 5.5 Windows No
Internet Explorer 5.0 Windows No
Internet Explorer 5.2 Macintosh is
All current versions of Mozilla are
Mozilla Firefox all versions are
Netscape 4.x No
Netscape 6.x+ is
Opera 6.0, 7.0 Macintosh and Windows are
Safari 1.2 is

Despite the limitations of browser support, most designers advocate that you do as much as you can. But we can still use CSS to handle everything.


Use text arrangement (text-align)

This scheme needs to use the Text-align property, apply to the BODY element and give the value of center.

Body {
Text-align:center;
}

It treats all kinds of browsers fairly, completely, and at their fingertips. However, this is given the nature of the text, which makes the text in the #container also centered. So, we have to do some extra work on the layout:

Div#container {
Text-align:left;
}

This allows you to return the text alignment to the default state.

Integrated borders and text alignment

Because the text is backward-compatible and contemporary browsers support adaptive boundaries, many designers combine them to achieve cross-browser use.

Body {
Text-align:center;
}
#container {
Margin-left:auto;
Margin-right:auto;
border:1px solid red;
width:168px;
Text-align:left
}

Alas, still imperfect, because it is still a hacker skill (hack). You have to write down the extra rules for the text arrangement. But now, we can use a more perfect cross-browser scenario.

Negative Boundary Solutions

This scheme has to be combined using absolute positioning (absolute positioning). First of all, put the #container absolute positioning and left offset 50%, so that #container的左边界就是页面分辨率的一半. Next, set the left edge of the #container to a negative value, which is half the width of #container.

#container {
Background: #ffc URL (mid.jpg) repeat-y Center;
Position:absolute;
left:50%;
width:760px;
Margin-left: -380px;
}

Look, there is no hacker skill (no hacks)! Even Netscape 4.x support!

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.