CSS align DIV horizontally and vertically (compatible with IE/FF/google browsers)

Source: Internet
Author: User

Instance

The code is as follows: Copy code

<Style type = "text/css">
Body {margin: 0; padding: 0; background: # 1d1d1d; font-size: 10px; font-family: Verdana, Arial, Helvetica, sans-serif ;}
H1 {font: 4em Georgia, "Times New Roman", Times, serif; color: # fff; border-bottom: 5px dotted #999; margin: 0; padding: 0 0 10px ;}
H1 span {font-weight: bold; display: block; font-size: 1.5em; color: # fff000 ;}
. Vert {width: 580px; height: Pixel px; position: absolute; top: 50%; left: 50%; margin:-95px 0 0-290px ;}
</Style>

Html code


<Div class = "vert">
<H1> www.111cn.net <span> CSS vertical center DIV </span> </Div>

Example 2

<Html>
<Head>
<Style>
Body {TEXT-ALIGN: center ;}
# Center {MARGIN-RIGHT: auto;
MARGIN-LEFT: auto;
Height: 200px;
Background: # F00;
Width: 400px;
Vertical-align: middle;
Line-height: 200px;
}
</Style>
</Head>
<Body>
<Div id = "center"> <p> test content </p> </div>
</Body>
</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 the vertical center.


Another solution

The following shows the style for the div to display in the middle of the browser:

The code is as follows: Copy code

<Style type = "text/css">
# Centerdiv {
Position: absolute; <! -- This property sets the div to be displayed at an absolute position -->
Top: 50%; <! -- Display at the position 50% from the top -->
Left: 50%; <! -- Display at 50% left -->
Margin:-150px 0 0-100px; <! -- This setting may not seem quite clear at the beginning. I will explain it in detail below -->
Width: 300px;
Height: 200px;
Background: black;

</Style>

I believe that the last thing you do not understand is that when you do not set this property in the property margin:-150px 0 0-100, the x coordinate of this div is 50% of the content display part, the y coordinate is 50% of the content display. However, in this case, we can only say that the starting point of the div is in the center. What we need is to display the entire div in the center, therefore, to margin-left:-150px; (here, 150 is obtained from the width attribute and it is equal to half of width, the corresponding margin-top method is also obtained through the sample method .) In this way, the width of the entire div is divided into half to the left to center horizontally. In the same way, set margin-top:-100px, the height of the entire div is displayed in half. The entire div is displayed in the middle of the browser.

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.