CSS Redundancy Checker
Software, Web Development July 24th, 2007 - 9,919 views
While I’m primarily a developer, I end up spending a lot of time working with CSS and other front end technologies. I’ve learned a lot about CSS, but I don’t use it enough to have really good habits and often end up with superfluous selectors and bloated code. So when Tom Armitage’s CSS Redundancy Checker popped up in my feed reader I was intrigued. Tom’s solution is simple, elegant, and reasonably quick. But it’s a command line app that requires ruby and some additional packages — not exactly newb (or lazy programmer) friendly. Since I was looking for an excuse to play with Rails a bit more, I decided to write a web front end and build an Online CSS Redundancy Checker on top of Tom’s code.
The checker asks for your stylesheet URI and a list of HTML pages to analyze. The parser scans the specified stylesheet looking for CSS selectors that are not present in any of the HTML pages listed. Since these selectors are not used anywhere, they can probably be removed from your stylesheet without causing any problems.
Before you get too excited, there are some caveats. The redundancy checker does not understand javascript, and may tell you that certain selectors are redundant simply because they don’t appear in your static HTML. If you use javascript to manipulate your HTML documents (e.g., by adding class attributes on hover, etc.) you should be aware of this problem. You may also have selectors that apply to only one or two HTML documents that are buried in some obscure location on your site. So be careful, and think before you delete!
At this point the CSS Redundancy Checker is fairly primitive, with limited input options and no real API. If there’s enough interest I’m willing to add features (e.g., a sitemap parser so you don’t have to input page URIs by hand). If you have any thoughts, suggestions, problems, feature requests, etc. please let me know in the comments. Otherwise, enjoy.
July 25th, 2007 at 12:19 am
[...] Mike ha sacado un servicio bastante útil, cuantas veces no nos pasa que estamos modificando una hoja de estilos mientras desarrollas un diseño y al ir cambiando tus puntos de vista muchas quedan sobrando. [...]
July 25th, 2007 at 1:16 am
Great tool Mike! This will come in handy. I personally don’t think the JS caveat is a big deal since I don’t use much JS on my site anyways.
July 25th, 2007 at 1:16 pm
Good work!! I will test it with my sites!
July 25th, 2007 at 3:29 pm
great stuff mike, looking forward to using it to clean up some css!
July 25th, 2007 at 7:00 pm
This is pretty cool, but it would be nice if you could have it check pages that have the CSS in the website themselves (like blogspot.com).
July 25th, 2007 at 7:11 pm
Hey, i think that you should offer email subscription to your rss feed, because i want to subscribe by email because your blog is great!
July 25th, 2007 at 7:23 pm
Hey Mike, nice tool! One thing that I noticed is that it counts CSS hacks as unused:
# #navbar[id] ul li
# #navbar[id] ul li.first
# .item[class]
These are used but are not literally called in the html document.
July 26th, 2007 at 4:08 pm
Great Tool. thanks!
July 26th, 2007 at 4:15 pm
Thanks for the feedback.
@Andy: I’ve been thinking about doing email subscriptions, but until now nobody has actually requested them… I’ll look into it.
@Dave: I’ll have to look into that for the next version. Large stylesheets are typically external, so inline styles are usually more manageable… but it could be useful.
@Dan: Not sure if there’s an easy way to prevent that from happening. That’s definitely something to look out for, though.
July 26th, 2007 at 4:38 pm
Thanks a bunch! I used it for a site that I’m working on and it worked perfectly!
August 1st, 2007 at 11:41 am
Hello,
I think CSS Redundancy Checker tool is down and not working…
August 1st, 2007 at 11:57 am
Is anyone else having problems..? I just tried it and it worked fine.
August 1st, 2007 at 12:02 pm
Hmm..
to every try with different sites, i got the same error…
” Oops… something bad happened. Are your stylesheet and page URIs valid? “
August 1st, 2007 at 12:15 pm
Found the error.
The tool is not recognising the following line, although the css is valid :
*{margin: 0; padding: 0;}*
August 1st, 2007 at 12:21 pm
Just removed the last *
and working awesome.
Thanks mike, great timesaver tool!
August 6th, 2007 at 12:46 pm
[...] Оригинал здесь. [...]
August 7th, 2007 at 7:59 am
for some reasons i can’t always get the css valid on my joomla site, can you help me?
August 16th, 2007 at 3:31 pm
excellent tool, glad I came across this post of yours!, thanks
August 28th, 2007 at 2:23 am
Hi Mike, splendid work done here !
One proposal to make it more user friendly (??) - possibility to specify a , say, site_uris.txt file as input so you can save them on your website and then run it through your page - even nicer - possibility to browse for the file so i can save it local.
Great tool anyway - thx !
September 5th, 2007 at 2:51 am
Hi, this is a great tool! Tho’ the sitemap parser feature would be great!
September 23rd, 2007 at 11:33 am
[...] http://meiert.com/en/blog/20070621/load-time-the-ux-factor-facts-and-measures/ http://immike.net/blog/2007/07/24/css-redundancy-checker/ PreviousTags: Web HostingCategory: Internet, Technology Get RSS Feed Email This [...]
April 13th, 2008 at 4:14 am
Check out Dust-Me Selectors, a Firefox plugin that automates this process for you.
http://www.sitepoint.com/dustmeselectors/