This article brings you to the content is about how to use pure CSS to achieve the sun and the Earth and the moon's running model animation, there is a certain reference value, the need for friends can refer to, I hope to help you.
Effect Preview
Source code Download
Https://github.com/comehope/front-end-daily-challenges
Code interpretation
To define the DOM, the container contains 3 elements:
<div class= "Container" > <div class= "Sun" ></div> <div class= "Earth" > <div Class= "Moon" ></div> </div></div>
Center display:
body { margin:0; HEIGHT:100VH; Display:flex; Align-items:center; Justify-content:center; Background-color:black;}
Define Container Dimensions:
. container { font-size:10px; Width:40em; Height:40em; Position:relative;}
Draw the Sun:
. Sun { position:absolute; top:15em; left:15em; Width:10em; Height:10em; Background-color:yellow; border-radius:50%; box-shadow:0 0 3em White;}
Draw the path of the Earth and the moon:
. Earth,.moon { position:absolute; Border-style:solid; Border-color:white transparent transparent transparent; Border-width:0.1em 0.1em 0 0; border-radius:50%;}. Earth { top:5em; left:5em; Width:30em; Height:30em;}. Moon { top:0; right:0; Width:8em; Height:8em;}
Plot the Earth and the moon with pseudo-elements:
. earth::before,.moon::before { position:absolute; border-radius:50%; Content: ';}. Earth::before { top:2.8em; right:2.5em; Height:3em; Width:3em; Background-color:aqua;}. Moon::before { top:0.8em; Right:0.2em; Width:1.2em; Height:1.2em; Background-color:silver;}
To set the animation effect of running:
/* Rotation period 365.2422 days */.earth { animation:orbit 36.5s linear infinite; } /* Rotation period 27.322 days */.moon { animation:orbit 2.7s linear Infinite;} @keyframes Orbit {to { transform:rotate (360deg); }}
Finally, hide the parts that might appear outside the container:
body { Overflow:hidden;}
Done!