CSS (3) for horizontal vertical centering

Source: Internet
Author: User

Method One:

 div  { position :  absolute ; /*  position:fixed can also  */  left :  0 ;  right :  0 ;  top :  0 ;  bottom :  0 ;  width :  100px ;  height :  100px ;  margin :  auto ;  Background-color :  green ;} 
< Div ></ Div >

Method Two:

 div  { position :  absolute ; /*  position:fixed can also  */  left :  50% ;  top :  50% ;  width :  100px ;  height :  100px ;  Margin-left :  -50px ;  Margin-top :  -50px ;  Background-color :  red ;} 
< Div ></ Div >

Method Three:

 div  { position :  absolute ; /*  position:fixed can also  */  left :  50% ;  top :  50% ;  width :  100px ;  height :  100px ;  Transform :  translate ( -50%,-50%) ;  Background-color :  green ;} 
< Div ></ Div >

Method Four:

 body  { display :  Flex ;  justify-content :  center ; /*   */  Align-items :  center ; /*   */} div  { width :  100px ;  height :  100px ;  Background-color :  green ;} 
< Div ></ Div >

Method Five:

Div {    position: absolute;     Left: Calc ((100%-100px)/2);     top: Calc ((100%-100px)/2);     width: 100px;     height: 100px;     background-color: red;}
< Div ></ Div >

Method Six:

. Table{Display:Table;/*This element is displayed as a block-level table (similar to <table>) with line breaks before and after the table*/width:100%;Height:100%; }. OK{Display:Table-cell;/*This element is displayed as a table cell (similar to <td> and <th>)*/text-align:Center;/*Center Horizontally*/vertical-align:Middle;/*Center Vertically*/}. Innerbox{Display:Inline-block;width:100px;Height:100px;Background-color:Red;}
<class= ' table '>    <class= ' Ok '>        <class= ' Innerbox '></  div>    </div></Div  >

CSS (3) for horizontal vertical centering

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.