DEV Community

Siva Ranchani
Siva Ranchani

Posted on

How to Build an Animated CSS Penguin: A Step-by-Step Guide #html5 #css3 #animation #Responsive web design

**While completing the freeCodeCamp Responsive Web Design Certification, I built an interactive penguin mascot to master pure CSS styling, positional layout techniques, and keyframe animations.

In this tutorial, I'll walk you through the end-to-end process of structuring the HTML elements, applying custom styles, and creating a waving animation using pure CSS.

1. Project Overview & Interactive Demo

Before diving into the code, you can view and test the complete live code directly on my CodePen:

Live Demo: View my Penguin Project on CodePen

  1. Building the HTML Foundation

The penguin is composed of semantic div elements representing the head, facial features, body, heart badge, and arms.

HTML

I <span>♥</span> CSS
Enter fullscreen mode Exit fullscreen mode
  1. Positioning and Background Gradients

To create the vibrant background seen in the preview, we use CSS linear gradients across a multi-layered canvas:

CSS
/* Background environment layout */
body {
background: linear-gradient(90deg, rgb(88, 175, 236), rgb(182, 255, 255));
margin: 0;
padding: 0;
}

/* Parent container setup */
.penguin {
position: relative;
margin: 0 auto;
width: 200px;
height: 200px;
z-index: 3;
}
Using position: relative on .penguin creates an anchor frame. Every child element inside (like the eyes, beak, and arms) can then be placed precisely using position: absolute.

  1. Crafting the Keyframe Wave Animation

To animate the penguin waving its arm smoothly back and forth, we define a @keyframes sequence and anchor the arm's rotational pivot point using transform-origin.

CSS
/* Define keyframe rotation */
@keyframes wave {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(110deg);
}
100% {
transform: rotate(0deg);
}
}

/* Apply animation to the arm /
.arm.right {
position: absolute;
top: 10%;
left: 5%;
/
Set shoulder joint pivot point */
transform-origin: 0% 0%;
animation: wave 3s infinite ease-in-out;
}
By default, CSS elements rotate around their middle (50% 50%). Setting transform-origin: 0% 0% locks the rotation to the top-left shoulder, allowing the arm to swing naturally instead of spinning in place.

  1. Summary & Key Takeaways

Layering with z-index: Controls feature stacking order so eyes and beak sit cleanly above face shapes.

Transform Origins: Crucial for setting realistic joints and motion paths for 2D CSS animations.

Timing Functions: ease-in-out creates organic acceleration and deceleration during animation cycles.

About the Author

I am a MERN Stack Developer focused on building responsive, performant, and user-centric web applications. Currently expanding her skills through freeCodeCamp certifications, she specializes in clean CSS architecture, React UI components, and full-stack API integration.

i am available for remote frontend development roles, technical writing, and freelance projects.

Top comments (0)