How does CSS align the DIV Layer? _ CSS/HTML

Source: Internet
Author: User
How does CSS center the DIV layer?
The main style definition is as follows:

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, we recommend that you do not set it in a DIV. You can split multiple p in sequence, you only need to define MARGIN-RIGHT: auto; MARGIN-LEFT: auto; in each split p.

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_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"

The effect is as follows:

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:






Test content




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.

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.