0

From this question ("Rotate objects around circle using CSS?"), I copied the following code, but is it possible to insert pictures into the code? I would like to insert pictures into the circles so that one picture orbits another. For example, Earth orbiting the sun.

How do I modify the code in such a way as to make the earth orbit the sun instead of the blank circles?

<!DOCTYPE html>
<html>
<head>
<style>
.outCircle  {
    width: 200px;
    height: 200px;
    background-color: blue;
    left: 270px;
    position: absolute;
    top: 50px;
    -moz-border-radius: 100px;
    -webkit-border-radius: 100px;
    border-radius: 100px;
}



.rotate {
    width: 100%;
    height: 100%;
    -webkit-animation: circle 10s infinite linear;    
   
}

.counterrotate {
    width: 50px;
    height: 50px;
    -webkit-animation: ccircle 10s infinite linear;    
}
    
.inner {
    width: 100px;
    height: 100px;
    background: blue;
    -moz-border-radius: 50px;
    -webkit-border-radius: 50px;
    border-radius: 100px;
    position: absolute;
    left: 0px;
    top: 0px;
    background-color: pink;
    display: block;

}


@-webkit-keyframes circle {
    from {-webkit-transform: rotateZ(0deg)}
    to {-webkit-transform: rotateZ(360deg)}
}

@-webkit-keyframes ccircle {
    from {-webkit-transform: rotateZ(360deg)}
    to {-webkit-transform: rotateZ(0deg)}
}
</style>
</head>
<body>
<script>
.outCircle {
  width: 200px;
  height: 200px;
  background-color: blue;
  left: 270px;
  position: absolute;
  top: 50px;
  -moz-border-radius: 100px;
  -webkit-border-radius: 100px;
  border-radius: 100px;
}
.rotate {
  width: 100%;
  height: 100%;
  -webkit-animation: circle 10s infinite linear;
}
.counterrotate {
  width: 50px;
  height: 50px;
  -webkit-animation: circle 10s infinite linear;
}
.inner {
  width: 100px;
  height: 100px;
  background: blue;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 100px;
  position: absolute;
  left: 0px;
  top: 0px;
  background-color: blue;
  display: block;
}
@-webkit-keyframes circle {
  from {
    -webkit-transform: rotateZ(0deg)
  }
  to {
    -webkit-transform: rotateZ(360deg)
  }
}
@-webkit-keyframes ccircle {
  from {
    -webkit-transform: rotateZ(360deg)
  }
  to {
    -webkit-transform: rotateZ(0deg)
  }
}
</script>

<div class="outCircle">
<div class="rotate">
<div class="counterrotate">
<div class="inner">Hello</div>
</div>
</div>
</div>
</body>
</html> 



EDIT: This is the output when I add the images:

<!DOCTYPE html>
<html>
<head>
<style>
.outCircle  {
    background-image: url("https://media.beam.usnews.com/5a/5e/5a739e244b289049e789d7752975/170531-sun-editorial.jpg");
    width: 200px;
    height: 200px;
    background-color: blue;
    left: 270px;
    position: absolute;
    top: 50px;
    -moz-border-radius: 100px;
    -webkit-border-radius: 100px;
    border-radius: 100px;
}



.rotate {
    width: 100%;
    height: 100%;
    -webkit-animation: circle 10s infinite linear;   
   
}

.counterrotate {
    width: 50px;
    height: 50px;
    -webkit-animation: ccircle 10s infinite linear;    
}
    
.inner {
    width: 100px;
    height: 100px;
    background: blue;
    -moz-border-radius: 50px;
    -webkit-border-radius: 50px;
    border-radius: 100px;
    position: absolute;
    left: 0px;
    top: 0px;
    background-color: pink;
    display: block;
    background-image: url("https://i.imgur.com/Eo52CF0_d.webp?maxwidth=760&fidelity=grand");

}


@-webkit-keyframes circle {
    from {-webkit-transform: rotateZ(0deg)}
    to {-webkit-transform: rotateZ(360deg)}
}

@-webkit-keyframes ccircle {
    from {-webkit-transform: rotateZ(360deg)}
    to {-webkit-transform: rotateZ(0deg)}
}
</style>
</head>
<body>
<script>
.outCircle {
  width: 200px;
  height: 200px;
  background-color: blue;
  left: 270px;
  position: absolute;
  top: 50px;
  -moz-border-radius: 100px;
  -webkit-border-radius: 100px;
  border-radius: 100px;
}
.rotate {
  width: 100%;
  height: 100%;
  -webkit-animation: circle 10s infinite linear;
}
.counterrotate {
  width: 50px;
  height: 50px;
  -webkit-animation: circle 10s infinite linear;
}
.inner {
  width: 100px;
  height: 100px;
  background: blue;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 100px;
  position: absolute;
  left: 0px;
  top: 0px;
  background-color: blue;
  display: block;
}
@-webkit-keyframes circle {
  from {
    -webkit-transform: rotateZ(0deg)
  }
  to {
    -webkit-transform: rotateZ(360deg)
  }
}
@-webkit-keyframes ccircle {
  from {
    -webkit-transform: rotateZ(360deg)
  }
  to {
    -webkit-transform: rotateZ(0deg)
  }
}
</script>
<div class="outCircle">
<div class="rotate">
<div class="counterrotate">
<div class="inner">Hello</div>
</div>
</div>
</div>
</body>
</html> 
pr0grammar
  • 107
  • 10

3 Answers3

3

i just add background-image css to both element. And linking the earth and moon picture from Wikipedia to that element.

body{
    background:#000;
}
.earth, .moon{
    background-position: center; /* Center the image */
    background-repeat: no-repeat; /* Do not repeat the image */
    background-size: 120%; /* Resize the background image to cover the entire container */
    -moz-border-radius: 50%; /* to make circle shape */
    -webkit-border-radius: 50%;
    border-radius: 50%;
}
.earth  {
    background-color: blue;
    background-image:url( "https://upload.wikimedia.org/wikipedia/commons/9/90/Small_Earth.jpg" );
    position: absolute;
    width: 200px;
    height: 200px;
    box-shadow:0 0 20px dodgerblue;
    margin:50px;
}
.moon {
    position: relative;
    background-color: white;
    background-image:url( "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/FullMoon2010.jpg/631px-FullMoon2010.jpg" );
    width: 100px;
    height: 100px;
}

.rotate {
    width: 100%;
    height: 100%;
    -webkit-animation: circle 10s infinite linear;
}
.counterrotate {
    width: 50%;
    height: 50%;
    -webkit-animation: ccircle 10s infinite linear;    
}

@-webkit-keyframes circle {
    from {-webkit-transform: rotateZ(0deg)}
    to {-webkit-transform: rotateZ(360deg)}
}

@-webkit-keyframes ccircle {
    from {-webkit-transform: rotateZ(360deg)}
    to {-webkit-transform: rotateZ(0deg)}
}
<div class="earth">
  <div class="rotate">
    <div class="counterrotate">
      <div class="moon"></div>
    </div>
  </div>
</div>
Hanna Rose
  • 412
  • 2
  • 9
  • 1
    You'r wellcome (: i also found the complex version Earth, Moon and Sun by Stéphanie Walter. check this link bellow if you interested: https://codepen.io/stephaniewalter/pen/vYGmrLR – Hanna Rose Aug 03 '21 at 21:11
  • i just do some revision. i remove all useless code, now its clean; adding dark background and blue glow on earth as the atmosphere. – Hanna Rose Aug 04 '21 at 21:48
1

You could try adding each image as a background-image right in the CSS rules that define their size/shape.

Ito Pizarro
  • 1,607
  • 1
  • 11
  • 21
1

Add a background image to your elements. Open your inspector and click on the element and it will highlight the elements border-box, identify which element is what and then in your CSS, add a background:url(link to the image) to the selector/element you wish to have an image on.

.outCircle {
  width: 200px;
  height: 200px;
  background-color: blue;
  left: 270px;
  position: absolute;
  top: 50px;
  -moz-border-radius: 100px;
  -webkit-border-radius: 100px;
  border-radius: 100px;
}

.rotate {
  width: 100%;
  height: 100%;
  -webkit-animation: circle 10s infinite linear;
  background: url(https://cdn.pixabay.com/photo/2017/05/12/22/48/mouse-2308339__180.jpg) no-repeat;
  border-radius: 50%;
}

.counterrotate {
  width: 50px;
  height: 50px;
  -webkit-animation: circle 10s infinite linear;
}

.inner {
  width: 100px;
  height: 100px;
  background: blue;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 100px;
  position: absolute;
  left: 0px;
  top: 0px;
  display: block;
  background: center url(https://as1.ftcdn.net/jpg/00/31/01/02/220_F_31010244_P6FGF9nfBY1oaGFndhdHhUUIfjHqMoib.jpg) no-repeat white;
  background-size: 90%;
}

@-webkit-keyframes circle {
  from {
    -webkit-transform: rotateZ(0deg)
  }
  to {
    -webkit-transform: rotateZ(360deg)
  }
}

@-webkit-keyframes ccircle {
  from {
    -webkit-transform: rotateZ(360deg)
  }
  to {
    -webkit-transform: rotateZ(0deg)
  }
}
<div class="outCircle">
  <div class="rotate">
    <div class="counterrotate">
      <div class="inner">Hello</div>
    </div>
  </div>
</div>
dale landry
  • 7,831
  • 2
  • 16
  • 28