{"id":342198,"date":"2021-06-15T16:24:20","date_gmt":"2021-06-15T23:24:20","guid":{"rendered":"https:\/\/css-tricks.com\/?p=342198"},"modified":"2021-06-15T16:36:30","modified_gmt":"2021-06-15T23:36:30","slug":"detect-unused-classes-in-html","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/detect-unused-classes-in-html\/","title":{"rendered":"Detect Unused Classes in… HTML"},"content":{"rendered":"\n

Usually, when “unused” comes up in conversation regarding CSS, it’s about removing chunks of CSS that are not used in your site or, at least, the styles not currently in use on a specific page. The minimal amount of CSS is best! I’ve written about how this is a hard problem<\/a> in the past. In JavaScript-land, the equivalent is tree shaking<\/a> (removing unusued JavaScript).<\/p>\n\n\n\n

But what about the other way around, detecting classes in HTML<\/strong> that aren’t used in your CSS? If you knew this for sure, you could clean up your markup, removing classes that don’t do anything.<\/p>\n\n\n\n\n\n\n\n

I saw Robert Kieffer post a Gist<\/a> the other day with an interesting solution. The idea is to load up document.styleSheets<\/code> and find all<\/em> the rules (the ones that are classes). Then, use a MutationObserver<\/code> to watch the DOM for all HTML, and check the classList<\/code> of each node to see if it matches any from any stylesheet. If the HTML has a class not found in a stylesheet, report it. <\/p>\n\n\n\n

I gave it a quick whirl and got it working and correctly reporting unused classes:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Your mileage may vary. For one thing, this script is set up as an ES Module. That means if you just import<\/code> it and run it on a regular ol’ HTML document, it won’t find anything because your <script type=\"module\"><\/code> is deferred and the MutationObserver<\/code> won’t pick anything up. I just un-moduled it and put it in the <head><\/code> to make my demo work.<\/p>\n\n\n\n

I Netlify Dropped<\/a> the site online<\/a> in case you wanna dig into it and check it out. I would have used CodePen, but CodePen doesn’t link up your styles as <link><\/code>ed stylesheets (by default, but you could use external resources to do that). I just thought it would be more clear as a deployed site.<\/p>\n\n\n

Careful now.<\/h3>\n\n\n

Just like unused CSS is a hard problem because of how hard it is to know for sure know if a ruleset is unused, this approach may be an even harder<\/em> problem. For one thing, classes might be used as a JavaScript hook for things. Styles might get injected onto the page in <style><\/code> blocks, which this script wouldn’t check. Heck, you might have integration tests that run in CI that use classes to do testing-related things. <\/p>\n\n\n\n

I’d say this kind of thing is a useful tool for havin’ a looksie at classes that you have a hunch might be unused. But I wouldn’t say there’s a permission slip to run this thing and then yank out every reported class without further investigation.<\/p>\n","protected":false},"excerpt":{"rendered":"

Usually, when “unused” comes up in conversation regarding CSS, it’s about removing chunks of CSS that are not used in your site or, at least, the styles not currently in use on a specific page. The minimal amount of CSS is best! I’ve written about how this is a hard problem in the past. In […]<\/p>\n","protected":false},"author":3,"featured_media":272552,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[4],"tags":[1443],"jetpack_publicize_connections":[],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2018\/06\/unused-css.png?fit=1200%2C600&ssl=1","jetpack-related-posts":[{"id":167005,"url":"https:\/\/css-tricks.com\/spring-cleaning-unused-css\/","url_meta":{"origin":342198,"position":0},"title":"Spring-cleaning Unused CSS","date":"March 28, 2014","format":false,"excerpt":"Addy Osmani intros a Grunt task for removing unused CSS from your stylesheet before serving it. While this is certainly a worthy goal, I look at the quotes from people who reduced their stylesheets by 3\/4 or more and I'm like holy crap what kinda shop are you running over\u2026","rel":"","context":"In "Link"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":360928,"url":"https:\/\/css-tricks.com\/css-me-not-bookmarklet\/","url_meta":{"origin":342198,"position":1},"title":"Using the CSS Me Not Bookmarklet to See (and Disable) CSS Files","date":"January 14, 2022","format":false,"excerpt":"Stoyan is absolutely correct. As much as we all love CSS, it's still an important player in how websites load and using less of it is a good thing. He has a neat new bookmarklet called CSS Me Not to help diagnose unnecessary CSS files, but we'll get to that\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/01\/CleanShot-2022-01-09-at-09.25.43@2x.png?fit=1200%2C890&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":332768,"url":"https:\/\/css-tricks.com\/animxyz\/","url_meta":{"origin":342198,"position":2},"title":"AnimXYZ","date":"January 18, 2021","format":false,"excerpt":"There are quite a few CSS animation libraries. They tend to be a pile of class names that you can apply as needed like \"bounce\" or \"slide-right\" and it'll... do those things. They tend to be pretty opinionated with nice defaults, and not particularly designed around customization. It looks like\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/01\/animxyz.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":312407,"url":"https:\/\/css-tricks.com\/analyzing-notion-app-performance\/","url_meta":{"origin":342198,"position":3},"title":"Analyzing Notion app performance","date":"June 8, 2020","format":false,"excerpt":"Here\u2019s a fantastic case study where Ivan Akulov looks at the rather popular writing app Notion and how the team might improve the performance in a variety of ways; through code splitting, removing unused vendor code, module concatenation, and deferring JavaScript execution. Not so long ago, we made a list\u2026","rel":"","context":"In "Link"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/06\/notion.png?fit=1200%2C790&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":348288,"url":"https:\/\/css-tricks.com\/we-had-90-unused-css-because-everybody-was-afraid-to-touch-the-old-stuff\/","url_meta":{"origin":342198,"position":4},"title":"“We had 90% unused CSS because everybody was afraid to touch the old stuff”","date":"August 18, 2021","format":false,"excerpt":"Over at the JS Party podcast: [Kend C. Dodds]: [...] ask anybody who\u2019s done regular, old CSS and they\u2019ll tell you that \u201cI don\u2019t know if it\u2019s okay for me to change this, so I\u2019m gonna duplicate it.\u201d And now we\u2019ve got \u2013 at PayPal (this is not made up)\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2018\/06\/unused-css.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":351562,"url":"https:\/\/css-tricks.com\/just-in-time-css\/","url_meta":{"origin":342198,"position":5},"title":"“Just in Time” CSS","date":"September 10, 2021","format":false,"excerpt":"I believe acss.io is the first usage of \"Atomic CSS\" where the point of it is to be a compiler. You write CSS like this:

text <\/div> And it will generate CSS like: .C\\(\\#333\\) { color: #333; } .P\\(20px\\) { padding: 20px; } (Or something like that.)\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-08-at-8.59.19-AM.png?fit=1200%2C1021&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]}],"featured_media_src_url":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2018\/06\/unused-css.png?fit=1024%2C512&ssl=1","_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/342198"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=342198"}],"version-history":[{"count":6,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/342198\/revisions"}],"predecessor-version":[{"id":342349,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/342198\/revisions\/342349"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media\/272552"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=342198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/categories?post=342198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=342198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}