"Stop bloating your bundles! If your animation task is purely visual (like a simple hover, pulse, or loader), native CSS @keyframes
and the animation
property are your fastest, most performant solution. Here’s a quick breakdown and a practical example."
As frontend developers, we strive for smooth, 60fps experiences. While JS libraries like GSAP are incredible for complex sequences, they carry a payload and run on the main thread. For basic visual cues, we must leverage the browser's hardware acceleration via CSS.
The Power of @keyframes
The @keyframes
rule defines the style state at specific points during the animation sequence. The browser handles all the interpolations between these states.
Key Animation Properties:
Property | Description | Example |
---|---|---|
animation-name |
Links the class to your @keyframes block. |
pulse |
animation-duration |
Specifies how long one cycle of the animation takes. | 2s |
animation-iteration-count |
Specifies how many times the animation should run (or infinite ). |
infinite |
animation-timing-function |
Controls the acceleration/deceleration curve. | ease-in-out |
The Subtle Button Pulse Example
This animation subtly scales a button up and down infinitely—a common marketing trick—with minimal overhead.
/* 1. Define the states */
@keyframes pulse-btn {
0%, 100% {
transform: scale(1);
box-shadow: 0 0 0 rgba(255, 100, 0, 0);
}
50% {
transform: scale(1.03);
box-shadow: 0 0 10px rgba(255, 100, 0, 0.5);
}
}
/* 2. Apply the animation */
.animated-button {
background: orange;
color: white;
padding: 10px 20px;
/* Shorthand application: name duration timing-func iteration-count */
animation: pulse-btn 2s ease-in-out infinite;
}
The Takeaway
By leveraging properties like transform
and opacity
inside your @keyframes
, you tap into hardware acceleration. Always profile your animations, but for simple, continuous visual effects, native CSS is the fastest path to a smoother user experience.
What performance trick do you use most often in your CSS?
Top comments (0)