CSS/CSS3 to achieve vertical centering and horizontal centering of unknown, wide-height elements

Source: Internet
Author: User

Some of its usual layouts also often have the need to achieve vertical centering and horizontal centering of elements, following the writing of the horizontal and vertical centering of unknown wide-height elements implemented by several CSS/CSS3

PS: Do not consider compatibility issues (next will write JS implementation elements of the horizontal and vertical center)

The first kind of CSS3 transform

. ele{/* set Element Absolute positioning */    position:absolute;/*top 50%*/    top:50%;/*left 50%*/left:50%;/*css3 transform   implementation *    /Transform:translate (-50%,-50%);}

The second type of Flex box layout

. ele{/* Elastic Box Model */        display:flex;/* Spindle Center Alignment */    justify-content:center;/* side Axis alignment */        align-items:center;   }

The Third Kind of display Table-cell

. box{/* let elements render as table cells */    display:table-cell;/* Set text horizontally centered */    text-align:center;/* Set Text to center vertically */    vertical-align : Middle;     

The third type is to add a style to the parent. First write these kinds, enough for everyone to use, and through the pseudo-class to achieve the center of the effect, interested can knock yourself down to try

CSS/CSS3 to achieve vertical centering and horizontal centering of unknown, wide-height elements

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.