Lately I’ve been concentrating on learning more and more about website performance, mainly on the Frontend side. I’d like to share a recent finding that I thought was rather interesting: Object Oriented CSS. OOCSS itself is a philosophy for writing Frontend code, focused on rapid development, efficiency of code, and maintainability. Recently, OOCSS Grids was created as an HTML/CSS framework to demonstrate these points in action and to give developers an ideal starting point, beyond a simple CSS reset.
Background
The OOCSS (Object Oriented CSS) Grids project is aimed at providing a framework for developers to rapidly produce performance oriented, easily manageable layout structures for their websites. OOCSS Grids was created by Nicole Sullivan, a Frontend Engineer and former member of Yahoo’s Exceptional Performance Team. Nicole is also cocreator of the image optimization tool smush.it, which I highly recommend. OOCSS Grids is loosely derived from YUI Grids.
Principles
As noted on the project’s Github page, OOCSS Grids has two key principles in mind:
- Separate structure and skin
- Separate container and content

These principles focus on keeping the page as flexible as possible by breaking down areas of the page into objects which use the same base markup for their containers. When an object requires special styling, it is easily extended by adding additional classes to the container element. This is ideal in a number of ways, perhaps the most important because it forces code to be produced in a much more predictable fashion. In the long term this will greatly assist Backend developers, content creators and designers in obtaining a concrete understanding of how the Frontend side of your site works. The idea being that team members who understand not only their own process, but also the process of those around them, will accomplish their tasks faster and more efficiently, while being measured against higher standards. A win for everyone.
A good question to ask right about now would be “won’t doing it this way produce a lot of extra, unnecessary markup?” The answer is yes and no. The extra markup will indeed make load times a little longer, but it is necessary to give flexibility for both current and future layout needs. For instance, what if a client wanted a sidebar container to suddenly have slick outer drop shadows along the border? Well, depending on the site’s current markup that could be a really tricky task to restyle, possibly involving new markup and a lot of QA for such a small task. However, with an OOCSS Grids implementation in place, it would be as trivial as extending our container element of class “mod” to include the class “complex”, which has styles to accommodate for similar border types. Next you would need to add and style a custom class, for example “tricky_shadow”, with some easy customizations, upload any necessary images and it’s good to go. Here is an example, based on the OOCSS Grids Modules page currently in the Git repository:
Before
<div class="mod">
<b class="top"><b class="tl"/><b class="tr"/></b>
<div class="inner">
<h3>Some Module</h3>
<p>Lorem ipsum dolor sit amet...</p> </div>
<b class="bottom"><b class="bl"/><b class="br"/></b>
</div>
After
<div class="mod complex tricky_shadow“>
<b class="top"><b class="tl"/><b class="tr"/></b>
<div class="inner">
<h3>Some Module</h3>
<p>Lorem ipsum dolor sit amet...</p>
</div>
<b class="bottom"><b class="bl"/><b class="br"/></b>
</div>
What our custom CSS might look like
.tricky_shadow .inner {
border:solid 10px #fff;
margin:0 4px;
}
.tricky_shadow b {
background-image:url(images/shadow.png);
}
.tricky_shadow .tr,
.tricky_shadow .tl {
width:20px;
}
.tricky_shadow .top,
.tricky_shadow .bottom {
height:4px;
margin-top:0px;
}
.tricky_shadow .bl,
.tricky_shadow .br {
height:16px;
margin-top: -12px;
}
Note: remember that the “mod” and “complex” styles would be already have been predefined for the basic layout of the container.
Performance
So, where does the promised performance benefits come in to play? Well, duh, it comes in the form of constantly reusing the same base classes, thus minimizing the amount of CSS needed and maximizing the utilization of the browser’s ability to cache and reuse commonly occurring elements. To delve further, because every page will be using the same basic location independent styles, the browser will have less unique objects to render. It will render the generic objects and extend them only when necessary, resulting in pages that appear faster.
To help demonstrate, below is a visual representation of the Mozilla.org homepage being flowed into the browser. While it isn’t using OOCSS Grids, it’s still utilizing some of the same practices, because after all, it is Mozilla. Pay special attention to the way the columns are being created.
An added bonus gained from these location independent styles is the reduction in the impact reflows have. A reflow is when the browser repositions all child elements as well as any elements following it in the DOM. It can be triggered a number of ways, notably when JavaScript is used to change a class or style for an element. Knowing this, it is best to be as specific as possible and target only what you need to change when using JavaScript in this manner. Because OOCSS states all of the elements should be styled based on either their own class or those of their immediate (or very close) ancestors, reflow effects are kept to a minimum.
Recap
Pros
- Faster development times
- Graded browser support
- Easier to comprehend what the HTML is doing
- Markup that is easy to style, from new modules to complete overhauls of the design
- Promotes low impact on browser reflows
- Easy to maintain, especially when inheriting code from someone else
- Small learning curve
Cons
- Likely to have more markup, meaning more DOM nodes for our Javascript to parse through and larger file size
- A little overcomplicated for small sites, in which case you wouldn’t want to use it
- It has a learning curve, where those using it will most likely already know how to get the job done (even if it’s in a less efficient manner) and resist changing their way
Conclusion
For the most part, OOCSS is a cool idea. While the general concept of reusing styles in CSS is not new by any stretch, the creation of such a concise framework that champions the strict use of these concepts should be welcomed with open arms. Nicole Sullivan deserves a ton of respect for all of the time she has put into this project and for all the other related articles she’s published, such as the information on reflows and repaints.
Now, the real question is how do we take these CSS frameworks and turn them into great looking, high performance websites that reflect the brand messages of our clients? That is what we’ll be focused on and I’m sure we’ll come up with some really great stuff.