posted on:December 16, 2009
Class Names Revisited
In this article I am going to remind us all about the class names. I’ll try to explain the true meaning of classes in HTML and take a look at their usage.
True Meaning of Class Names in HTML
Just as the element’s id, class names placed inside a class attribute are actually used to identify the element. Their "secondary" purpose is to be used as a more precise style sheet selector.
Class attribute is a part of the HTML structure so opinion that it’s there only for styling purpose is wrong. Sure, widely spread habit among web designers is to use class names almost exclusively for that purpose, but saying that class attribute is only for CSS selecting would be more or less the same as saying that element tag names or ids are there only to be used in style sheets.
Multiple Names
Class attribute allows multiple class names. While element can have only one id you can assign as many class names to an element as you want. Think of it like this: YOU have your unique name, but you can also be identified as a web designer, male, athlete, student, friend, geek etc. While your name is your ID these attributes are your “class names”.
Just as you can belong to many groups of people HTML elements can belong to different “groups” so it makes sense that class attribute can accommodate many class names.
Semantics
We said that classes in HTML are part if its structure. Just as you need to take care of semantic meaning when building structure of a document or wrapping content inside HTML elements, you need to take care of class name semantics.
How to choose a proper class name for the element? Well, remember that you need to identify the element not describe its appearance. Step away from thinking about classes as purely presentational tools. Let’s use that personal analogy once more, if you would assign “class name” for yourself you can’t describe yourself as a “red shirt” because next day you might want to wear a blue shirt. So, HTML element can’t be identified as “red”, cause it can easily change color.
Usage
We said that class names are used to identify the element and used as a style sheet selectors. In my recently released framework I have a set of built-in set of JavaScript functions and I am using class names as “hooks” to trigger element’s behavior. For instance in my tooltip example I simply assign a class name “tooltip” to an element and activate the functionality. Many JS libraries, support class names selectors so it’s fairly easy to “get” to the element you want to use.
Hope this article help you understand the meaning of class names or perhaps remind you what the class names stand for. 🙂
Enjoyed the article?
Then don't miss our next one! Subscribe to our RSS feed or share it with your friends.
Share on FacebookTweet thisDeliciousStumbleUpon RedditSubscribe
Comments (11 Comments)
Sorry, the comment form is closed at this time.
Benjamin Reid
December 16, 2009
Redswish
December 16, 2009
Edgar Leijs
December 16, 2009
freaknboy
December 16, 2009
Jacob Lowe
December 16, 2009
Peter
December 16, 2009
Carl
December 17, 2009
cssglobe
December 17, 2009
strija
December 17, 2009
Red
December 21, 2009
jitendra
January 1, 2010