For A Beautiful Web

More on developing naming conventions, Microformats and HTML5

It seems like a lifetime ago that I first sat down with a cup of tea and a bourbon biscuit and thought about the conventions that we use for naming HTML/XHTML id and class attribute values.

Not quite a lifetime, but it was way back in May 29th 2004, on my retired blog And All That Malarkey, when I surveyed forty designers' sites to see what conventions they used for common page elements like headers and banners, navigation, content and footers (the results at the time).

It was hardly scientific research but in June that year I followed up on some comments by Eric Meyer and published a set of naming conventions. I am always really pleased when I find a site that has adopted these naming conventions and I still use them every single day, even over four years later.

My thoughts at the time can be summarized to this.

id and class attribute names must reflect the function or content of an element, rather than reflect presentation. So out went header and in came branding; out went footer and in its place came site-info.

Naming should take on almost an XML style structure. So inside content came content-main, content-sub and content-supp.

These conventions have served me well and I've made almost no changes to their core. Without doubt, they have made my work faster, more consistent and more profitable. They made it easier to build products and easier to train people I work with in my way of thinking. Naming conventions work.

Enough bourbon biscuits to sink a battleship

In four years a lot has happened (and I've eaten enough bourbon biscuits to sink a small battleship). I've seen so-called CSS frameworks developed and grow in popularity. I've seen mighty Microformats bring their set of class attribute values based upon existing standards such as vCard. I've also watched the popularity of JavaScript frameworks such as jQuery grow and with them plugins and markup library items for all manner of interface behaviors.

While my thinking about the importance of naming conventions hasn't changed one byte, my own conventions have evolved. First in relation to Microformats and recently in relation to HTML5.

Microformats and associated attribute naming

Let's face it, Microformats such as hCard, hCalendar, hAtom and other drafts bring with them so many attribute values that it's often not necessary to think of more values by which to structure a document or provide hooks on which to bind CSS styles. I now use Microformats so much that I've even developed whole pages that contain no class attribute values other than those that come with a Microformat.

On the rare occasion where I need to add a new element (let's say a div for layout purposes), my first thought is to extend what is already there in a Microformat. I'll give you an example using the hAtom schema:

<div class="hentry">
<h2 class="entry-title">Title</h2>

<div class="entry-content">
Main content
</div>

<div class="entry-related">
Related content
</div>
</div>

If you're keeping on top of Microformats, you have noticed that entry-related is not part of the hAtom schema, but in a situation like this where I absolutely, definitely have to have an additional element, why make up an attribute value like related-sidelinks when extending the Microformat's naming schema seems so much more logical?

HTML5

I should preface this section by saying that frankly, I couldn't care less about HTML5 at this point in time. Still, that's not really the point. HTML5 introduces some potentially very useful new elements such as:

section
A generic document or application section. A section, in this context, is a thematic grouping of content.
article
A section of a page that consists of a composition that forms an independent part of a document, page, or site. This could be a forum post, a magazine or newspaper article, a Web log entry, a user-submitted comment, or any other independent item of content.
aside
A section of a page that consists of content that is tangentially related to the content around the aside element, and which could be considered separate from that content. Such sections are often represented as sidebars in printed typography.

As it was logical for the inventors of Microformats to base their schemas on existing specifications, surely it now makes sense for me to adapt my naming conventions to follow those in HTML5? Of course I can't yet use:

<section>
<h2> Title</h2>

<article>
Main content
</article>

<aside>
Related content
</aside>
</section>

But I can use id and class attribute values to help me get familiar with HTML5 and bring my documents a little closer to it now.

<div class="section">
<h2> Title</h2>

<div class="article">
Main content
</div>

<div class="aside">
Related content
</div>
</div>

This seems to me to be a logical next step for me to take. So take a look at this demonstration file in which I've taken HTML5 elements as the basis as my naming conventions. In addition to the ones I've just mentioned, look out for the way that I've classified and identified navigation (nav), built columns with colgroup and col and turned an unordered list into a grid with datagrid.

The HTML5 markup specification also includes details, dialog and figure that I could use now as attribute values in the same way.

If I could have one wish fulfilled today, it would be that all CSS framework developers would adopt the same naming conventions (and embed Microformats extensively too), so that people who are new to meaningful markup and CSS get off to the right start and use meaningful, logical rather than presentational id and class attributes.

I'm glad that my naming conventions have worked so well for me and I think they deserve a little tender, loving updating once in a while to keep them relevant.

Oh, and it also means that I can justify eating more biscuits.

From the people behind For A Beautiful Web and Stuff and Nonsense. You will find news about our events, links to sites and articles elsewhere that have inspired us, plus original articles and screencasts on the creative and technical sides of web site design and development by Andy Clarke and others.

Search this site

There have been 21 replies so far

  1. #1Ryan

    Nov 15th 2008 • 5:20 pm

    Interesting stuff and certainly food for thought. I’m pleased to see I follow many of your conventions in most of my work, and I can’t really disagree with the things you have suggested that I don’t already follow. I’m currently reading through Transcending CSS so once I’m done I’ll have a review of the way I do things.

    Cheers.

  2. #2Christopher Schmitt

    Nov 15th 2008 • 5:22 pm

    Nicely done article--definitely food (biscuits?) for thought.

  3. #3Ryan

    Nov 15th 2008 • 5:23 pm

    But one thing I’d like to ask and hopefully you’ll answer, how come you don’t indent your HTML?

  4. #4Andy Clarke

    Nov 15th 2008 • 5:29 pm

    @Ryan: I never indent my markup because I think it looks ugly. I would never typeset a web (or print) document with an ugly, hard-to-read left rag, and the same goes for my code. (oh and I hope you enjoy Transcending CSS. Thanks for buying (or stealing) it.

  5. #5Andrew Woods

    Nov 15th 2008 • 6:00 pm

    Good post!
    “why make up an attribute value like related-sidelinks when extending the Microformat’s naming schema seems so much more logical?”

    This made me think of the browser-specific css rules. Mozilla will now do something like -moz-radius in the event that radius gets added to the standard. I’m wondering if you thought of doing something similar. In your example you used <div class="entry-related">, but perhaps you could use ‘clarke-entry-related’ just in case the identifier ‘entry-related’ actually gets added to the microformat in a different way than you intended. Perhaps I’m overthinking things.

  6. #6antoine butler

    Nov 15th 2008 • 6:12 pm

    great write up! I’ve tended to write naming conventions based on the site in question. Making them awkwardly related to the site topic when ever possible. Ie. For a print designers website I used names such as sheet, letterhead, contents, etc. That’s just qwerky personal preference I guess but in an effort to streamline the process I may have to give your namng conventions a try.

    The use of microformat only naming conventions always made me nervous. Not wanting to get locked into too few options. But the logical deviation you’ve used is giving me second thoughts.

    Thanks

  7. #7Stelabouras

    Nov 15th 2008 • 6:56 pm

    “id and class attribute names must reflect the function or content of an element, rather than reflect presentation.”

    I couldn’t agree more with you.

    On the other hand, HTML5 “footer” and “header” elements are more presentation-aware than content-aware, forcing someone to create div elements with id and class names like “footer” and “header” :P And ye, I am staring at your demonstration file :P

  8. #8Jeremy Anderson

    Nov 15th 2008 • 7:59 pm

    First off, thanks for the post Andy. This has been something that’s been on my mind recently/again. I’ve never actually been 100% convinced that *all* presentational id/class attributes should be removed. In fact, the conventions I’ve been using for quite some time reflect what you suggest here to prepare for HTML5. Granted, using “header” and “footer” elements to me is a bit more problematic than using them as classes or ids.

    My main thinking has been that for top level structural elements, such as the “container”, “header” and “footer” are perfectly fine. These elements almost certainly appear on all websites. Inward, attributes should reflect content or function. So it’s kind of nice to see this come full circle for me.

    Thanks.

  9. #9Andy Clarke

    Nov 15th 2008 • 8:32 pm

    @Andrew Woods: I think the best answer to the (interesting) question you pose is that in the case of Microformats such as hCard, the attribute values come directly from the existing vCard standard — so entry-related wouldn’t ever (to my knowledge) become part of hCard making the need for a prefix unnecessary.

    @Stelabouras: Yeah, you’re exactly correct and using header and footer from the HTML5 spec makes me feel like I just sneezed on my tie. I mean what in the world were the developers of HTML5 thinking for God’s sake?

    (PS: I’ll design a bigger comment textarea soon, I promise.)

  10. #10Neil Bradley

    Nov 15th 2008 • 9:27 pm

    I am also in the process of reading the Transcending CSS book, which is very insightful as I was unable to get to the recent Master Class.

    Currently building my own ‘framework’, which is more of a base template to start with for upcoming designs. I had not thought of using the Microformats approach… great article providing food for thought on a Saturday evening. *Goes to open Coda.*

  11. #11Ross Bruniges

    Nov 16th 2008 • 12:26 am

    I certainly agree with the use of microformat class attributes to standardise things - it’s one of the main reasons for microformats at the moment as adoption and real-world uses seem to have run out of steam rather (I’m prepared to be beat down on this - and in many ways hope that I am)

  12. #12Peter Gasston

    Nov 17th 2008 • 1:45 am

    Funnily enough, I had the same thought myself at about this time last year; must be the onset of winter which makes us stay inside and think more.

    http://www.broken-links.com/2007/12/03/html-5-and-reserved-class-names/

  13. #13Clinton Montague

    Nov 17th 2008 • 12:19 pm

    Very interesting stuff. I like the idea of giving elements class names which represent what they will become in HTML5. Will certainly make migration a lot easier!

  14. #14Richard Clark

    Nov 17th 2008 • 12:34 pm

    Andy, I presuming that you’re sticking with your current naming conventions for the time being as you don’t seem to be a HTML5 fan!

    Just a small point, if we’re talking HTML5, would we not end up with <section class="hentry"> to keep the microformat goodness? Which means in the meantime we would use <div class="section hentry">. However, then you are making the HTML bloated by adding extra classes (which I know you don’t like?). What do you think would be the best approach?

  15. #15Andy Clarke

    Nov 17th 2008 • 12:47 pm

    @Peter Gasston: I think it must all the warm, hat generated blood in our heads. That or the long winter nights.

    @Richard Clark: You’re right. I don’t see the relevance of HTML5 right now, and for something that was supposed to address so many needs in HTML, some of it looks like a missed opportunity. Still, that’s no reason to not look to the future I suppose.

    In my new conventions schema, if I was publishing a blog entry I would use class="section hentry”, yes. I’m not opposed to class attributes, as long as they add meaning and represent the content they are describing. I wouldn’t think twice about class="section hentry”

  16. #16John Arthur

    Nov 17th 2008 • 4:22 pm

    I’ve used a lot of your conventions ever since reading your article years ago, and I think it’s made me a better code monkey.

    When I read about the ‘role’ attribute in XHTML2, I decided I liked the ideas behind that a bit more, and have since adapted it into my conventions. Now I use class="role_banner" for the header, role_main for main content, etc.

    I’ll still use nav-main, -secondary, etc, but they’ll usually paired with or contained within a role_navigation.

    I just liked the extra bit of semantical goodness that came of it. Also, role_banner seemed to be more encompassing than ‘branding’, which was troubling if I wanted to include navigation and search elements in the ‘header’.

    Just my 2 biscuits.

    JA

  17. #17Andrew Rodgers

    Nov 17th 2008 • 10:32 pm

    Great article Andy. This, along with your Content Based Workflow, has convinced me to move forward with a set of front-end web standards/best practices for the company I work for. Thanks, I’ll let you know how warmly these are accepted/practiced.

  18. #18Anders

    Nov 18th 2008 • 12:25 pm

    Andy,

    interesting read as usual.
    Not sure if i will adopt this convention right away, but it definitely looks interesting.

    Looking at your demo, what’s the reasoning behind the naming of the inner footer area? At the moment you’ve got the outer div with class footer and the inner footer content has an id with the same name (footer) applied to it. A bit confusing i think. Would it be more clear to instead let the inner footer area have the class section, instead of the footer id?

  19. #19Andy Clarke

    Nov 18th 2008 • 1:26 pm

    @ Anders: I needed an extra wrapper div to allow the background colour to fill the window while keeping the inner content fixed-width and centered. I suppose I could have added the class to either.

  20. #20Anders

    Nov 18th 2008 • 2:17 pm

    I’m with you Andy, just thought you could reuse the .section that you already defined in your stylesheet, which actually takes care of both the centering and width. Like:

    <div id="footer">
    <div class="section">
    [...]
    </div>
    </div>

  21. #21DAZ

    Nov 20th 2008 • 11:12 pm

    Hi Andy, an interesting post.

    I think that the thinking behind ‘header’ and ‘footer’ is that they are the header and footer of the markup - they do usually go at the beginning and end after all.

    I think that you’re definitely right - it would be good if we started using classes from the html5 spec as well as microformats. I also think it would be a great idea for developers to have some conventions when it comes to class names - in fact it’s something that I’ve felt should happen for a while now. The main problem would be reaching a consensus - why should we use the class names that work for you? For example, ‘nav’ means nothing to me, surely we should use the full word ‘navigation’? What is needed is some sort of get together where these conventions could be thrashed out and then people would have to start using them ... in a similar way to microformats. Certainly, the html5 spec would be a good place to start and make it easier to update markup and styles in the future.

    I’m trying to develop a CSS framework and have tried to incorporate microformats, html5-like class names (’header’, ‘section’ etc) and other standard class naming conventions. It would be great if the community could get together and decide on these conventions.

    I disagree with your statement that:
    ‘id and class attribute names must reflect the function or content of an element, rather than reflect presentation’
    Non-semantic class don’t lose anything in your markup and should be fine to use, especially if supplemented with semantic class names too. In fact, it is almost impossible to produce a framework without using some generalised class names. I’ve blogged about this here:
    http://dazzl.co.uk/blog/don-t-sweat-the-unsemantic-stuff

    I also don’t quite get your use of ‘colgroup’ - surely this is presentational?

    cheers,

    DAZ

Commenting is not available in this weblog entry.
Stuff and Nonsense Ltd.
The Cow Shed Studio, Eversleigh
Gwaenysgor
Flintshire, North Wales
LL18 6EJ
UK