How does CSS align the DIV layer horizontally? Today, CSS is a very tricky problem. Div itself does not define its own center attribute. Many Methods on the Internet are to use the upper-level text-align: center then nest a div layer to solve the problem. but is this method actually scientific? The following conclusions are obtained through network search and hands-on experiments: Correct settings do not affect the Page Structure: Add the following attributes to the DIV layer that needs 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 online articles exactly. What 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. you need to add this code 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>
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: body {text-align: center ;}# center {margin-Right: auto; margin-left: auto;} description: first, define text-align in the parent element:
Center; 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; "it should be noted that if you want to use this method to center the entire page, it is recommended not to enclose it in a div, you can split multiple divs one by one, as long as you define margin-Right: auto; margin-left: auto; in each split Div.
How to align the image vertically in the DIV using 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"
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 implement vertical center. The complete code is as follows: <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 center the text vertically.
CSS + Div control page element vertical center code global and region vertical center
<Style type = "text/CSS" Media = screen> body {text-align: center; }# A {width: 200px; Height: 400px; Background: #000 ;} # B {margin-top: expression (. 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 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>