CSS Positions
Posted in CSS Positions
5082
1:33 am, June 14, 2022

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%;
}

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Items in CSS Positions
Search Code
Search Code by entering your search text above.
Welcome to CSSBundle.com

Hi there and welcome to CSSBundle. Here at CSS Bundle we aim to find the best css and javascript bundles and demos and add them here to make them easy to find all in one place.

If you would like to stay up to date with our latest updates you can subscribe to our mailing list where we will send you one email per week with the latest updates, no spam emails just the latest updates.

Random Quote


Latest Code
css animations Falling Stars CSS Animation html Video different sources on screen sizes video embed html responsive css animations Animated Triangles on Canvas JS HTML HTML Canvas Instant colour fill with HTML Canvas javascript Transform Text into Images using SnapText JS CSS Tools Fancy Border Radius CSS Generator Tool - spin animation keyframes CSS Tools CSS Drop Filter Shadow Creator CSS Slider A CSS and HTML Only Carousel Slider CSS Z-Index Z-Index Code Front - CSS to change z-index when element is active sections section with image to the right text to the left gradient Sections Two Clickable Logo Promo Box Section Foundation HTML CSS Sections Inline Contact Form Formatting HTML CSS Sections Overview full width section in Poppins font with light gray background using foundation Interactive Images Map Image with Easy Dots and Titles using only CSS HTML Sections Social Header Links easy Drop in Code with Font Awesome Icons Sections Responsive Friendly Subscribe to our Newsletter Section CSS Tips Create a button with CSS, HTML and Center it! CSS Text Formatting Truncate Text to an amount of lines using line-clamp CSS Positions Item Positioning in CSS CSS Tips Draw a Circle in CSS css animations Magical Text Effect Bootstrap 4.2 Kitchen Sink CSS Frameworks Make your site look like windows 7 CSS Backgrounds Fancy up your banner image hero sections using overlay gradients CSS Modal Floating Modal Message Fixed on the Bottom of the Screen CSS Modal Create a modal with only CSS css grid display items in a css grid CSS Text Effects Using the selection selector to change the default highlight color Text Truncate your Text with CSS CSS Cursors Change your cursor into an Emoji Cursor or Image Cursor CSS How to Center Everything or Anything with CSS CSS Animations Smooth Scrolling with just CSS CSS Animations Radial Glow under cursor on box hover over effect CSS Shadows Using a filter drop shadow for transparent images CSS Animations Pure CSS 3D Flipping Book with Animations CSS Create a checkerboard background pattern with CSS CSS Animations button animations hover and fill animation CSS Animations button on focus swing animation CSS Animations Animated Button Border when Active Quick CSS create a border with top triangle in css CSS Animations Single Element CSS Spinners CSS Animations CSS Shake - Shake up your elements CSS Framework NES-style(8bit-like) CSS Framework CSS Tips Why to use a CSS Reset? Web Developer Checklists Front-End Checklist Icons Font Awesome Icons CSS Animation Animate Stuff with Animate.css CSS Animations Check Wave Click a Checkbox and Watch the Animation Animation Cloudy spiral animation with CSS3 Notifications Toast Messages and Notifications Standalone Library no JQuery - notify.js