How CSS elements are horizontally vertically centered

Source: Internet
Author: User

1. The element is centered horizontally

1.1 Setting the properties of a parent element

Text-align:center;

Description: This property is valid only for child elements of the parent element, such as Img,input,select,button (inline elements, but in the form of inline elements, can be set width and height), span needs to set the display property to inline, and set width and height.

Reference to: MDN inline element

1.2 Setting the element's own margin properties

Margin:0 Auto;display:block;

Description: The element must be set as a block element, and the margin property automatically assigns the left and right margins to the center effect

1.3 Setting element Positioning properties

position:relative; The parent element sets the relative positioning Position:absolute; The child element sets the absolute position and moves the element to the left by half its length Width:50px;height:50px;left:50%;transform:translatex (-50%);

2. The element is centered vertically

2.1 Setting element positioning properties, similar to 1.3

position:relative; The parent element sets the relative positioning Position:absolute; The child element sets the absolute position and moves the element to the left by half its length Width:50px;height:50px;top:50%;transform:translatey (-50%);

2.2 Setting elements with the table attribute

Display:table-cell;vertical-align:middle;

Description: The inline or inline element is disguised as a cell lattice element, and the cell is processed through the properties of the table tag, which is incompatible with the lower version of IE6/7

3. Element horizontally vertically Centered

3.1 Setting element positioning properties, similar to 1.3

position:relative; The parent element sets the relative positioning Position:absolute; The child element sets the absolute position and moves the element to the left by half its length width:50px;height:50px;left:50%;top:50%;transform:translate (-50%,-50%);

3.2 Auto-positioning attributes based on elements

position:absolute;top:0;right:0;bottom:0;left:0;margin:auto;width:50px;height:50px;

Note: This method is similar to 3.1, except that all margin values are automatically calculated to reach the top and bottom left and right center effect.

Reference: https://www.cnblogs.com/zjjDaily/p/5952723.html

How CSS elements are horizontally vertically centered

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.