How to find unused classes in my HTML?

This is a partial answer on how to get the class names in html

var classesEvery = [];
var elementsEvery = document.querySelectorAll('*');
for (var i = 0; i < elementsEvery.length; i++) {
  var classes = elementsEvery[i].className.toString().split(/\s+/);
  for (var j = 0; j < classes.length; j++) {
    var thisClass = classes[j];
    if (thisClass && classesEvery.indexOf(thisClass) === -1)
      classesEvery.push(thisClass);
  }
}

One can get the Class names in the html file using this Javascript code. For getting all classes used in CSS, try list-selectors. Still thinking on how to get class names used in Javascript angularJS. Added a working snippet with random HTML classes to test the js.

var classesEvery = [];
var elementsEvery = document.querySelectorAll('*');
for (var i = 0; i < elementsEvery.length; i++) {
  var classes = elementsEvery[i].className.toString().split(/\s+/);
  for (var j = 0; j < classes.length; j++) {
    var thisClass = classes[j];
    if (thisClass && classesEvery.indexOf(thisClass) === -1)
      classesEvery.push(thisClass);
  }
}
console.log(classesEvery);
.hidden {
  display: none;
}
<!-- Some random HTML code. -->
<div ng-controller="HomeController" class="container hidden">

  <span>Simple Notifications:</span>
  <div class="rows">
    <div class="col-md-2"><button class="btn btn-primary" href ng-click="simple()">Simple notification</button></div>
    <div class="col-md-2"><button class="btn btn-warning" href ng-click="warning()">Warning notification</button></div>
    <div class="col-md-2"><button class="btn btn-success" href ng-click="success()">Success notification</button></div>
    <div class="col-md-2"><button class="btn btn-danger" href ng-click="error()">Error notification</button></div>
    <div class="col-md-2"><button class="btn btn-grimace" href ng-click="wait()">Wait notification</button></div>
    <div class="col-md-2"><button class="btn btn-primary" href ng-click="pop()">Link to other page</button></div>
  </div>
  <hr/>
  <span>Addding Option from Script:</span>
  <div class="rows">
    <div class="col-md-6">
      <Span>With CLose button</span>
      <button class="btn btn-primary" href ng-click="close()">Close Button </button>
    </div>
    <div class="col-md-6">
      <Span>Fade after 1 Sec</span>
      <button class="btn btn-primary" href ng-click="timeout()">Html notification</button>
    </div>
  </div>
  <br/>
  <hr/>
  <span>Custom Notification and Body output type:</span>
  <div class="rows">
    <div class="col-md-3"><button class="btn btn-primary" href ng-click="Custom_temp()">Custom template</button></div>
    <div class="col-md-3"><button class="btn btn-primary" href ng-click="trusted_html()">Trusted HTML</button></div>
    <div class="col-md-3"><button class="btn btn-primary" href ng-click="default_temp()">Default Template</button></div>
    <div class="col-md-3"><button class="btn btn-primary" href ng-click="file_custom_temp()">Including file from Folder</button></div>
  </div>
  <script type="text/ng-template" id="myTemplate.html">
    <div class="Custom_temp_html">
      <p>Notice, custom temlate is not in the list</p>
    </div>
  </script>
</div>

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.


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).