CSS Centering method

Source: Internet
Author: User

CSS centering method is very much, according to the actual situation of the work to adopt the appropriate method, it is desirable to a multiplier effect.

Some of the common centering methods are organized as follows:

The code is as follows:

<div class= "Con" >
<div class= "Con1" >

<span> Front-end development </span>
</div>
</div>

If the. Con1 is to be centered when it knows the width and height, you can use the following methods:

Simple vertical centering: margin:0 auto; (provided the fixed width is set to. Con1 and the width is less than the parent element. Con);

Center vertically and horizontally:. con{position:relative;}

. con1{width:300px;height:300px; position:absolute;top:50%;left:50%;margin-left:-150px;margin-top:-150px;}

The method of centering the picture inside the. Con1:. con1{height:300px; line-heigth:300px;}

. Con1 Img{vertical-align:middle}

The method of centering the text inside the. Con1: Use only. con1{height:300px; line-heigth:300px;} is enough;

Universal Centering Method:

Method One:

. con{position:relative;}

. Con1{position:absolute;top:0;right:0;bottom:0;left:0;margin:auto;}

Method Two:

. con{position:relative;}

. Con1{position:absolute;top:50%;left:50%;transform:translate ( -50%,-50%);-webkit-transform:translate ( -50%,-50%) ;-moz-transform:translate ( -50%,-50%);-o-transform:translate ( -50%,-50%);}

Horizontal vertical Centering method under Flexbox layout:

. Con{justify-content:center;align-items:center;display:-webkit-flex;}

CSS Centering method

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.