Box Shadow Performance Killer

2 min read

When it comes to adding visual effects to our web designs, box-shadow is a popular CSS property that is often used. However, as developers, we need to be mindful of the performance cost that comes with using it. In some cases, using a pseudo-element might be a better option.

Changing box-shadow alters the geometry of the element. That means that it may affect the position or size of other elements on the page, both of which require the browser to perform layout operations. Once those layout operations have completed any damaged pixels will need to be painted and the page must then be composited together.

— box-shadow, csstriggers.com

Monitoring Performance with Chrome Developer Tools

To compare the performance cost of box-shadow vs using a pseudo-element, we can use Chrome Developer Tools. In the Performance Monitor tab, we can track the rendering performance of our web page. We can observe the rendering timeline and look for any significant dips in performance that might be caused by box-shadow. We can also use the Rendering tab to identify any rendering issues that might be impacting performance.

Animating Box-Shadow

Box-shadow is a property that adds a shadow effect to an element. It is a commonly used property that can add depth and dimension to a design. However, box-shadow can have a significant impact on performance, especially when it is used extensively on a web page. Each box-shadow requires a separate rendering pass, which can slow down the rendering process and impact the overall performance of the page.

example of box-shadow performance

Using a Pseudo-Element for Shadow Effects

On the other hand, using a pseudo-element to add a shadow effect can be a more performant option. By creating a separate element and applying a shadow effect to it, we can avoid the performance cost of box-shadow. This technique can be especially useful when the shadow effect is applied to multiple elements on a page.

example of psuedo element performance

Conclusion

When it comes to adding a shadow effect to our designs, we should be mindful of the performance cost of box-shadow. While it is a popular property, using it extensively can impact the performance of our web pages. By using a pseudo-element instead, we can avoid the performance cost and maintain a fast and responsive website. By using Chrome Developer Tools to monitor and observe performance, we can ensure that our designs are both visually appealing and performant.

You can check out the playground here.