Tool to Recreate a Page’s CSS and Reduce it to Minimum Styles Used

I recently got a question on my Instagram from someone struggling with a bloated CSS file on their website. They were asking if there’s a way to recreate a page’s CSS and reduce it to only the minimum styles actually used. This is something I’ve seen quite a few developers grapple with, so I thought I’d address it in a blog post. Let’s explore an example of what could go wrong and how to solve it.

1. Example of What Can Go Wrong

Imagine you have a massive CSS file with hundreds of lines of code. You suspect that only a small portion of these styles is actually being used on a particular page, and you want to trim down the file. You might think of using a simple approach like this:

code<!-- HTML File -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Page</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
.container {
width: 100%;
max-width: 1200px;
margin: 0 auto;
}
.header, .footer {
background-color: #333;
color: #fff;
}
.header h1 {
font-size: 2rem;
}
.content {
padding: 20px;
}
.unused-class {
display: none;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Welcome to My Website</h1>
</div>
<div class="content">
<p>This is a paragraph of content on my page.</p>
</div>
<div class="footer">
<p>Footer Content</p>
</div>
</div>
</body>
</html>

In this example, you might think that manually cutting down the styles like this would solve the issue, but it’s not efficient and prone to errors. There might be unused styles that you miss, or you might accidentally remove something important.

The Correct Solution: Using a Tool to Automatically Reduce CSS

The best way to handle this is by using a tool like PurgeCSS or UnCSS. These tools scan your HTML files and remove any unused CSS automatically, leaving you with only the styles that are actually being applied to your page.

Here’s how you can correctly use PurgeCSS with a simple setup:

  1. Install PurgeCSS via npm: codenpm install purgecss --save-dev
  2. Create a PurgeCSS Config File:Create a purgecss.config.js file to specify which files to scan. codemodule.exports = { content: ['./*.html'], // Specify the HTML files css: ['./css/style.css'], // Specify the CSS files output: './css', // Output folder for the cleaned CSS file };
  3. Run PurgeCSS:After setting up the configuration, you can run PurgeCSS from the command line: codenpx purgecss --config ./purgecss.config.js This will output a cleaned CSS file that only includes the styles actually used on your page.
  4. Check Your Reduced CSS:The newly generated CSS file will be much smaller and more efficient, containing only the necessary code/* Reduced CSS Output */ body { margin: 0; padding: 0; background-color: #f4f4f4; } .container { width: 100%; max-width: 1200px; margin: 0 auto; } .header, .footer { background-color: #333; color: #fff; } .header h1 { font-size: 2rem; } .content { padding: 20px; }

Conclusion

If you’re dealing with bloated CSS files, manually reducing styles might seem like a quick fix, but it’s not reliable. Instead, tools like PurgeCSS or UnCSS are your best friends. They help automate the process, ensuring that your CSS files are as efficient as possible. Not only does this speed up your website, but it also makes maintaining your styles much easier.

If you’ve got any more questions or tips on CSS optimization, feel free to reach out on Instagram or drop a comment below. I’m always here to help!

Related blog posts