A summary of how CSS elements are centered horizontally

Source: Internet
Author: User

In line: Text-align:center;

Fixed width block: margin:xpx auto;

Variable width BLOCK:

1.<table> <tbody><tr><td> and then table is the fixed-width block element

2.display:inline then treated as inline elements (there is a problem: Some features are missing, such as setting the length value)

3. By setting float to the parent element and then setting position:relative and left:50% for the parent element, the child elements are set position:relative and left:-50% to achieve horizontal centering.

First, the parent Div floats, and its left edge moves to a position relative to the body, and then the child Div floats, the initial position at its parent Div, and the distance to the left is equal to the parent container 50%, so it is just centered.

The float:left is to make the div no longer exclusive to a single line, but to fit the width of its content.

A summary of how CSS elements are centered horizontally

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.