Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find unused classes in my HTML?

There are lots of tools for finding unused CSS rules in stylesheets, such as Chrome Audits and the Dust-Me Selectors add-on for Firefox (unfortunately not compatible with Firefox Quantum).

But what about the other way around?

How do I find classes that are in my HTML, that do not exist in my stylesheets?

like image 775
jennas Avatar asked Jul 23 '13 01:07

jennas


People also ask

How do I find unused CSS classes?

Open Chrome DevTools. Open the command menu with: cmd + shift + p. Type in "Coverage" and click on the "Show Coverage" option. Select a CSS file from the Coverage tab which will open the file up in the Sources tab.

How do I find unused CSS in HTML?

The Coverage tab in Chrome DevTools can help you find unused JavaScript and CSS code. Removing unused code can speed up your page load and save your mobile users cellular data.

How do I identify a class in HTML?

Open the page in a browser, like Chrome or Firefox, right click and inspect the element, You should see the source, there you can see the class.

How do I see all classes in CSS?

Focus that input and hit Ctrl + Space. A pick list of all class styles in the current opened document should appear.


2 Answers

This may be helpful: https://code.google.com/p/find-unused-classes/ . According to the description:

It shows classes that exist in css selectors and do not exist on html page and like-verse.

As Jim said, be warned that some classes may be unused by your stylesheets but still used in JavaScript.

like image 90
BadgerPriest Avatar answered Oct 06 '22 00:10

BadgerPriest


HTML Inspector from https://github.com/philipwalton/html-inspector has this as one of its features:

  • Unused Classes: Sometimes you'll remove a CSS rule from your stylesheet but forget to remove the class from the HTML. The "unused-classes" rule compares all the class selectors in the CSS to the classes in the HTML and reports any that aren't being used.

    Classes that are in the HTML as JavaScript hooks can be ignored via a whitelist. By default, any class prefixed with js-, language-, or supports- is whitelisted. More information on the rationale behind this rule can be found here.

In its FAQ you will find a bookmarklet.

Or add it via a script tag:

<script src="http://cdnjs.cloudflare.com/ajax/libs/html-inspector/0.8.2/html-inspector.js"></script> 

And run with this snippet:

HTMLInspector.inspect(["unused-classes"]); 

A comment by Andrew Grimm points out that this project is no longer maintained (last commit December 2017). Fortunately it still seems to be functional (tested June 2019).

There is also grunt-unclassify but that project seems dead (last commit December 2014).

like image 44
phk Avatar answered Oct 06 '22 00:10

phk