Autochrome 3.0 is coming soon. Buy Autochrome 2.0 for $19.99 & get free upgrade to Autochrome 3.0.

Multiple Class in CSS

— By

Damn! Why was I never told, taught, tiped that this is possible and modern browsers support ‘em. The oracle of CSS would be laughing at me for learning this late, yes very late. I had missed an online conference today while trying to get my head around to this trivia, “mutltiple classes” in CSS. I have learnt long back that I can have a class piggybag to an ID but what was troubling me was that I wanted to inherit all properties of a particular class and then apply some sparingly used class to few HTML elements (they appear one or twice in a page while the parent class was appearing many times in a page).

What I have been thinking all along was that we cannot apply two classes, well logically thinking, it is not possible to do class=”firstClass” class=”secondClass” inside, say, a single DIV tag. I was very tempted to break the valid standard rule and do an ID=”firstClass” class=”secondClass” but I know I will be breaking my own principle, “stick to the rules”. I ended up nesting DIV tags just to inherit the parent property (that sucks)! And I know of few fallouts if I keep using this technique and that will be particularly with IE (IE are still used by majority of the people, believe me! Some people think Internet means IE, even some seasoned developers/designers have not heard of Firefox).

I have got into this issue before and I wanted to avoid the same here. If the parent container has a background colour and is positioned (relative), then the floated child elements will not show their background (colour or image); and if the child is a floated image, then the whole image disappears. It is there, but “behind” (eh!) the background of the parent element. The solution is to also position the child element ({position: relative}).

Ok, now coming to the point, much to my surprise (I am feeling like a dork now) is that CSS can take in multiple classes, just separate the classes by a whitespace; myClass=”classOne classTwo” (look at the whitespace separating the classes). Ah! you can have more than two classes, yes, multiple classes. Isn’t this good news, now I have some good ideas to shorten my CSS even more.

[Note: Currently no browers serve compressed CSS and whitespace and comments are not striped thus adding to the file size. Let me give Flash a quick Thanks here. Neverthless, whitespaces and comments should be used appropriately so not to make your CSS look like a virgin African forest]

Brajeshwar

Brajeshwar believes in simplicity; pushes the envelop and envisions the betterment of usable and practical solutions.

More by Brajeshwar

  • John

    Oh yeah, it's a great feature.

    Saves file size too.

  • John

    Oh yeah, it's a great feature.

    Saves file size too.

  • Peter Hall

    Thanks! I didn't know about that either. Do you know which browsers support it?

  • Peter Hall

    Thanks! I didn't know about that either. Do you know which browsers support it?

  • http://brajeshwar.com/ Brajeshwar

    Peter, it works atleast on IE 5, 5.5, 6.0 on Windows XP/2000 and on the latest release of Firefox, Mozilla, Opera, Netscape 7.2. All those are the browsers I always test on my two machines running 2K and XP. The suspect of of IEs on Mac, if somebody can confirm where and on which version of IE/Mac hits the snag, then it will be helpful.
    (I use to test on IE 4.0 too but I stop testing against it recently)

  • http://brajeshwar.com/ Brajeshwar

    Peter, it works atleast on IE 5, 5.5, 6.0 on Windows XP/2000 and on the latest release of Firefox, Mozilla, Opera, Netscape 7.2. All those are the browsers I always test on my two machines running 2K and XP. The suspect of of IEs on Mac, if somebody can confirm where and on which version of IE/Mac hits the snag, then it will be helpful.
    (I use to test on IE 4.0 too but I stop testing against it recently)

  • http://www.exposuremanager.com/ Donovan Janus

    It works in Safari and IE 5.2 on the Mac as well.

  • http://www.exposuremanager.com/ Donovan Janus

    It works in Safari and IE 5.2 on the Mac as well.

  • Rhesa

    it's actually in the html spec:

    class CDATA #IMPLIED -- space-separated list of classes --

    see http://www.w3.org/TR/html401/sgml/dtd.html (Yes, html 4.01) at the core attrs section.

  • Rhesa

    it's actually in the html spec:

    class CDATA #IMPLIED -- space-separated list of classes --

    see http://www.w3.org/TR/html401/sgml/dtd.html (Yes, html 4.01) at the core attrs section.

  • http://www.exposuremanager.com/ Donovan Janus

    doh! I need better glasses... seems Safari does not support it though IE 5.2 does.

    Sorry for the confusion...

    (and it is according to the HTML spec BTW) (hat tip, Rhesa Rozendaal: http://www.rhesa.com)

  • http://www.exposuremanager.com/ Donovan Janus

    doh! I need better glasses... seems Safari does not support it though IE 5.2 does.

    Sorry for the confusion...

    (and it is according to the HTML spec BTW) (hat tip, Rhesa Rozendaal: http://www.rhesa.com)

  • http://brajeshwar.com/ Brajeshwar

    I may sound a but dumb in the (x)html/css world, but may ask who is Rhesa Rozendaal (http://rhesa.com/)

  • http://brajeshwar.com/ Brajeshwar

    I may sound a but dumb in the (x)html/css world, but may ask who is Rhesa Rozendaal (http://rhesa.com/)