Skip to content
md.mudassir
← all writing

· 2 min read · originally on LinkedIn ↗

How I removed hundreds of unused CSS lines quickly in React

After many design iterations my stylesheet was full of dead rules. A free tool, UnCSS, stripped them in three steps without breaking the new design.

cssreactperformance

After many design iterations on a web page, the CSS kept piling up. It got confusing to tell which styles were still in use and which weren't, and the stylesheet kept growing in size. I wanted to remove the unused CSS without messing up the new design.

After some googling I came across a handy online tool called UnCSS that does the job quickly and efficiently.

The process is simple, and it works for any page, not just a React app. Here's how it goes on an example app.

1. Edit the root element as HTML

Open the browser's dev tools, inspect the page and find the element with the id root.

The #root element in Chrome DevTools

Right-click it and select Edit as HTML.

Edit as HTML in the element's context menu

2. Copy the contents

Select everything in the editor and copy it. That's the rendered markup of your page.

The root element's markup, ready to copy

3. Let UnCSS remove what isn't used

Open UnCSS. Paste the copied HTML into the HTML section, then copy the CSS from your stylesheet into the CSS section, and click UNCSS MY STYLES. It generates only the CSS the page needs, with every unused rule removed.

UnCSS with the HTML and CSS pasted in, and the shortened CSS below

Written by Mohammed Mudassir, senior software engineer in Bengaluru. Turning ideas into scalable products.

say hello

Got an idea that should be a product?

I'm always up for a good product conversation — a role, a collaboration, or just comparing notes on shipping. Open to interesting product conversations.

I usually reply within a couple of days.

Local time
— · Bengaluru, IST
Status
Open to interesting product conversations