About using CSS center (including horizontal/vertical)

Source: Internet
Author: User

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"

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.