Object Oriented CSS for ChanLu

blog-oocOn my workplace, we had one major site that had a redesign, we – the designers, agreed on applying the Object Oriented way on our CSS files. We didn’t want to repeat the same mistakes all over again with our other huge, messy and unmanageable CSS files.  By applying the Object Oriented CSS, the site that we redesigned is more manageable, having cleaner and prettier code.

Using ChanLu Design I will practise on using this method, since during the redesign process, I was not assigned to be one of the coders, I ended up creating mock-ups for the individual pages of the site.

On applying OOC, you can get rid of unnecessary classes and reuse other classes. Here’s a perfect example of a good way in using OOC by Matthew Anderson that was posted on Jeff Croft’s blog post about OOC:

Messaging in applications is a great example. Your success, informational and error messages should have a similar look and feel. Using classes like “success message”, “info message” and “error message” are no less descriptive and no less communicative than “success-message”, “info-message” and “error-message”. However, they are a lot more maintainable and you accomplished it with less code.

This means that the structure of the element would go on the “message” class. Like messages are a block element having a padding of 10px. Their differences would most likely be on font colors, backgrounds and icons. These maybe placed on new classes that will contain their look and design. Usually well-known websites have their success messages as green with a check icon, info/notice messages to indicate something to think about are usually colored yellow with an exclamation icon while red with an X icon for something critical for the error messages.

The basic concept of OOC is separating the structure from the skin.

Sample Code without OOC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
.success-message{
display: block;
padding: 10px;
font-size: 1em;
color: green;
background: url('../images/success.gif') no-repeat;
} 
 
.info-message{
display: block;
padding: 10px;
font-size: 1em;
color: yellow;
background: url('../images/info.gif') no-repeat;
}
 
.error-message{
display: block;
padding: 10px;
font-size: 1em;
color: red;
background: url('../images/error.gif') no-repeat;
}

Sample Code with OOC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
.message{
display: block;
padding: 10px;
font-size: 1em;
}
 
.success{
color: green;
background: url('../images/success.gif') no-repeat;
} 
 
.info{
color: yellow;
background: url('../images/info.gif') no-repeat;
}
 
.error{
color: red;
background: url('../images/error.gif') no-repeat;
}

The messages do have the same structure, all they have are different appearances. That’s why one of the designers in my workplace said “It’s just like dressing up”. Whether you put on a hat, or glasses perhaps. Your still clothing a person. Sizes or colors may differ, but they still have the same structure. By separating the structure from the skin/design, we can reuse code and make our css files more maintainable.

Related posts:

  1. WIO Screenshots

6 Responses to “Object Oriented CSS for ChanLu”


  • She looks good though, her eyes shine in a good way. Very anime-like.

  • Yet another pwnsome piece of art by Charlie ;) Who’d guess? ;D

    According to your article, I guess the OOC method is yet another useful CSS file “charlady” ? ;) I may use it one day, probably when coding another DH layout ;]

  • Great article. It’s always better to have CSS that is efficient, concise, and lacking redundancy.

    On the other hand, I like how her eyes seem to shine in your sketch.

    • This isn’t really an article though, it’s more like a post to share the word. But yes, what you said is true, though it isn’t that much of a big deal on ChanLu (since it’s a small site), I just want to practice on using the OOC method.

      Thanks about the sketch, does her eyes shine that much? Hehe. She has a broad shoulders though, I should have made her into the typical petite anime girl.

  • Whenever I start coding, I always plan to write clean CSS code but ended up in a mess. I should start applying this method.

    Great post and the sketch too. :*

    • Haha. You should!
      Nothing is really great about it, I just explained the thing to spread the word more.
      Thanks about the sketch, though I still don’t find myself to like it…

Leave a Reply