CSS elements are centered horizontally vertically

Source: Internet
Author: User

First, Text-align:center;vertical-align:middde;

HTML section

<class= "parent">    <class= " Child ">    </div></Div >

CSS Section

. Parent{width:400px;Height:400px;background:#666666;text-align:Center;font-size:0px; }. Child{font-size:16px;background:#ccc;Display:Inline-block;vertical-align:Middle; }. Parent:after{content:"';width:0;Height:100%;Display:Inline-block;vertical-align:Middle; }/*. add{width:0;         height:100%;         Display:inline-block;    Vertical-align:middle; }*/

You can also write a span class to add tag after the child's div without the pseudo element after.

font-size:0px: Resolves a problem where the word inside the child element exceeds the vertical center of a line.

  

Second, positioning top left right bottom margin:auto to achieve;

CSS elements are centered horizontally vertically

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.