How CSS makes the div layer centered _css/html

Source: Internet
Author: User

How do I make a div centered
The main style definitions are as follows:

body {text-align:center;}
#center {Margin-right:auto; Margin-left:auto; }
Description

First, the parent element defines text-align:center, which means that the content within the parent element is centered, and for IE this setting is OK. But you can't center in Mozilla. The solution is to add "Margin-right:auto" when the child element definition is set; Margin-left:auto; ”

What needs to be explained is that if you want to use this method to make the entire page centered, it is recommended not to be nested in a div, you can split out a number of Div, as long as in each split div defined margin-right:auto; Margin-left:auto; It's OK.

How do I get a picture centered vertically 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, which defines the location of the picture. It can also be written as "top left" or "bottom right", or you can write the value "50 30" directly.

The effect is as follows:

How do I align text vertically in a div
If it is text, you cannot use the background method, you can use to increase the line spacing to achieve vertical center, complete code as follows:

<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>
<body>
<div id= "center" ><p>test content</p></div>
</body>
Description

Vertical-align:middle indicates that the line is centered vertically, we increase the spacing to be as high as the entire Div line-height:200px, and then insert the text and center it vertically.

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.