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 }