A summary of the absolute center problem when CSS is used to create a webpage!

Source: Internet
Author: User
Summary of the absolute center problem during CSS webpage creation [Source: webpage tutorial Network | Author: | time: 10:22:50 | Add this article to favorites] [large, medium, and small]

What is absolute center? That is to say, the container has spacing between the left and right sides of the screen, and remains unchanged with the screen size scaling or proportional adjustment, always in the middle of the screen. Absolute center also has two conditions: absolute container size and relative size. At the same time, there are many methods that can be implemented, which are summarized as follows:

Station. Long. Station

 

1. Using the table feature, when width and height all set 100%, the container can be nested in TD to form an absolute center. In this case, the nested container can be an absolute or relative size. (Non-Standard) do not add a statement! Chinese Webmaster. Station

If you want to add a style body in standard mode, HTML {Height: 100%; padding: 0; margin: 0;} Chinese site. Long SITE

Reference content is as follows:

<Table width = "100%" Height = "100%" border = "0" align = "center" cellpadding = "0" cellspacing = "0">
<Tr>
<TD style = "text-align: center;">
<Table width = "200" Height = "50" border = "0" cellpadding = "0" cellspacing = "0" bgcolor = "# ef1122">
<Tr>
<TD> </TD>
</Tr>
</Table>
</TD>
</Tr>
</Table> Webmaster. Station

Chinese webmaster Station

 

2. Use the negative margin method to absolutely locate and center the container relative to the screen. At this time, the nested container can only be an absolute size. (Standard) according to the proportional control of the negative value of margin and top left, the parameter of margin top left needs to be adjusted again when the screen is absolutely centered and the container size changes:Www.chinaz.com

Reference content is as follows:

<Div style = "Background: # f00; width: 740px; Height: 340px; left: 50%; margin:-170px 0 0-370px; position: absolute; top: 50%; ">
</Div>Chinaz.com

Chinaz ^ com

 

3. The display: inline-block; and ie6.0 were used to pass the test. (Standard)

China site. Long SITE

 

Note 1. Height: 100% is the key: 2. Edge has no nested relationship with container: site. Long SITE

This is a bug in IE. I have limited understanding of display: inline-block. Edge can be seen as a fill. container is now an object in the Upper and Lower center. You can see more clearly with the background color. Of course, you can also add any absolute or relative size container in the container. However, the problem is that span is only an inline element, and adding block-level elements internally is not a standard logic.Station. Long. Station

Reference content is as follows:
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "<a href =" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "target =" _ blank "> http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd </a>">
<HTML xmlns = "<a href =" http://www.w3.org/1999/xhtml "target =" _ blank "> http://www.w3.org/1999/xhtml </a>">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/> <br>
<Title> untitled document </title>

Chinese Webmaster. Station

<Style type = "text/CSS">
<! -- Body {margin: 0; Height: 100% ;}
# Edge {width: 0; Height: 100%; display: inline-block; Vertical-align: middle ;}# container {text-align: center; width: 100%; display: inline-block; Vertical-align: middle;} -->
</Style>
</Head>
<Body>
<! -- Required for xhtml1.1 validation only -->
<Span id = "edge"> </span> <span id = "Container">
<Div style = "width: 200px; Height: 50px; Background: # f00; line-Height: 50px;"> only implemented in ie6.0 environment </div>
</Span>
</Body>
</Html>

China webmaster site

4. CSS behavior expression control implementation. However, expression is unique to IE and consumes a lot of resources, especially when it is used heavily. (Standard)

Www_chinaz_com

 

Note the key definition. Do not think that height: 100% is useless in IE:

Chinese webmaster _ station, providing motivation for Chinese Websites

 

Obtain the screen height and width values in real time, subtract the container height and width values, and divide them by 2. Obtain the exact coordinates for absolute positioning: China.

reference content is as follows:
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
http://www.w3.org/1999/xhtml ">
none Title document





5. The ff1.5 test is passed. The absolute position and margin are proportional. At this time, the container height and width are always 50% relative sizes of the viewport (standard ):

China site. Long SITE

 

Reference content is as follows:

<Div style = "position: absolute; top: 0; Right: 0; bottom: 0; left: 0; width: 50%; Height: 50%; margin: auto; Background: # f00; color: White; line-Height: 20px; text-align: center; "> ff1.5 pass test </div>

Chinese webmaster _ station, providing motivation for Chinese Websites

 

Webmaster. Station

6. ff1.5 test passed, absolute positioning, direct positioning of up to bottom left and right, force edge bureaus to achieve the relative container size (standard ):[Chinese webmaster site]

Code: OmittedChinaz.com

7. ff1.5 ie5 IE6 passed the test on the Chinese site. Long. Station

Reference content is as follows:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 strict // en" "// www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">
<HTML xmlns = "// www.w3.org/5o/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> vertical centering in valid CSS </title>
<Style type = "text/CSS">
Body {padding: 0; margin: 0; font-size: 75%; line-Height: 140%; font-family: Arial, Helvetica, sans-serif ;}
Body, HTML {Height: 100% ;}
A {color: #333 ;}
A: hover {color: green ;}

Chinese webmaster _ station, providing motivation for Chinese Websites

# Outer {Height: 100%; overflow: hidden; position: relative; width: 100%; Background: ivory ;}
# Outer [ID] {display: Table; position: static ;}
# Middle {position: absolute; top: 50%; text-align: center;}/* For explorer only */
# Middle [ID] {display: Table-cell; Vertical-align: middle; position: static ;}
# Inner {position: relative; top:-50%; width: 600px; margin: 0 auto; text-align: Left;}/* For explorer only */
Div. greenborder {border: 1px solid green; Background-color: # FFF ;}
P {margin: 1em ;}
</Style>
<SCRIPT type = "text/JavaScript">
// <! [CDATA [
Function togglecontent (name, n ){
VaR I, T = '', El = Document. getelementbyid (name );
If (! El. origcont) El. origcont = El. innerhtml;

For (I = 0; I <n; I ++) T + = El. origcont;
El. innerhtml = T;
}
//]>
</SCRIPT>
</Head>
<Body>
<Div id = "outer">
<Div id = "Middle"> Chinese webmaster _ station, providing motivation for Chinese Websites
<Div id = "inner" class = "greenborder">
<P> <a href = "javascript: togglecontent ('inner ', 1)"> default length </a> <a href = "javascript: togglecontent ('inner ', 2) "> extend the page </a> </P>
<P> 1. Open illustrator, create a new file, and draw a rectangle, which is larger than the image to be imported and filled with white. <Br/>
2. Select a rectangle and choose Effect> distort & transform> zig zag from the menu, as shown in. <Br/>
3. Menu: Effect> stylize> drop shadow, as shown in. <Br/>
1. Open illustrator, create a new file, and draw a rectangle, which is larger than the image you want to import and is filled in white. <Br/>
2. Select a rectangle and choose Effect> distort & transform> zig zag from the menu, as shown in. <Br/>
3. Menu: Effect> stylize> drop shadow, as shown in. </P> www_chinaz_com
<Address style = "text-align: center; padding:. 5em; clear: Left;">
Design by <a href = "// www.webjx.com"> webjx </a> This demonstration uses <a href = "// www.creativecommons.cn/"> Create shared authorization </a> -- Signature and non-commercial use.
</Address>
</Div>
</Div>
</Div>
<SCRIPT src = "// www.google-analytics.com/urchin.js" type = "text/JavaScript">
</SCRIPT>
<SCRIPT type = "text/JavaScript">
_ Uacct = "UA-152060-1 ";
Urchintracker ();
</SCRIPT>
<NoScript> <p> Google-analytics </P> </NoScript>
<SCRIPT src = "// www.webjx.com/#/developer.js" type = "text/JavaScript"> </SCRIPT>
<NoScript> <p> Stat. </P> </NoScript>
</Body>
</Html> WWW @ chinaz @ com

 

China site. Long SITE

8. Use expression (for IE only)

Webmaster. Station

 

the reference content is as follows:

chinaz.com

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.