Item Positioning in CSS
I was just adding the previous post on how to draw a circle in css, when i thought it might also be useful to know how to move these items around. To make more complicated layouts or drawings.
Lets take our circles from the previous post and add them to our demo.
We have circle and circle-green.
HTML
<div class="circle"></div>
<div class="circle-green"></div>
CSS
.circle {
width: 32px;
height: 32px;
background: #9C27B0;
border-radius: 50%;
}
.circle-green {
margin:20px;
width: 200px;
height: 200px;
background:green;
border-radius: 50%;
}
Add some more circles, maybe change the pink circle to white.
This should give us something like this:
Ok so now we want to move the eye circles on top of the green circle.
To do this, we need to add a wrapper div around the current circle elements.
Now set this wrap class to relative. Which means all the elements in it will be able to be positioned within that element.
Now set all of the circle elements to absolute, which will place them in the same location unless you specify a top,right,bottom,left attribute, if you do this it will start moving them around in the circle wrap element.
As these elements are no longer going to be pusing out the size of the circle wrap, we will need to set a height and width on this element, otherwise its size will go to 0.
Set it to 100% and height of 300px to start with.
Now the dots are all sitting in the top left of the circle-wrap container.
Open up your dev tools in your browser. Usually F12 key. Depending on your browser and OS.
Select the first eye circle.
Now we can add the top, and the left attributes and you can see the circle moving in the browser.
Also you will notice that the eye dissapears under the green circle, this is due to its z index being lower than the green circle, as it appears before the green and is stacked under it.
So to fix this also add the z-index property and set it to 5, or greater than 0.
Now we can copy this rule and paste it into our css, once you are happy with where it is placed.
And now we have a green circle with 2 smaller circles in there on top.
CSS
.circle-eye-one {
width: 32px;
height: 32px;
background: #EEE;
border-radius: 50%;
position: absolute;
top: 70px;
left: 70px;
z-index: 5;
}
.circle-eye-two {
width: 32px;
height: 32px;
background: #EEE;
border-radius: 50%;
position: absolute;
z-index: 5;
top: 70px;
left: 140px;
}
.circle-green {
margin:20px;
width: 200px;
height: 200px;
background:green;
border-radius: 50%;
position:absolute;
}
.circle-wrap {
position:relative;
height:300px;
width:100%;
}
HTML
<div class="circle-wrap">
<div class="circle-eye-one"></div>
<div class="circle-eye-two"></div>
<div class="circle-green"></div>
</div>
Next i will be adding some kind of mouth to this.
Item Positioning in CSS Demo
View Demo Full Screen View Demo New Tab
Item Positioning in CSS Code
HTML
<div class="circle-wrap">
<div class="circle-eye-one"></div>
<div class="circle-eye-two"></div>
<div class="circle-green"></div>
</div>
CSS
.circle-eye-one {
width: 32px;
height: 32px;
background: #EEE;
border-radius: 50%;
position: absolute;
top: 70px;
left: 70px;
z-index: 5;
}
.circle-eye-two {
width: 32px;
height: 32px;
background: #EEE;
border-radius: 50%;
position: absolute;
z-index: 5;
top: 70px;
left: 140px;
}
.circle-green {
margin:20px;
width: 200px;
height: 200px;
background:green;
border-radius: 50%;
position:absolute;
}
.circle-wrap {
position:relative;
height:300px;
width:100%;
}