January 6, 2006

Class Aggregation in CSS

Filed under: HowTo, CSS, Web Design | Lindsay @ 8:55 am

CSS is “teh awesome”. Being able to control the look and feel of an entire website with one file while simultaneously keeping your markup clean and creating a separation between presentation and code is a wonderful thing. But sometimes, especially for beginners, CSS itself isn’t necessarily used or written in the most efficient way. There’s a powerful feature in CSS that I don’t see a lot of people using or talking about and I thought I’d share it since it’s been useful to me in several projects.

Most people know about grouping: you can write your CSS classes with multiple selectors if they will share the same attributes.

body, div, p, td, .heading, #content {
        padding: 0;
        margin: 0;
        background-color: #fff;
}

Grouping can be useful and help keep your CSS from getting bulky with duplicate classes, but there’s an opposite approach that can also reduce duplication of code while providing some other benefits.

I don’t know if there’s an actual name for this practice but I call it “aggregation”. You may create CSS classes for specific attributes and apply them simultaneously to your HTML elements. The styles will aggregate and all of them will then display on the element:

.bluefont { color: #009; }
.padleft { padding: 0 0 0 10px; }
.yellowback { background-color: #ffd; }
.bottomborder { border-bottom: solid 1px black;}

You can apply the classes to HTML elements by separating them with spaces in the class attribute.

<div class=“bluefont yellowback”>This text has a blue font and yellow background.</div>
<div class=“bottomborder padleft”>This text is padded 10px on the left and has a border on the bottom</div>
<div class=“bluefont yellowback bottomborder padleft”>This text has all of the above</div>

The above code will result in something like this:

This text has a blue font and yellow background.
This text is padded 10px on the left and has a border on the bottom
This text has all of the above

Why is this useful? Because you can separate things like changing the colors of an element from it’s layout information and only apply the styles to the element as needed.

Say you have a stylesheet that you want to keep all the layout information the same (margins, paddings, font sizes, positions, etc..), but just change the colors of your backgrounds and fonts. If the only place you declare your color attributes is in your special classes you only have to change the colors there instead of hunting down and changing every class you used “color: #a508fd;”.

Creating special classes to aggregate can also make your HTML more readable for yourself or whomever may come along after you to maintain your site (well, depending on how you name your special classes!). You don’t necessarily know from just looking at the HTML what the class “myspecialheader” will cause your element to look like but you have a good idea how an element with the class “bigfont bluefont padleft” will display.

Another benefit is that you can apply styles in special situations without necessarily having to create a class just for that exception. For instance if you have a table that is styled with a padding of 5px on all TD elements but you need the first column to have extra padding on the left, you don’t have to declare a special class just for that one situation if you’ve already got a “padleft” class in your CSS. Apply it only to that first TD and you’re set.

The one caveat to styling with aggregation is that you have to be careful that you really segregate the attributes you will be putting in your special classes. If you use two classes with the same attribute there will be conflicts (such as using two classes that both have “color” defined).

.header { color: #900; border-bottom: 1px solid #000; }
.bluefont { color: #009;}

But these conflicts can be overcome by paying attention to the scope of your classes.

<div class=“header bluefont”>This is a header with a conflict, one of the color styles is ignored</div>
   <div class=“header”><div class=“bluefont”>This is still a header but had a blue font because of the scope.</div></div>
This is a header with a conflict, bluefont is ignored
This is still a header but had a blue font because of the scope.

Setting up your CSS for aggregation does require a bit of planning, but if done correctly it can make your CSS efficient and both your CSS and HTML easier to maintain in the long run. It’s an approach worth trying if you haven’t before. I hope you might find it as useful as I have.

» » » » » » » »
, , , , , , ,

7 Responses to “Class Aggregation in CSS”

  1. TAD Says:

    This is so nerdy it makes my head hurt. Will you have my babies? Oh, wait…

  2. Lindsay Says:

    Well, too late for that! Already had one!

  3. MikeL Says:

    I had this very thought a few minutes ago. I was reading all this stuff about cascades and “inheritance vs redundancy” and I just wondered where I had seen this kind of thing before. Suddenly I remembered - “Design Patterns Explained” an OOP book(Shalloway & Trott). The discussion was all about the enormous confusion and bloat you get if you keep overriding in child classes and speculates how there must be a better way. In the end the solution pointer is “favour aggregation over inheritance” and suddenly there is all this decoupling and the worst of the problem just evaporates away!

    Now I am a bit slow so I’m going to reread my old book and see if this really could be the key to a clean CSS construction method, but after downloading somebody’s “professional” css for a large site I suspect that reengineering along these “aggregation” lines would reduce its size, maintainability and complexity enormously.

    Mike

  4. Declan Says:

    I need some help with the Asktheseguys wordpress theme..
    I can't figure out how to get the header and ad bar at the top to go all the way across the screen.. Or better yet, how I can place an ad box in the empty space at the top right.
    I can usually figure this stuff out easily, but can't find anything in the CSS that will make it work..
    Anyone have any ideas?

  5. Private Krankenversicherung Says:

    Very goood blog and design. I wish good luck from Private Krankenversicherung

  6. Web Design Glasgow Says:

    I love both the grouping and aggregation concepts and will be putting them to use, my css files are rather large at times and any reduction in css and html code is welcome.

    Do not forget; more than often, you need seperate css files for different browsers.

    regards
    George

  7. DoggyDude Says:

    Glad you wrote this. I’m currently trying to find a way to combine class names in a CSS header file (e.g. something like .new_look = old_look + red_strip where old_look and red_stripe are also classes). I haven’t yet found if this is possible.

Leave a Reply