
How to Create Web Animations With Only HTML and CSS
Creating engaging web animations doesn’t always require JavaScript. With modern HTML and CSS, you can build smooth, performant animations that enhance user experience while keeping your code lightweight and maintainable.
CSS animations and transitions allow you to bring interfaces to life using simple properties liketransform, opacity, and transition. By leveraging these tools,
you can create hover effects, loading indicators, and even complex sequences—all without writing a single line of
JavaScript.
One of the biggest advantages of CSS-based animations is performance. Since they’re handled by the browser’s rendering
engine, they’re often more efficient and smoother than JavaScript-driven animations. This makes them ideal for subtle UI
enhancements and responsive design patterns.
Basic Steps
To start building animations with HTML and CSS, focus on defining a simple element and applying transitions or keyframe animations. Begin with a base state and define how the element should change when triggered—such as on hover or when a class is added. Properties liketransform and
opacity are especially useful because they are optimized for performance and don’t trigger layout
recalculations. You can also control timing using easing functions and durations to create natural motion. Understanding
how states change and how the browser interpolates between them is key to mastering CSS animations. - 1. Define a simple HTML element to animate
- 2. Use CSS transitions or keyframes to define motion
- 3. Trigger animations with hover states or class changes
Example
Markup
<div class="box"></div>
.box {
width: 100px;
height: 100px;
background: #1e87f0;
transition: transform 0.4s ease, opacity 0.4s ease;
}
.box:hover {
transform: translateY(-20px) scale(1.1);
opacity: 0.8;
}
/* Keyframe animation example */
@keyframes float {
0% {
transform: translateY(0);
}
50% {
transform: translateY(-15px);
}
100% {
transform: translateY(0);
}
}
.box.animate {
animation: float 2s ease-in-out infinite;
}
In this example, the box element responds to user interaction using a hover
state, while also supporting a reusable keyframe animation with the animate class. Combining
transitions and animations gives you flexibility to create both interactive and continuous motion effects without
relying on JavaScript.
transform and opacity
whenever possible. These are GPU-accelerated and result in smoother animations compared to properties that trigger
layout changes, such as width or margin.
Component options
| Value | Description |
|---|---|
ease |
Starts slow, speeds up, then slows down again for a natural motion feel. |
linear |
Maintains a constant speed from start to finish. |
infinite |
Repeats the animation continuously without stopping. |