Speed Is King Offered Their Suggestions Best Practices
Speed Is King Offered Their Suggestions Best Practices
Speed Is King Offered Their Suggestions Best Practices
As described on the OOCSS GitHub repos Wiki page, OOCSS is based on two main principles.
Separation of Structure From Skin
Almost every element on a styled Web page has different visual features (i.e. skins) that are
repeated in different contexts. Think of a websites branding the colors, subtle uses of
gradients, or visible borders. On the other hand, other generally invisible features (i.e.
structure) are likewise repeated.
When these different features are abstracted into class-based modules, they become reusable
and can be applied to any element and have the same basic result. Lets compare some before
and after code so you can see what Im talking about.
Before applying OOCSS principles, you might have CSS that looks like this:
#button {
width: 200px;
height: 50px;
padding: 10px;
border: solid 1px #ccc;
background: linear-gradient(#ccc, #222);
box-shadow: rgba(0, 0, 0, .5) 2px 2px 5px;
}
#box {
width: 400px;
overflow: hidden;
#widget {
width: 500px;
min-height: 200px;
overflow: auto;
border: solid 1px #ccc;
background: linear-gradient(#ccc, #222);
box-shadow: rgba(0, 0, 0, .5) 2px 2px 5px;
}
The three elements above have styles that are unique to each, and theyre applied with the nonreusable ID selector to define the styles. But they also have a number of styles in common. The
common styles might exist for branding purposes or consistency of design.
With a little bit of planning and forethought, we can abstract the common styles so the CSS
would end up instead like this:
.button {
width: 200px;
height: 50px;
}
.box {
}
width: 400px;
overflow: hidden;
.widget {
width: 500px;
min-height: 200px;
overflow: auto;
}
.skin {
Now all the elements are using classes, the common styles are combined into a reusable skin
and nothing is unnecessarily repeated. We just need to apply the skin class to all the elements
and the result will be the same as what the first example would produce, except with less code
and a possiblity for further reuse.
Separation of Containers and Content
The second principle described on the OOCSS GitHub wiki page is the separation of containers
from their content. To illustrate why this is important, take the following CSS:
#sidebar h3 {
font-family: Arial, Helvetica, sans-serif;
font-size: .8em;
line-height: 1;
color: #777;
text-shadow: rgba(0, 0, 0, .3) 3px 3px 6px;
}
These styles will apply to any third-level headings that are children of the #sidebar element.
But what if we want to apply the exact same styles to third-level headings that appear in the
footer, with the exception of a different font size and a modified text shadow?
Then we would need to do something like this:
#sidebar h3, #footer h3 {
font-family: Arial, Helvetica, sans-serif;
font-size: 2em;
line-height: 1;
color: #777;
text-shadow: rgba(0, 0, 0, .3) 3px 3px 6px;
}
#footer h3 {
font-size: 1.5em;
text-shadow: rgba(0, 0, 0, .3) 2px 2px 4px;
}
Now were unnecessarily duplicating styles, and might not realize it (or simply dont care).
With OOCSS, were encouraged to give more forethought to what is common among different
elements, then separate those common features into modules, or objects, that can be reused
anywhere.
The styles that are declared using the descendant selector in those above examples are not
reusable, because they are dependent on a particular container (in this case either the sidebar or
the footer).
When we use OOCSSs class-based module building, we ensure that our styles are not dependent
on any containing element. This means they can then be reused anywhere in the document,
regardless of structural context.
A Real-World Example
To further illustrate how OOCSS can be used, Ill use something similar to what I did on my
sites recent redesign. After coding the inner header element on my site, I realized that the basic
structural styles for the inside of the header could be reused on other elements on the page.
So heres something along the lines of what I had when I started styling my header:
.header-inside {
width: 980px;
height: 260px;
padding: 20px;
margin: 0 auto;
position: relative;
overflow: hidden;
}
A few of the styles listed here are unique to the .header-inside element. But the rest can form
a module that I can reuse. So I can abstract the structural styles into their own reusable class.
Heres the result:
.globalwidth {
width: 980px;
margin: 0 auto;
position: relative;
padding-left: 20px;
padding-right: 20px;
overflow: hidden;
}
.header-inside {
padding-top: 20px;
padding-bottom: 20px;
height: 260px;
}
A fixed width
Now were free to use these styles on any elements that require these same characteristics by
simply adding that class to the desired element without writing a single extra line of CSS.
For my site, I reused these structural styles on the primary content element and the inner footer
element. Depending on the design, these styles could also apply to a horizontal navigation
element that might appear between the header and the content, or any other element that has a
fixed-width and needs to be centered on the page.
After adding the globalwidth styles to these elements, the markup would look something like
this:
<header>
<div class="header-inside globalwidth">
</div>
</header>
<div class="main globalwidth">
</div>
<footer>
<div class="footer-inside globalwidth">
</div>
</footer>
Some may feel that this type of styles abstraction clutters the HTML and goes against the
principle of separating markup from presentation.
But putting aside any debates about how this might affect the markup, no one can question that
this abstraction has now made it easier to track down and modify the common styles that are
used to structure these three elements.
The media object is a great example of the power of OOCSS because it can contain a media
element of any size with content to its right. Although many of the styles that apply to the
content inside of it and even the size of the media element itself could change, the media
object itself has common base styles that help avoid needless repetition.
With OOCSS, instead of a constantly growing stylesheet full of specificity wars, youll have an
easy to maintain set of modules where the natural cascade plays an important role.
When making additions to an existing site, you wont be adding new styles to the bottom of your
stylesheet without regard for what came before. Instead youll be reusing existing styles and
extending your styles based on existing rule sets.
With this type of forethought, its possible to create entire pages while coding very little CSS.
Any existing CSS modules can serve as a basis for all new pages, and any new CSS will be
minimal. In some cases you might even be able to create a new fully-styled page without coding
a single line of CSS.
These maintainability benefits also extend to the robustness of your stylesheets. Because the
styles are modular, pages built on OOCSS will be less likely to break when a new developer
starts to use the stylesheet.
Nonetheless, I think its a good idea, at the very least, to start thinking in terms of OOCSS in all
your projects. Once you get the hang of it, Im sure youll find it much easier to get it working on
bigger projects where the benefits would be more noticeable and relevant.
Use CSS Lint to check your CSS (and know that it has options and method to its
madness)
There will obviously be times when some of these rules will be broken, but overall, these are
good habits to develop and will lead to stylesheets that are smaller and easier to maintain.
Conclusion
Many people fear the OOCSS ideology because it seems to go against many of the so-called
best practices weve learned. But once the long-term benefits of using OOCSS are
understood, Im sure many developers will become converts.
Overall I think OOCSS has a bright future in CSS development, and its a concept that all
developers should start incorporating into their projects at least on some level to help
create Web pages that are faster, more efficient, and easier to maintain.