How do I identify and eliminate unused CSS styles from my bloated stylesheet?
Asked Answered
C

7

34

I have a legacy stylesheet that is now full of unused styles. The problem is identifying the necessary from the unnecessary. Are there any tools to help with this?

Catania answered 26/8, 2010 at 10:17 Comment(2)
possible duplicate of Tool to identify unused css definitionsCystotomy
possible duplicate of How can I find unused images and CSS styles in a website?Preform
R
26

CSS Usage is a great Firefox add-in. You can browse multiple pages and it will work out which rules haven't been used on any of them - so it is more accurate than a tool that scans a single page.

Roter answered 26/8, 2010 at 10:20 Comment(2)
+1 Sounds good - although I suggested Dust-Me, it can be a bit tedious. I'll certainly look into this one.Artemus
+1 for CSS Usage over Dust-Me as it provides a great deal more information.Eastnortheast
A
3

You could try the Firefox Dust-Me Selectors add-on.

Artemus answered 26/8, 2010 at 10:19 Comment(0)
A
2

Install Google's pagespeed plugin for firebug:

http://code.google.com/speed/page-speed/

Then in Firebug, open the 'pagespeed' tab and, with 'performance' selected, click 'analyze performance'.

If you have unused style rules on the present page, then along with lots of other useful suggestions, you will see a list item labelled "Remove Unused Css". Click to expand it and see a breakdown by resource of unused css rules appearing on the present page, along with the memory size that you will save by removing the unused rules.

This is just one tiny feature of the pagespeed toolkit, which you definitely familiarize yourself with if you're at all interested in your page performance on the client side.

You may also be interested in yslow, a similar tool for firebug developed by yahoo.

Antoninaantonino answered 29/12, 2010 at 4:4 Comment(0)
S
1

This tool called, "csscss" removes identifies duplicated styles:

One of the best strategies for me to maintain CSS is to reduce duplication as much as possible. It’s not a silver bullet, but it sure helps.

To do that, you need to have all the rulesets in your head at all times. That’s hard, csscss is easy. Let it tell you what is redundant.

Shockheaded answered 23/4, 2013 at 3:39 Comment(0)
S
1

There is a really handy plugin for Grunt called UnCSS. It will automatically remove unused CSS on the fly. Check out this link for more info:

Remove Unused CSS automatically using Grunt

Shawana answered 5/3, 2014 at 14:42 Comment(0)
S
0

Remove Unused CSS automatically using Grunt

Gruntfile.js

module.exports = function (grunt) {

    grunt.initConfig({
        uncss: {
            dist: {
                files: [
                    { src: 'index.html', dest: 'css/test.css' }
                ]
            }
        },
      cssmin: {
            dist: {
                files: [
                    { src: 'css/test.css', dest: 'cleancss/testmin.css' }
                ]
            }
        }
    });

    // Load the plugins
    grunt.loadNpmTasks('grunt-uncss');
    grunt.loadNpmTasks('grunt-contrib-cssmin');

    // Default tasks.
    grunt.registerTask('default', ['uncss', 'cssmin']);

};
Statics answered 17/4, 2014 at 8:46 Comment(0)
H
0

npm install uncss -g

Then

uncss http://example.com/ > out.css

Hangchow answered 24/7, 2015 at 2:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.