CSS Common centering method

Source: Internet
Author: User
In the CSS layout, the center occupies a very important role, if not good use of the center, it will cause the entire page confusion.

In the actual page layout, there are many ways to center, there are many simple and practical methods, there are honest methods. Just started to write the page, I prefer to use the honest way to write, because it is not wrong, but we should also grasp the common center method.

Common Centering Method: Text-align:center

Centers inline text, or inline elements, horizontally in the parent container.

Vertical-ailgn:middle

Vertical centering of inline text, inline elements, with Display:table,display:table-cell, works wonders.

Line-height

Aligns the text vertically with the height.

Margin:auto

Teamed with width to center horizontally.

Hacks,hacks (Little Tricks)

There are many hacks, negative margin, shadow elements: Before and so on, if your content is not very fixed flower, most of them are very fragile.

Translate ( -50%,-50%)

Using position plus translate translate ( -50%,-50%) is more peculiar, the percentage calculation is not based on the parent element, but on its own basis.

Absolute Positioning Center

Note: Height must be defined and Overflow:auto is recommended to prevent content overflow.

Center Viewport

Content elements: positioon:fixed, z-index:999, remembering the parent container element position:relative.

Negative margin

Know exactly how wide the height is, and the negative margin is half the width and height.

CSS Common centering method

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.