How to create an Angry Birds green pig using pure CSS

Implementation steps

Define the DOM. The container contains three elements representing the ear, eye, and nose:

<div class="pig">
    <span class="ears"></span>
    <span class="eyes"></span>
    <span class="nose"></span>
</div>
Copy the code

Center display:

body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: skyblue;
}
Copy the code

Set the common attributes of the pseudo-element. There are several uses of the pseudo-element:

.pig::before..pig::after..pig *::before..pig *::after {
    content: ' ';
    position: absolute;
}
Copy the code

Define the container size:

.pig {
    width: 12em;
    height: 10em;
    font-size: 20px;
    background-color: #50a032;
    border: 0.2 em solid #2b4d13;
}
Copy the code

Outline the head with the rounded corner property:

.pig {
    border-radius: 50% 50% 50% 50% / 55% 60% 40% 45%;
}
Copy the code

Border-radius was thought to have four corners, but can actually have eight edges.

Outline your nose:

.pig {
    position: relative;
}

.nose {
    position: absolute;
    width: 4.6 em;
    height: 4em;
    background-color: #82b923;
    border: 0.1 em solid #1d3c07;
    border-radius: 50% 50% 45% 45% / 55% 55% 45% 45%;
    top: 3em;
    left: 4.2 em;
}
Copy the code

Draw nostril with false element:

.nose::before..nose::after {
    width: 1.2 em;
    background-color: #0f2d00;
    border-radius: 50%;
    top: 1.4 em;
}

.nose::before {
    left: 0.8 em;
    height: 1.8 em;
}

.nose::after {
    right: 0.8 em;
    height: 1.6 em;
}
Copy the code

Outline your eyes:

.eyes::before..eyes::after {
    width: 2.8 em;
    height: 2.8 em;
    background: white;
    border-radius: 50%;
    border: 0.1 em solid #193c09;
    top: 3.6 em;
}

.eyes::before {
    left: 0.8 em;
}

.eyes::after {
    right: 0.3 em;
}
Copy the code

Draw eyes with radial gradient:

.eyes::before..eyes::after {
    background: 
        radial-gradient(
            circle at var(--eyeball-left) 1.5 em,
            black 0.4 em,
            transparent 0.4 em
        ),
        white;
}

.eyes::before {
    --eyeball-left: 1em;
}

.eyes::after {
    --eyeball-left: 1.9 em;
}
Copy the code

Outline the inner ear:

.ears::before..ears::after {
    width: 0.8 em;
    height: 0.9 em;
    background-color: #2f6317;
    border: 0.1 em solid #1d3a0d;
    border-radius: 45% 45% 45% 45% / 55% 45% 55% 45%;
}

.ears::before {
    top: 0.3 em;
    left: 1.3 em;
}

.ears::after {
    top: -1.1 em;
    right: 5.8 em;
}
Copy the code

Shadow the outer ear:

.ears::before {
    color: #50a032;
    box-shadow: 
        0.4 em 0.7 em 0 -0.2 em,
        -0.2 em 0.7 em 0 -0.1 em,
        -0.6 em 0.5 em 0 -0.2 em,
        -0.1 em -0.2 em 0 0.4 em,
        -0.1 em -0.2 em 0 0.6 em #2b4d13;
    transform: rotate(-40deg);
}

.ears::after {
    color: #5cb739;
    box-shadow: 
        0.3 em 0.6 em 0 -0.2 em,
        -0.1 em 0.6 em 0 -0.1 em,
        -0.6 em 0.6 em 0 -0.2 em,
        -0.1 em -0.2 em 0 0.4 em,
        -0.1 em -0.2 em 0 0.6 em #2b4d13;
    transform: rotate(-6deg);
}
Copy the code

Draw eyebrows with false elements:

.pig::before..pig::after {
    width: 1.4 em;
    height: 1em;
    border-top: 0.5 em solid #0f2d00;
    top: 2.3 em;
    border-radius: 50% 50% 0 0 / 40% 40% 0 0;
}

.pig::before {
    left: 1.2 em;
    transform: rotate(-20deg);
}

.pig::after {
    right: 1em;
    transform: rotate(25deg);
}
Copy the code

Next, set the shadow to increase the stereo effect. Add a shadow effect to the head:

.pig {
    box-shadow: 
        inset -1.5 em 1em 1.5 em -0.5 em rgba(255.255.255.0.3),
        inset 0.5 em -0.5 em 0.8 em 0.2 em rgba(0.0.0.0.2);
}
Copy the code

Add shadow effect to nose:

.nose {
    box-shadow: -0.1 em 0.5 em 0.2 em 0.1 em rgba(68.132.36.0.6);
}

.nose::before..nose::after {
    box-shadow: inset -0.3 em -0.2 em 0.1 em -0.1 em #2d6b1f;
}
Copy the code

Add shadow effect to eyes:

.eyes::before..eyes::after {
    box-shadow: 
        inset 0.3 em -0.6 em 0.5 em -0.2 em rgba(0.0.0.0.3),
        -0.1 em 0.5 em 0.2 em 0.1 em rgba(68.132.36.0.6);
}
Copy the code

Ok!

References:

  • www.runoob.com/cssref/func…
  • Segmentfault.com/a/119000001…