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

1

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!"

Dev Diairies image

User Feedback & The Pivot That Saved The Project ↪️

We’re following the journey of a dev team building on the Stellar Network as they go from hackathon idea to funded startup, testing their product in the real world and adapting as they go.

Watch full video 🎥

Top comments (0)

AWS Q Developer image

Build your favorite retro game with Amazon Q Developer CLI in the Challenge & win a T-shirt!

Feeling nostalgic? Build Games Challenge is your chance to recreate your favorite retro arcade style game using Amazon Q Developer’s agentic coding experience in the command line interface, Q Developer CLI.

Participate Now

👋 Kindness is contagious

Show gratitude for this enlightening post and join the vibrant DEV Community. Developers at every level are invited to share and grow our collective expertise.

A simple “thank you” can make someone’s day. Leave your appreciation below!

On DEV, collaborative knowledge clears our path and deepens our connections. Enjoyed the article? A quick message of thanks to the author goes a long way.

Count me in