Choosing Class and ID Names

Semantics should also be used when deciding on class or id names for elements. Developers usually choose unwisely when deciding on these names, as they choose a name specific to the design they are creating at the time. This article will discuss what good class/id names are with examples of both bad and better choices.

Bad Choices

  • leftmenu
  • redlink
  • no_border

The reason these aren’t very good class/id names is because they relate to the current design of the website. With the ability of CSS to change a design extremely easily makes naming elements properly important. A new design might have the menu on the right, and the “redlink” might be normal. When choosing class and id names keep in mind the purpose of the styling not the styling itself.

Better Choices

Below are examples of good class/id names:

  • container or wrapper
  • navigation
  • submenu
  • external_link

With names like these it’s clear to see what the function of that area is. This is important when creating a new design as you can easily identify the purpose of the styling.

Generic Names

A page is often made up of five main areas. Below are some suggestions of the naming of these containers. Using these naming conventions also help people who use custom stylesheets to override certain styles to improve their own experience target the areas more easily.

  • header
  • navigation
  • content
  • sidebar
  • footer

Conclusion

Simply keep in mind what the elements function is, such as external link or more information, and not what the element is going to look like during the current design. Check this by reading through the code to check whether you can work out what each element does and not what it looks like. If you can do this then the class/id names are good.

4 Comments to "Choosing Class and ID Names"

  1. 1 Carlos Bernal 2 days, 14 hours later.

    Carlos Bernal's Gravatar

    Great advice. However, due to such large style sheets that are now produced, I have found it necessary to abbreviate all classes and id’s above and beyond using shorthand and consolidation.

    So instead of:
    wrapper
    header
    navigation
    content
    sidebar
    footer

    I now use:
    wrp
    hdr
    nav
    con
    sid
    ftr

  2. 2 Bojan 2 weeks, 2 days later.

    Bojan's Gravatar

    Yes, good advice indeed.

    @Carlos: Actually, I find names such as those quite confusing. I always want to find my way around the rule sets quickly, so I use full, descriptive names. If you’re fine with it, then there’s no problem, but if you’re doint that just to cut down the size of the stylesheet, then there may be other more appropriate ways.
    Cheers!

  3. 3 marko 5 months, 3 weeks later.

    marko's Gravatar

    I am alwaus using descriptive names so I can remember when editing but also look to keep them as shorter as I can.

  4. 4 clara 6 months later.

    clara's Gravatar

    I’m having trouble creating a user ID for my emails. I wanted to include clara john david crooks in whatever order and not subject to include all names. I only come up with unelegant words…Could you help me? I’d really appreciate it.

    thanks
    clara