CSS classes and IDs, Which one to use when?

I was trying out a way to float 4 DIVs inside a parent DIV on a quick site design, and this topic came up to my mind. I even explained this to a group of attendees at the India MAX 2004, Bangalore. Many times in the past, I have tried to get this archived but somehow slipped my mind, so, here we go.

Classes and IDs, which one to use, when and why?

By now, it is rather a common thing, most (x)html/css layout designers know very well where to separate ID selectors from classes in CSS! This article should most aptly apply to newcomers.

I was trying out a way to float 4 DIVs inside a parent DIV on a quick site design, and this topic came up to my mind. I even explained this to a group of attendees at the India MAX 2004, Bangalore. Many times in the past, I have tried to get this archived but somehow slipped my mind, so, here we go.

Classes and IDs, which one to use, when and why?

By now, it is rather a common thing, most (x)html/css layout designers know very well where to separate ID selectors from classes in CSS! This article should most aptly apply to newcomers.

The W3C defines that attributes of the type ID cannot have the same value more than once in a document, which means that it is unique for that particular document. Nonthless, as of today, most browsers will not complain even if you violate the same. It may be noted that a CSS ID Selector usually contains a “#” followed by the ID value.

#myid {
	position: relative;
	margin: 0 auto;
	padding: 0;
	text-align: left;
	background: url(../images/pattern.gif) repeat-y;
}

or

div#myid {
	position: relative;
	margin: 0 auto;
	padding: 0;
	text-align: left;
	background: url(../images/pattern.gif) repeat-y;
}

Classes on the other hand can be used many times in a document. So, they are best used to define a common style which can be used on multiple elements on your document. They start with a . (dot) followed by the value.

.myClass {
	font: normal normal normal 86%/1.6em "Lucida Grande", "Lucida Sans Unicode";
	border-top: 1px solid #ebebeb;
	text-transform: lowercase;
	color: #666;
}

An element can have an ID selector and mutiple classes applied to it. Remember one important thing here, ID selectors have higher precedence over class selectors.

So, IDs are for specific unique elements in your particular document and classes to be applicable to multiple elements in a document.


Don't like it? There are lots of published articles, pick a random one.

oCricket

Brajeshwar posted this article on Mon, May 2nd, 2005 at 9:22 pm
Categorized under Technology

Prev Article: CSS Library

Next Article: Kingdom of Heaven


Possibly Related Articles

Archives: Visit the Archives for more articles.

Comments Post Yours

There are no response so far. You can follow any responses to this entry through the RSS feed. You can skip to the end and leave a response. Pinging is currently not allowed.

Post yours

Sidenotes

Quick notes, scribbles, somehow related to this website and to what I do. Or perhaps I'm just plain lazy to make them into a full article.

Introduction to Adobe AIR (NetTuts)

Nettuts have a nice Introduction to Adobe AIR which focus on using HTML/JS to build an AIR Application. It covers -- Adobe AIR Architecture, How to install AIR, Get the development SDK, Configurations, gets on to ...2nd Oct, 2008

Decide Team Type from Books

What do you think of the "Team-Type" if they got these books recently? What "Team-Type" do these books suggest? ( surveys) 29th Sep, 2008

What do investors look for in a startup?

A great video Interview with Venture Hacker Naval Ravikant answering the age-old question: What do you look for in a startup? Investors look for two things that are paramount above all: # Great team: It's obvious. It's a ...27th Sep, 2008

You don't mess with the 'Englineer Bother'

(Via: Hiring Horror) 24th Sep, 2008

Dear Adobe

Do you have a gripe with the Adobe Softwares that you use? Do you have complaints about Adobe Softwares? Please visit Dear Adobe to file your informal gripe with Adobe. 15th Sep, 2008

View the Sidenotes Archive

Play the Penguin Game

Recommended

  • AS 2.0 Reference Reference for ActionScript 2.0 Programming Language used in Flash. Primarily stashed here for my own personal reference.
  • Ode to Apple Dedicated to Apple - Mac, iPhone, iPod, iTunes, Quicktime, Apple TV and all the awesome softwares for the Apple Mac.
  • Downloads All downloads, Free and Open Source.
  • oCricket oCricket is about Cricket and people enthusiastic about it.
  • o! Just Me Of colorful cultures, entertainment, media, life hacks, music, books and movies from hollywood & bollywood.
  • ActionScript 3.0 Reference Flash/Flex ActionScript 3.0 Reference.
  • My Special Job My Special Job is a place where you can look for your weird necessities, strangely superb employees, when your need are more of those hackers, geeks, and ultimate rockstars in the Internet Technology.

Download free Brajeshwar Wordpress Theme

Brajeshwar

Brajeshwar I firmly believe in keeping things simple, easy for users and I envison pushing the technical envelop time and again for the betterment of viable commercial and practical applications. More about me.

Brajeshwar Personal Identity Portal powered by VeriSign Labs

Brajeshwar's affinity with Adobe

My Photos

More photos on Flickr

Member of 9rules Network

Since its inception on 11th June, 2001, "Brajeshwar" has 862 Articles and 5,993 comments, contained within 20 categories and 1,229 tags.