How To Make a Moon With CSS

How To Make a Moon With CSS

I know making a moon with css sounds easy and simple but someone quote RTed one of my tweets

Screenshot from 2021-11-01 19-11-44 (1).png

So I thought I should make a quick post about it


If I were to explain how box-shadow property can make a moon, I'd say :
Imagine there's a hidden circle and moon is the shadow of this hidden circle.
Here, let me show you :

1. First let's ignore the box-shadow and make a circle

.moon {
    width: 15rem;
    height: 15rem;
    border-radius: 50%;
    background: #fff77e;
}

Screenshot from 2021-11-01 19-19-15 (1).png


2. Now give it box-shadow property

.moon {
    width: 15rem;
    height: 15rem;
    border-radius: 50%;
    background: #fff77e;
    box-shadow: 2rem 2rem 0 0 red;
}

Screenshot from 2021-11-01 19-22-20 (1).png

The red box-shadow looks like a moon, doesn't it?


3. Now remove the background color and you just created a moon

.moon {
    width: 15rem;
    height: 15rem;
    border-radius: 50%;
    box-shadow: 2rem 2rem 0 0 #fff77e;
}

Screenshot from 2021-11-01 19-26-13 (1).png


And here's the pen :