CSS Lesson No.1 - More classes to a tag
by Bence Kucsan 1 year, 4 months ago on Mar 19, 2007 - Commentary (28) - Filed under - Tutorials - Grab the RSS


Well, it's quite simple, goes just like this: <div class=”message error”>


So I wanted to start this new section with a very impressive technique, I just discovered a couple of days ago. Although it’s very plausible, I’ve never used this before, but probably would have saved me a good amount lines of code.

How does it work?

It’s very simple, you can add multiple classes to a tag separated with a space. In the example I highlighted above I would use it to define the model of a text box called “message” only once, like

.message { width: 250px; background-color: #eeeeee; border: 1px solid #cccccc; }

and let them mutate in cases where I need this box in form of an error message, where I only need for example the borders to be red. For this I would only need the easy one liner:

.error { border: 1px solid red }

And this would overwrite the border definition of the class message to the red one. Awesomeness.



by Bence Kucsan 1 year, 4 months ago on Mar 19, 2007 - Commentary (28) - Filed under - Tutorials - Grab the RSS
 
28 Comments  ↓  Last 1 week, 6 days ago + Add
 
Bence 
28 comments, add yours!

Your thoughts are welcome!
Make your voice heard, fill out the comment form below and don't be shy to hit that big blue button.

Matthew Holmes 
on March 19, 2007 at 11:27

I have been using this technique for last three years, and found it to be very useful in reducing some code and more flexibility.  It rocks.

by Matthew Holmes 1 year, 4 months ago - Permalink - Add a Response
trovster 
on March 19, 2007 at 11:31

Not sure why you’re adding the class ‘message’, but like Matthew, this is really useful. For example:

div class="warning missing” div class="warning invalid” div class="warning invalid email”

by trovster 1 year, 4 months ago - Permalink - Add a Response
trovster 
on March 19, 2007 at 11:35

Oh yeh, forgot to mention. This isn’t a CSS trick. It’s a HTML ‘trick’. You can access they classes with JavaScript, as well.

Also, beware when using the selectors. .message.error {} works incorrectly in IE6. It will apply this selector to any element which has a class of ‘error’, ignoring the other classes in the selector part. Whenever you join up classes like this IE6 will apply the selector to the last one mentioned.

by trovster 1 year, 4 months ago - Permalink - Add a Response
jinesh 
on March 19, 2007 at 12:57

fyi: you can add more than two classes to an object...i’ve tested upto 50 classnames and the cascading works perfectly :)

by jinesh 1 year, 4 months ago - Permalink - Add a Response
Ash 
on March 19, 2007 at 13:12

I found this out around a year ago. I then thought to myself ...
“Why the hell didn’t I know that? “
its so simple and logical when you think about it, but I can say I have never seen a tutorial or blog entry talking about it.

by Ash 1 year, 4 months ago - Permalink - Add a Response
Jonathan Sweet 
on March 19, 2007 at 13:27

The problem comes when you’re writing your CSS in IE (bug surprise there). If you try the following it won’t work in IE (which stinks because it really would make life easier sometimes):

<div id="message_box" class="error">Message</div>

div#message.error {font-size:2em;}

by Jonathan Sweet 1 year, 4 months ago - Permalink - Add a Response
Bence 
on March 19, 2007 at 16:04

@ trovster, Jonathan - Thanks for sharing that!
@ jinesh - LOL

by 1 year, 4 months ago - Permalink - Add a Response
 
on March 25, 2007 at 01:12

Jonathan - just add a space:

div#message_box .error { font-size: 2em; }

...and it will work just fine. The class doesn’t belong to the selector but to the div tag.

:)

by 1 year, 4 months ago - Permalink - Add a Response
Laszlo 
on April 04, 2007 at 18:39

wow...very cool. I stumbled across your site at artnetz.de. I’ll bookmark this one for sure. Keep ‘em coming!

by Laszlo 1 year, 4 months ago - Permalink - Add a Response
Will 
on April 10, 2007 at 03:20

Gah! How did I not ever think of this?!

This is like Brian Velso’s styling of the html tag some months back…

Thanks!

by Will 1 year, 4 months ago - Permalink - Add a Response
Pim 
on April 12, 2007 at 17:42

I was looking for a way to add more than one class to a tag, used Google and your site was the 1st on the result list and provided precisely the answer I needed. Thanks very much!

by Pim 1 year, 3 months ago - Permalink - Add a Response
Bence 
on April 12, 2007 at 22:42

Glad to hear that folks, actually I’m a bit behind with postings, but there are definitely more coming, stay tuned!

by Bence Kucsan 1 year, 3 months ago - Permalink - Add a Response
Ricky 
on April 13, 2007 at 18:21

Oh great! Thanks! :) Does it work with ID’s to? My guess is that it doesn’t, but I had to ask. :)

by Ricky 1 year, 3 months ago - Permalink - Add a Response
Viorel Mocanu 
on April 23, 2007 at 06:17

No, ID’s are unique throughout the document and should be used accordingly - one for each element. Classes on the other hand are far more flexible.

by Viorel Mocanu 1 year, 3 months ago - Permalink - Add a Response
Zig28 
on November 11, 2007 at 10:07

Wow, great tip - I’ve been using CSS for years and always wondered if I could do this but never did much about it until now.  I Googled “add two classes to tag”, found your page and tried it—it works! 

I now can use one class to shape a server-side include menu in a right column, and I added another class that I needed to put a space above that right-column include only on some pages.

Thanks!

by Zig28 9 months ago - Permalink - Add a Response
sveta 
on February 24, 2008 at 03:26

diivanilaud++&#x3C;

by sveta 5 months, 2 weeks ago - Permalink - Add a Response
 
on March 19, 2008 at 11:09

Hello!
I think this try.

by 4 months, 3 weeks ago - Permalink - Add a Response
fisheryouhn 
on April 05, 2008 at 15:06

Tips for caring for a fish aquarium
http://www.freewebtown.com/toyaquarium/
Tips for caring for a fish aquarium

by fisheryouhn 4 months ago - Permalink - Add a Response
Pierre Farrell 
on April 18, 2008 at 04:50

sapropel drudgism chufa emersion cheap seizer unphysicked doughmaker
<a href= http://www.jud.state.ct.us/external/kids/history/postcards/ >Connecticut Courthouse Postcards</a>
http://www.clubdonatello.org

by Pierre Farrell 3 months, 3 weeks ago - Permalink - Add a Response
SeeniWedkex 
on May 07, 2008 at 02:12

http://www.youtube.com/SergTorres free porn movies
<a href=” http://www.youtube.com/SergTorres “>free porn movies</a>
<a href=” http://www.youtube.com/SergTorres “>free porn movies</a>

by SeeniWedkex 3 months ago - Permalink - Add a Response
Martyn 
on May 19, 2008 at 17:37

uh wow pretty simple really, but yet I never thought of doing that. Cheers

by Martyn 2 months, 2 weeks ago - Permalink - Add a Response
Porn 
on June 05, 2008 at 00:13

Thanks for the lesson!

by Porn 2 months ago - Permalink - Add a Response
saemco 
on June 13, 2008 at 07:14

example site for css - stylesheet : http://hotels.saemco.org

by saemco 1 month, 3 weeks ago - Permalink - Add a Response
 
on June 26, 2008 at 16:04

well the comments seem to have disintegrated as we get near the bottom of the list.

but your post is still helping people!

by 1 month, 1 week ago - Permalink - Add a Response
parzufim.com 
on July 09, 2008 at 04:48

viagra cialis levitra clomid buy viagra onli generic viagra http://www.parzufim.com cipro gay viagra buy viagra online cheap viagra
free viagra
herbal viagra
propecia xenical viagra side effects buy viagra zithromax lasix female viagra cream cheapest cialis http://www.parzufim.com

by parzufim.com 4 weeks, 1 day ago - Permalink - Add a Response
tery 
on July 14, 2008 at 15:50

Cool 100%

by tery 3 weeks, 3 days ago - Permalink - Add a Response
Kevin Design 
on July 15, 2008 at 08:55

Thanks for the tip, i can see that being useful for for reducing coding. Im amazed at how many neat little tricks there are that keep poping up, this is another to add to the ever growing list. - thanks =)

by Kevin Design 3 weeks, 2 days ago - Permalink - Add a Response
bootleg movie download 
on July 25, 2008 at 05:24

This e-mail message is intended only for the addressee(s) and contains information which may be confidential or legally privileged. If you are not the intended recipient please advise the sender by return e-mail, do not use, copy or disclose the contents, and delete the message and any attachments from your system. Unless specifically indicated, this email does not constitute formal advice or commitment by the sender. E-mail communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions which arise as a result.

 
 
 
Leave a Comment
Make your voice heard, fill out the comment form below and don't be shy to hit that big blue button.


Comment                                                                                                                                              *required
Name                                                              *required

E-Mail Address

Website URL

Personal Options
 Remember my personal information

 Notify me of follow up comments

Nothing to say?
No problem, just give stars like they were cookies
Rate Entry
from 11942 readers

I'm not a fan of any form of moderation, but off-topic, offensive comments may be deleted, repeat offenders are playing with a lifetime ban from commenting. Thanks for helping me making this a cool place to be.