DEV Community

Cover image for Is Your Website Slow? How Unexpected CSS Units Can Be the Problem (and the Solution!)
Werliton Silva
Werliton Silva

Posted on • Edited on

Is Your Website Slow? How Unexpected CSS Units Can Be the Problem (and the Solution!)

"Did you know that your choice of CSS unit can directly impact your application’s performance, affecting animations and even screen refresh rates?"


measuring

What is Throughput in Frontend?

Throughput refers to the amount of work the browser can accomplish per second. This includes:

  • Measuring and painting elements on the screen.
  • Responding to layout changes.
  • Handling real-time resizes or animations.

Poorly chosen units can negatively impact this crucial performance metric.


Units and Their Impact on Throughput

Unit Advantages Disadvantages Best For
px Easy to compute, fast Doesn't scale well with zoom Borders, shadows, fast animations
em/rem Flexible, accessible Computationally heavy in animations Fonts, scalable layouts
% Adapts to parent size Can cause costly reflows Image and container widths
vw/vh Dynamic based on viewport Impacts performance on mobile Responsive element dimensions
fr Efficient for CSS Grid layout Expensive in dynamic grids Structured layouts

Practical Cases of Poor Throughput

❌ Animation with em on Nested Elements

.card-title {
  animation: pulse 0.3s infinite;
  font-size: 1.2em; /* Bad for animation */
}
Enter fullscreen mode Exit fullscreen mode

Every font-size change for .card-title requires the browser to recalculate its size based on its parent's font size. In a long list of cards, this becomes very slow, causing jank during the animation.

✅ Alternative with px or rem for Animations

.card-title {
  font-size: 1.2rem; /* Better for animation */
}

/* Or for a fixed size */
.card-title {
  font-size: 18px; /* Best for animation when size is fixed */
}
Enter fullscreen mode Exit fullscreen mode

rem is based on the root html font size, making its calculation much faster for the browser. px is absolute and the fastest for animations when you don't need scaling based on user preferences.


Best Practices

  • Use px or rem in animations and transitions. They offer predictable and efficient calculations.
  • Avoid % and vw/vh on elements with high update frequency. If an element's size needs to change constantly, these units can introduce performance bottlenecks.
  • Avoid em in deeply nested structures with many levels. This prevents a cascade of costly recalculations.
  • Use will-changeto optimize animated elements. This CSS property hints to the browser about what properties will change, allowing it to prepare for the animation more efficiently.
.card:hover {
  will-change: transform, opacity; /* Hint to the browser for better performance */
  transform: scale(1.05);
  opacity: 0.9;
}
Enter fullscreen mode Exit fullscreen mode

In Summary:

Choosing the right CSS unit goes beyond aesthetics; it's a critical decision for optimizing your web application's performance. By understanding how each unit impacts browser calculations, you can make informed choices that lead to smoother animations, faster rendering, and a better user experience.


"Have you ever faced CSS performance issues? Share your experience in the comments or check out advanced optimization techniques!"

Top comments (0)