How to align the DIV layer horizontally with CSS
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" "[/code] 'target = _ blank> 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 (/style/001/logo_w3cn_194x79.gif) # FFF 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"