What You'll Learn
You'll learn why opacity and transform are the go-to CSS properties for smooth, performant animations in React applications.
Why These Properties Matter
Not all CSS properties animate equally. When you change most properties (like width, height, top, or left), the browser must recalculate layout for the entire page—a process called layout thrashing that kills performance.
opacity and transform, however, are compositor-only properties. The browser can animate them on the GPU without triggering layout or paint operations, resulting in buttery-smooth 60fps animations even on slower devices.
The Performance-Friendly Duo
opacity
Controls transparency from 0 (invisible) to 1 (fully visible). Perfect for fade-in/out effects:
.fade-enter {
opacity: 0;
}
.fade-enter-active {
opacity: 1;
transition: opacity 300ms ease-in;
}
transform
Handles visual transformations without affecting layout:
translateX/Y/Z()— move elementsscale()— resizerotate()— spinskew()— distort angles
.slide-enter {
transform: translateX(-100%);
}
.slide-enter-active {
transform: translateX(0);
transition: transform 300ms cubic-bezier(0.4, 0, 0.2, 1);
}
Why Avoid width, height, top, left?
Think of layout properties like rearranging furniture—every change forces the browser to reconsider where everything else sits. transform and opacity are like moving a sticker on a window: the position changes, but nothing underneath needs adjusting.
In React Components
const [isVisible, setIsVisible] = useState(false);
<div
className={isVisible ? 'modal-active' : 'modal-hidden'}
style={{
transition: 'opacity 200ms, transform 200ms',
opacity: isVisible ? 1 : 0,
transform: isVisible ? 'scale(1)' : 'scale(0.9)'
}}
>
Modal Content
</div>
Key Takeaway: Stick to opacity and transform for animations—they bypass expensive layout calculations and leverage GPU acceleration for smooth, performant transitions that won't janky up your UI, even on mobile devices.