
December 24, 2011
June 24, 2011
January 24, 2011
December 19, 2010
June 2, 2010
April 22, 2010
April 2, 2010
April 1, 2010
March 30, 2010
February 12, 2010
February 4, 2010
January 29, 2010
February 12, 2010
Write less CSS and build in consistency using the Java principle of the superclass.
There’s a concept in Java that I realized I was already using when I write my CSS for a particular project. It’s known as a superclass.
Take a circle for instance. What is the nature of a circle? It’s a shape. Shape would be the superclass of circle. Circle would be the subclass of shape. This is basically the idea of going from something general to something increasingly specific.
Let’s say I’m working on a site that has multiple listings of property types, like golf courses, hotels and resorts, all mixed together in a search results type situation.
I could write my HTML like so:
<div id="results"><div class="course listing">...</div><div class="hotel listing">...</div><div class="resort listing">...</div></div>The .listing class would be like a superclass.
.listing {margin: 1em 0;padding: 1em;border: 1px solid #eee;font-family: georgia, times, serif;background: url("../graphics/bkg_product_listings.gif") repeat-x;}Since I know each listing is going to have a common set of declarations, I like to roll those up in one generic class. Then for each specific kind of listing, I’ll write the declarations separately. Only declarations that are unique for that type will be applied to that class.
.course {color: #360background: url("../graphics/bkg_course.gif") repeat-x;}.hotel {color: #666;background: #666 url("../graphics/bkg_hotel.gif") repeat-x;}.resort {color: #333;background: #999 url("../graphics/bkg_resort.gif") repeat-x;}This allows me to avoid duplicating the declarations like so:
.course {margin: 1em 0;padding: 1em;border: 1px solid #eee;font-family: georgia, times, serif;color: #360background: url("../graphics/bkg_course.gif") repeat-x;}.hotel {margin: 1em 0;padding: 1em;border: 1px solid #eee;font-family: georgia, times, serif;color: #666;background: url("../graphics/bkg_hotel.gif") repeat-x;}.resort {margin: 1em 0;padding: 1em;border: 1px solid #eee;font-family: georgia, times, serif;color: #333;background: url("../graphics/bkg_resort.gif") repeat-x;}Of course, if I want each listing to have slightly different padding, or a different kind of border, then it makes sense to write them separately. But if I want consistency, it doesn’t make sense to have all this duplicate CSS, especially if I want to make a change to all listings, then I have to remember to change the same CSS three times.
If I have a set of display items that seems to be similar to one another, I always try to extract what is common for those items and come up with a generic class that describes the nature of those items.
For example: .listings, .results, .icons, .subheaders
I never want to duplicate my CSS declarations over and over, and if I find myself doing that, I know I need to step back and rethink what I have so far. Thinking abstractly will lead to better written CSS that is leaner and easier to add to in the future.
