Note: You can center the DIV horizontally and vertically.

Source: Internet
Author: User

Note: You can center the DIV horizontally and vertically.

Today I wrote a job for the Baidu front-end college spring class:Locate and center problemsSince I learned CSS for a long time, and I used to take notes, I forgot only one of the following methods, today, I summarized the following two simple ways to center the DIV horizontally and vertically. =. =

First:

HTML code:

1 <div class="wrap">2     <div class="main first">3         <div id="left" class="yuan"></div>4         <div id="right" class="yuan"></div>5     </div>6 </div>

CSS:

 1 .main{ 2     width: 400px; 3     height: 200px; 4     overflow: hidden; 5     background-color: #ccc; 6     position: absolute; 7 } 8 .yuan{ 9     width: 100px;10     height: 100px;11     background-color:yellow;12     border-radius: 50%;13     -moz-border-radius: 50%;14     -webkit-border-radius: 50%;15     position: absolute;16 }17 #left{18     top: -50px;19     left: -50px;20 }21 #right{22     bottom: -50px;23     right: -50px;24 }

 

Method 1: use negative margin, provided that the size is known. Best compatibility.

Sets the horizontal and vertical offset of 50% of the parent element,
Then, the child element is moved to the left to half the size based on the actual length.
1 .first{2     top: 50%;3     left: 50%;4     margin-left: -200px;5     margin-top: -100px;6 }

Method 2: Use the CSS3 transform with an indefinite width and support IE9 +

1 .second{2     left: 50%;3     top: 50%;4     transform: translate(-50%,-50%);5 }

 

Related Article

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.