CSS: Horizontally centered, vertically centered

Source: Internet
Author: User

Center Horizontally  
    1. margin:0 Auto Dafa
. child{width:100px;margin:0 Auto} Note: A child element must have a width before it can be used
    1. Display:table Dafa
. child{display:table; margin:0 Auto;}
    1. Text-align:center Dafa
The normal child element is in the parent element. parent{text-align:center}.child{Display:inline-block} If the parent element is literal, it can not be displayed as inline block elements, because I think the text has inline properties. Parent {Text-align:center}
    1. Absolute positioning Dafa
. parent{position:relative}.child{position:absolute; left:50%; transform:translate (-50%)} It's amazing!! Oh, yes!! How did I not think!! (1) Positioning if absolute (2) moves to 50%, negative value another:. parent{position:relative}.child{position:absolute; left:50%;margin-left:-element width}// You have to know the width of the child element
    1. Display:flex Dafa
CSS3 to Be continued   Center Vertically
    1. Vertical-align Dafa
. parent{Display:inline-block; vertical-align:middle; line-height:200px;}. parent{Display:table-cell; vertical-align:middle; height:200px;} Vertical-align and Line-height (height) are more interesting, control is the parent element, the center is the child element
    1. Absolute positioning Dafa
. parent{position:relative}.child{position:absolute; top:50%; Transform:translatey (-50%);} Flex Dafa Horizontal Vertical CenterSeveral ways are available for reference:. parent{display:table-cell;vertical-align:middle;text-align:center;height:100px;width:200px;}. Child{display:inline-block;} Way two:. parent{position:relative;}. child{position:absolute; top:50%; left:50%; transform:translate ( -50%,-50%);}

CSS: Horizontally centered, 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.