views:

157

answers:

7

I need to maintain & improve an existing website and I am drowning in the redundancy I have discovered in its CSS stylesheet. Given the existing redundancy and non-methodlogical ordering of elements, it's hard to track and predict how a minor change will propagate through the system, or where to apply a change to achieve a certain effect, without spending a lot of time re-learning the CSS file and experimenting.

I have been using the latest Firebug and "Web Developer" add-ons for Firefox, but clearly they are not sufficient for me. I need a tool that can tell me where redundant "overrides" are and perhaps suggest a better cascading scheme.

In other words, help me generate the shortest CSS file that provides the same exact visual functionality that I have right now.

To further clarify, I am not looking for a tool that replaces "#000000" with "#000", "0.5em" with ".5em", "white" with "#FFF", etc. (this addresses "number of characters" redundancy, but doesn't address the "cascading logic" redundancy). I am looking for a tool that can tell me, for example, that a "font-size: 10px" attribute of a child element is redundant because it is already inherited from its parent.

A more advanced feature: "color: #000000" attribute of a class or an id is redundant because it is not used in any of the HTML/PHP files on the website.

Is there a tool that does this kind of "normalization" automatically?

A: 

You can try YUI Compressor. Works for CSS and Javascript files.

Guilherme Oenning
The YUI compressor is more of a minifier, and its workings are explained [here](http://developer.yahoo.com/yui/compressor/css.html). It doesn't do what the OP is asking for.
NullUserException
You're right. Thanks for the info. YUI is just going to give you the shortest CSS file, but will not remove redundancy.
Guilherme Oenning
A: 

You can try out CSS Tidy (online version). It will reduce redundancy, optionally convert to shorthand styles, and can be used to create minified output and pretty/formatted output.

bdukes
Thanks for the link to the online version of CSS Tidy. This tool, however, doesn't remove redundancy in the sense that I outlined above. I just tried it and all it does is replace "#000000" with "#000", "0.5em" with ".5em", "white" with "#FFF" -- which is clearly not what I want. It addresses "number of characters" redundancy, It doesn't address the "cascading logic" redundancy. I am looking for a tool that will tell me, for example, that a "font-size: 10px" attribute of a child element is redundant because it is already inherited from its parent.
Android Eve
not really what he was asking
Gary
It will take care of logical redundancy within a stylesheet (i.e. it will combine styles and eliminate duplicate declarations of the same style). However, it won't be able to determine redundancy from the structure of your HTML, that's true.
bdukes
+1  A: 

I'm not aware of such a tool and looking at the complexity of CSS, I'm not even sure it is possible to solve this (some CSS styles are just there to whack older browsers into compliance). So how much value does a tool have that creates a new W3C-compliant CSS which doesn't render on most browsers because of bugs?

That said, you should have a look at Envjs which allows you to read a web page in an emulated browser and then examine the result. Not perfect but might get you started.

Aaron Digulla
Such hacks should either not be used at all or, if absolutely necessary, quarantined in their own stylesheet, so I don't believe they negate the usefulness of such a tool.
Bobby Jack
+2  A: 

Check out SASS, it's a CSS compiler with its own language and syntax, but it has CSS import capability. http://sass-lang.com/

I wonder if just an import/export cycle would do the trick for you?

Edit: Looks like an import/export cycle isn't enough, but SASS provides you with facilities to make your life easier while refactoring.

Gary
In addition to Sass, use Compass to manage your stylesheet project. It handles the build configuration and has a library of useful CSS idioms to shorten up your source code. A simple import/export will not change your CSS. You need to refactor it into more reusable parts using Sass's mixin and extend features.
Andrew Vit
+2  A: 

I think you are looking for the holy grail of CSS. I don't think it exists. The problem being that you never know exactly what the browser is going to render and need in your stylesheet. I have used Dust-me Selectors plugin for Firefox to help locate unused styles, but to automatically do it is a tall task.

That said, I think that using a framework like SASS (as already mentioned) or LESS is a round-about answer to your question. The frameworks eliminate a lot of the redundancy and and make your CSS compact. I like LESS over SASS because of the similarities to CSS.

Aaron D
Compare to http://stackoverflow.com/questions/3518740/less-sass-css-opposite-from-minification-optimizations
NullUserException
+1  A: 

Aaron makes a good point about the complexity. For example, such a tool would need access to not only the CSS files, but also the HTML (it wouldn't be able to determine things like inheritance, otherwise). Are you then looking for something that will spider an entire site and take every page into account, or can the markup be provided from files?

It's also likely that such a tool would produce CSS that is less maintainable than a decent web designer would produce (I'm thinking shortening selectors which may then just need to be 're-lengthened' at a later date). That's no problem if this is just a temporary action, but I wondering why you would need to do this in the first place.

Bobby Jack
+1  A: 

For finding duplicate/unused/unnecessary CSS based on your markup, you can try out WARI, or Dust-Me Selectors (as @Aaron D mentioned), or CSS Crunch. The last two are browser extensions (for Firefox and IE, respectively), which will only look at one page, while WARI is intended to look at an entire site. However, I've not had much luck getting WARI to work.

bdukes