How does CSS align the DIV layer horizontally?

Source: Internet
Author: User
These two days began to make drastic changes to the website.
Today, I encountered a very difficult problem. Div itself does not define its own center attribute,
Many methods on the Internet are described using the upper-level text-align: center and then embedding a layer Div to solve the problem.
But is this method actually scientific?
The following conclusions are obtained through network search and hands-on experiments:
The correct setting does not affect the page structure as follows:
Add the following attributes to the DIV layer to be horizontally centered:

Margin-left: auto;
Margin-Right: auto;

After such a setup, it seems that the problem has been solved. FF has already been centered, but it is still not centered in IE!
After being depressed for one afternoon, I couldn't find the problem. I also compared the articles on the Internet.
Where is the problem?
Thanks to Lotte for helping us find out the cause of this evil problem.
It turns out that l-blog does not add DTD before HTML by default, So Ie will explain the document in HTML instead of XHTML.
The problem lies not in CSS but in the XHTML web page itself.
This code must be added to make the above settings effective:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> [/Code]

If you want more strict XHTML 1.0 strict or XHTML 1.1, please refer to the relevant documentation.
The above tests are based on Windows XP SP2 IE6 and Firefox 1.0 Final version.

How to center a div

The main style definition is as follows:

[Code] body {text-align: center ;}
# Center {margin-Right: auto; margin-left: auto ;}

 

Note:
First, define text-align: center in the parent element. This means that the content in the parent element is centered. for IE, this setting is enough. However, it cannot be centered in Mozilla. The solution is to add "margin-Right: auto; margin-left: auto;" when the sub-element is defined ;"
It should be noted that, if you want to use this method to center the entire page, it is recommended that you do not set it in one Div. You can split multiple divs in sequence, only
Define margin-Right: auto; margin-left: auto; in each split Div.
How to vertically center an image in a div

Use the background method. Example:

Body {Background: URL (http://www.w3cn.org/style/001/logo_w3cn_194x... nbsp; no-repeat center ;}

 

The key is the final center. This parameter defines the image position. It can also be written as "top left" (upper left corner) or "bottom right", or "50 30"

How to vertically center text in a div

If it is text, you cannot use the background method. You can use the method of increasing the line spacing to change the vertical center. The complete code is as follows:
 

<HTML>
<Head>
<Style>
Body {text-align: center ;}
# Center {margin-Right: auto;
Margin-left: auto;
Height: 200px;
Background: # f00;
Width: 400px;
Vertical-align: middle;
Line-Height: 200px;
}
</Style>
</Head>
<Body>
<Div id = "center"> <p> test content </P> </div>
</Body>
</Html>
 

Note:
Vertical-align: middle; indicates vertical center in the row. We will increase the line spacing to the same height as the entire Div line-Height: 200px; then insert the text to the vertical center.
CSS + Div control page element vertical center code global and region vertical center

<Style type = "text/CSS" Media = screen>
Body
{
Text-align: center;
}
#
{
Width: 200px;
Height: 400px;
Background: #000;
}
# B
{
Margin-top: expression (A. clientheight-50)/2 );
Width: 50px;
Height: 50px;
Background: # FFF;
}
# C
{
Position: absolute;
Left: expression (body. clientwidth-50)/2 );
Top: expression (body. clientheight-50)/2 );
Width: 50px;
Height: 50px;
Background: # f00;
}
</Style>
<Div id = "A">
<Div id = "B"> </div>
</Div>
<Div id = "C"> </div>

Another method:

<Div style = "Background: Blue; position: absolute; left: expression (body. clientwidth-50)/2); top: expression (body. clientheight-50)/2); width: 50; Height: 50 "> </div>
 

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.