Use structural naming conventions

Written by That Web Guy on 21st June 2009. 13 comments

Use structural naming conventions
  • Avant Innovations
  • Advertise Here

How much thought have you ever put into what you name your classes and ID's? If you're like me, your probably guilty at one time or another of assigning a name to a div that describes where it is or what it looks like.

While doing this isn't detrimental to your site, it's better practice to use a naming convention that describes the structure, not the appearance or position.

Here's a typical example of using a naming convention that describes the appearance and position.

 naming_wrong.gif

It looks innocent enough, but why is it no good? For starters, what if you wanted to swap things around with CSS? Let's say you wanted to move right_div to the other side, suddenly left_div will be in the right position.

Using another example, let's say you have a block of text on your page that is large and blue, like the intro text of this article. I could have easily used a class called big_blue_text, but what if I changed my mind about the colour or size later, and made it smaller and green?

The purpose of using a good naming convention is so you have the flexibility to control the layout of your page using CSS without having to change the HTML for things to make sense.

Sticking to a consistent naming convention also means you'll have an easier time editing your site in the future, and if you're in a team environment then it means the other guy won't be confused. Working on someone else's code can be difficult enough without trying to figure out why top_red_text is actually green and appears in the middle of the page.

What's the best way then?

As mentioned earlier you should use a naming convention that describes the structure. Here's an example.

 naming_right.gif

Some will argue that header and footer are still presentational descriptors, and that might be true. But given the field we are in, header and footer are long established and universally accepted as representing the top (header) and the lower (footer), so there should be no confusion behind their meaning.

But if you're not convinced of that, you could always try:

 naming_extreme.gif

Likewise for other elements that require a class or ID. Using the blue text I have at the start of my article as an example again, the class for that is called intro, as opposed to something like big_blue_text.

An easy way to keep you on track with good naming conventions is to simply ask yourself "what is this element intended for?" Not, "where is this element going?" or "what does it look like?".

Easy peasy lemon squeezy.

Is this worth sharing?

That Web Guy

About That Web Guy

That Web Guy (Mikey to his friends) is a veteran web designer based in Perth, Western Australia, and currently Design Director at Perth Web Design. When he's not XHTML'ing or messing around in Photoshop, Mikey can usually be found preaching web standards evangelism onto unsuspecting victims.

Feel free to send That Web Guy a message some time, follow him on Twitter, or make a donation.

Comments

Not a Member

santosh

Good articles ?

Monday 22nd June 2009 | 06:22 PM Reply Comment URL Back to top

That Web Guy

That Web Guy

Responding to this comment by santosh

Thanks? :-)

Monday 22nd June 2009 | 08:11 PM Reply Comment URL Profile Back to top

Not a Member

Jeff Chapman

You made some very relevant comments on naming DIV tags - I've often caught myself naming a DIV block as "left-content" on something similar, knowing that it might possibly switch place if the situation calls for it. Your "primary_content", "secondary_content" approach is nice.

While we're at it, it would be good to hear more about using relevant semantics and markup (something you may have touched upon in other articles) as well - for instance, when it is appropriate to use an H1, a BLOCKQUOTE, a UL element and so on... and why it matters. Just a thought.

Tuesday 23rd June 2009 | 10:19 PM Reply Comment URL Back to top

Kim OJ

Kim OJ

Is there a difference between class and id?

Wednesday 24th June 2009 | 12:08 AM Reply Comment URL Profile Back to top

That Web Guy

That Web Guy

Responding to this comment by Jeff Chapman

Thanks Jeff. I've got semantics in my pipeline so I may as well make it the next topic.

Wednesday 24th June 2009 | 05:59 AM Reply Comment URL Profile Back to top

Harun Smrkovic

Harun Smrkovic

Responding to this comment by Kim OJ

Of course. You name an object with ID when it's single.. In other words, you may only have one object with one ID.. If there is duplicate ID, it won't validate...
Tough with class you can name a whole bunch of objects like class="external_link".. and when you do a CSS, it will apply to a whole class.

Wednesday 24th June 2009 | 06:09 AM Reply Comment URL Profile Back to top

That Web Guy

That Web Guy

Responding to this comment by Kim OJ

Howdy Kim.

ID's can only be used once on any given web document because they are used to describe a unique element to separate/construct your mark-up. You couldn't for example have 2 divs called "primary_content".

ID's can be manipulated with JavaScript as well, so they need to be unique. Another example, the login slider on this site is in a div called "login_form" which slides open when activated by a menu button. If I had 2 divs with the same ID, then the JavaScript will be manipulating both of them which I don't want to happen.

A class can be used as many times as you like on any page as they are considered members of a group - hence "class" like "classroom".

Hope my feeble explanation helped.

Wednesday 24th June 2009 | 06:21 AM Reply Comment URL Profile Back to top

Felix

Felix

Very relevant. I think I finally got the point for naming DIV's.

Thanks for this.

Wednesday 24th June 2009 | 06:47 AM Reply Comment URL Profile Back to top

Kim OJ

Kim OJ

Responding to this comment by That Web Guy

Thanks! I think I need to go make some IDs into Classes.

Wednesday 24th June 2009 | 06:51 AM Reply Comment URL Profile Back to top

Not a Member

Jemma

Guilty as charged. Looks obvious enough when you take a step back to think about it.

Monday 29th June 2009 | 10:30 AM Reply Comment URL Back to top

Not a Member

Marc

Good article! ;p

Naming is important indeed, even more so in webprogramming.

Maybe off-topic, but is there a good reason why CSS files are written 1-statement per line?

I have tried putting all the declarations on 1 line per id or class and then a 4 page CSS-file suddenly becomes readable without even scrolling.
Probably it's just by convention..

Monday 29th June 2009 | 09:50 PM Reply Comment URL Back to top

Harun Smrkovic

Harun Smrkovic

Responding to this comment by Marc

Because it's easier to tune-up some values later, if you find yourself in need to. Probably.

What's wrong with scrolling :D

Monday 29th June 2009 | 10:15 PM Reply Comment URL Profile Back to top

That Web Guy

That Web Guy

Responding to this comment by Marc

Thanks Marc. There's no reason why it couldn't all be on a single line but for me it's easier to read one line at a time.

Tuesday 30th June 2009 | 05:24 AM Reply Comment URL Profile Back to top

FYI: You are currently not logged in. It's cool though - you can still comment. But only members get a profile page, access to the download section and they can pimp their own web sites. Feel free to register or Login now!

Your name:

Your comments:

Note: HTML tags are automatically stripped from comments.

Are you human?
Turing

Sorry, I have to ask. So what sort of animal is this? (Hint: you don't have to be perfectly specific)

Back to top

Login to That Web Guy Blog

Login

Not registered? | Forgot your Password? Cancel Login