CSS Div+css Layout Center

Source: Internet
Author: User
First, using automatic margin to achieve the center

The preferred way to center elements horizontally in CSS is to use the margin property-set the Margin-left and Margin-right properties of the element to auto. In practical use, we can create a container-scoped div for these elements that need to be centered. It is important to note that you must specify a width for the container:

DIV+CSS Layout Center

Div#container {

Margin-left:auto;

Margin-right:auto;

width:168px;

height:80px;

}



Second, the use of text-align to achieve the center

Another way to center the element is to use the Text-align property, set the property value to center, and apply it to the BODY element. This approach is downright hack, but it is compatible with most browsers, so it is naturally necessary in some cases.

It is hack because this method does not apply the text attribute to the text, but instead applies it to the element that is the container. This also brings us extra work. After creating the div necessary for the layout, we will apply the Text-align property to the body as follows:

DIV+CSS Layout Center

body{

Text-align:center;

}

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.