CSS Centering Method Summary

Source: Internet
Author: User

1.text-align:center;

Centering by using the above method is to center the Display:inline in the parent container, and the inline element or text horizontally.

2.inline-height: (height) value;

When using inline-height, it is often used with the height, so that the content is centered vertically, it is recommended to use Li inside the list;

3.margin:auto;

When using margin to center, the element is usually given the exact width value, set the Margin:auto; or margin:0 auto; Is the same effect: the current element will be horizontally centered;

When using margin, you can also set the value of Margin-left and Margin-right equal, but also to achieve horizontal center;

4.position Positioning Center

Set by top and left or bottom, right to align elements horizontally and vertically, but parent elements must be set to height;

5.Flex Box

Flex-align:center of the Flex container;

Align-items:center;

Justify-cotent:center;

Align-self:center of project properties;

6. Negative margin:

1 . Box{2 width:600px;3 Height:400px;4 position:relative;5}6 7 . Box1{8 width:300px;9 Height:200px;Ten padding:20px; One position:Absolute; A Top:50%; -  Left:50%; - Margin-left:-170px;/*(width+padding)/2*/ the Margin-top:-120px;/*(height+padding)/2*/ -}

The above code enables horizontal vertical centering of elements.

CSS Centering Method Summary

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.