posted on:January 3, 2009
My Top 10 Most Used CSS Class Names
Many developers are puzzled when it comes to assigning class names to elements and often end up using wrong ones. Class names should not describe how the element looks like or where is it placed. A good class name should describe what a certain element represents.
Here are my top 10 class names with explanations. Hopefully it will give you a clearer image of what kind of class names you should use.
class="fixed"
I use this class name in every style sheet. This is the class name I assign to container element that contains floated child elements. I use it to clear the floats within that container with this:
.fixed:after{ content:"."; display:block; height:0; clear:both; visibility:hidden; } .fixed{ display:block; } /* */ .fixed{ min-height:1%; } * html .fixed{ height:1%; }
So if I have situation like this:
<ul> <li><img src="images/img_01.jpg" alt="First Thumb" /></li> <li><img src="images/img_02.jpg" alt="Second Thumb" /></li> ... </ul>
…where images are floated next to each other I use:
<ul class="fixed">
…and that’s it.
class="alt"
Alt is short for "alternative" (or "alternate" if you want). I use this class name where I have a group of element styled in a certain way, but have some small variation on some of them. i.e. images floated to the left or right.
#content img{ float:left; display:inline; margin-right:10px; border:1px solid #ccc; padding:1em 0; background:#fff; } #content img.alt{ float:right; margin-right:0; margin-left:10px; }
class="selected"
This is the class name I usually use on navigation item that is being selected:
<li class="selected"><a href="/about">About Us</a></li>
Also I use it when I have JavaScript tabbed content to mark a selected tab:
<dl> <dt class="selected">Tag Cloud</dt> ... ... ... </dl>
class="first", class="last"
Until :first-child and :last-child pseudo-classes are supported in all browsers (and I really mean all, like 99,9% "all") I am sticking with this. I use these class names to select first and last child of a certain element. I find this to be a great method in reducing unnecessary markup. I will write an example in one of my next posts.
class="image"
I normally select image with tag selector (i.e. #content img) but this class name is used for image container (p class="image") when situation requires it. Let’s say you have a news list and you need image with caption below floated to the left and rest of the content next to it. I use:
<p class="image"> <img src="/images/img_me.jpg" alt="my funny face" /> This is me trying to look cool! </p> <p> The rest of the content here ... </p>
class="inner"
I often use this class and I must say that is mostly used for presentational purposes. I assign this class name to extra nested divs in case I need extra styling (i.e. double backgrounds)
<div id="container"> <div class="inner"> </div> </div>
class="link"
As the name says, I use this class name for links. 🙂 But I place this on a container, usually a P tag, not the A tag itself. The most common usage is with those "read more" buttons.
<p class="link"><a href="#">Read more...</a></p>
That way I can style the anchor within the P with .link a but can also apply specific styling to the paragraph as well.
class="one", class="two", class="three"…
I use these class names when I have to target each element individually. Most common case is when using image replacement technique in navigation:
<ul> <li class="one"><a href="#">Home</a></li> <li class="two"><a href="#">About</a></li> ... </ul>
class="even", class="odd"
Class names used for alternating rows in tables or lists.
<ul> <li class="even">Content</li> <li class="odd">Content</li> <li class="even">Content</li> <li class="odd">Content</li> </ul>
or
<table> <tr class="even"> <td>Content</td> <td>Content</td> </tr> <tr class="odd"> <td>Content</td> <td>Content</td> </tr> <tr class="even"> <td>Content</td> <td>Content</td> </tr> <tr class="odd"> <td>Content</td> <td>Content</td> </tr> </table>
class="section"
This class name is formerly known as "box". 🙂 I use it for certain sections of content when I require some special styling.
<div class="section"> content here... </div>
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 (24 Comments)
Sorry, the comment form is closed at this time.
John Faulds
January 3, 2009
mike
January 4, 2009
spawnsdf
January 5, 2009
Thomas Winsned
January 5, 2009
Harry Roberts
January 5, 2009
cssglobe
January 5, 2009
cssglobe
January 5, 2009
Fredrik W
January 5, 2009
cssglobe
January 5, 2009
fernando filho
January 5, 2009
Marco
January 5, 2009
Evan
January 5, 2009
Shane
January 6, 2009
cssglobe
January 6, 2009
John McMullen
January 6, 2009
Jon Hartmann
January 6, 2009
Navdeep
January 6, 2009
Kevin Mario
January 7, 2009
Neal
January 8, 2009
Neal
January 8, 2009
cssglobe
January 11, 2009
sidhu
January 24, 2009
Ben
February 12, 2009
electric_g
February 13, 2009