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.