Defining A CSS Selector That Requires A Multi-Class Union

Posted May 4, 2007 at 2:36 PM by Ben Nadel

Tags: HTML / CSS

Glen Lipka, user interface expert, just taught me the coolest little CSS trick (well not really a trick - more like a CSS fact that I didn't know). I had recently learned that an element could inherit the CSS rules from two different classes:

  • <div class="one two">
  • I get the CSS styles from both
  • div.one and div.two classes.
  • </div>

That in and of itself was very cool and useful. But, what Glen taught me takes that to the next level; apparently, you can define a CSS selector that only applies to a node that has multi-class CSS inheritance. In the following demo (which was really just a test for me to see this bad boy in action), I am creating three paragraphs. The first two get their own class. The third inherits properties of the previous two classes AS WELL AS properties that ONLY apply to it:

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>CSS Test</title>
  •  
  • <style type="text/css">
  •  
  • p {
  • padding: 5px 5px 5px 5px ;
  • }
  •  
  • p.one {
  • border: 1px solid black ;
  • }
  •  
  • p.two {
  • background-color: gold ;
  • }
  •  
  • /* ---- Here's where is gets crazy ---- */
  • p.one.two {
  • color: white ;
  • }
  •  
  • </style>
  • </head>
  • <body>
  •  
  • <p class="one">
  • Only One
  • </p>
  •  
  • <p class="two">
  • Only two
  • </p>
  •  
  • <p class="one two">
  • Both One And Two
  • </p>
  •  
  • </body>
  • </html>

Viewing the above in a browser gives you the following:


 
 
 

 
Multi-Class CSS Selector  
 
 
 

Notice that the third paragraph gets the border from p.one, the background color from p.two, and then gets its own font color from the CSS selector:

  • p.one.two {}

How cool is that? I am sure a lot of people know this, but this is waaay cool. Man, I love CSS; it just makes the development process so much easier.



Reader Comments

May 4, 2007 at 2:46 PM // reply »
46 Comments

Credit where credit is due. I just found out an hour ago.

Originally from Scott Sauyet.
http://scott.sauyet.com/


May 4, 2007 at 3:16 PM // reply »
56 Comments

Ben,

Have you taken your code and viewed it in IE 6.0 yet? I'll bet you'll be surprised that the "Only two" text color will be white.


May 4, 2007 at 3:28 PM // reply »
11,246 Comments

oooh crap! You are right. It's funny, cause I tested that in IE6 too :) I think I was just so excited that I didn't notice the font color change. It does this on IE5 on Mac as well (ha ha ha ha ha... IE 5 on mac).

Hmmm, that is lame. I don't have IE7 yet, so can't test that.

Thanks for pointing that out.


May 4, 2007 at 4:17 PM // reply »
56 Comments

http://meyerweb.com/eric/css/tests/multiclass.html

:)


May 4, 2007 at 4:30 PM // reply »
11,246 Comments

Awesome test setup. Stupid IE :)


May 5, 2007 at 5:11 PM // reply »
172 Comments

It's not that this doesn't work in IE6 at all, it's just very buggy, so I avoid it entirely. It works fine in IE7. As do more advanced CSS 2 selectors.


May 5, 2007 at 6:12 PM // reply »
11,246 Comments

Good to know it works nicely in IE7. I haven't taken the jump to upgrade yet (as some of us in the office need to have IE6 for testing purposes).


May 6, 2007 at 2:15 AM // reply »
172 Comments

FYI, in case you haven't already seen it, here's the "official" Microsoft solution for running IE6 and 7 side by side:

http://blogs.msdn.com/ie/archive/2007/04/17/ie7-virtual-pc-image-and-ie6-virtual-pc-image-refresh.aspx


May 8, 2007 at 4:48 AM // reply »
1 Comments

Try using this more simply for the technique to work in IE6. If you do like this:

p.red {color: red;}
p.yellow {color: yellow;}

<p class="red yellow">think this works in IE6</p>

I'd appreciate help with testing as I have installed IE7 now. IE5 for mac shouldn't be a concern anymore, it's daft!


May 8, 2007 at 12:30 PM // reply »
46 Comments

Unfortunately, This doesn't achive the goal. We want a CSS statement that acts as an AND operation. In other words, we want certain classes to be applied only if the element has BOTH classes.

so for your example <p class="red yellow">.
Imagine if you had:
p.red {color: red;}
p.yellow {color: yellow;}
p.red.yellow {color:orange} /* works in everything but IE6 */

Its the AND statement that would be so powerful.

Glen


May 8, 2007 at 2:59 PM // reply »
172 Comments

Obviously this wouldn't be the best solution in most cases, but if you really wanted you could use JavaScript to apply a third class to elements with the 2 classes in question. E.g., JavaScript turns class="one two" into class="one two one-two", and your CSS selector changes from ".one.two" to ".one-two" or even ".one.two, .one-two". That would work everywhere including IE6.


May 11, 2007 at 3:45 AM // reply »
2 Comments

Good point on JavaScript, no need to bloat your code with IE specific stuff. Personally I never needed to select both classes together tough.


May 11, 2007 at 8:38 AM // reply »
11,246 Comments

@Zoffix,

While I have never done that before (and didn't know that I could), it is one of those things where when I see it, I know that I could have used it somewhere :)


Apr 13, 2009 at 7:40 AM // reply »
1 Comments

Wow, that is a VERY NICE feature I didn't know of.
Too bad it doesn't work in IE6, but at least it makes my CSS for the other browsers a LOT MORE POWERFUL.

Thanks a lot,
Martien


Jim
Feb 17, 2010 at 11:07 AM // reply »
1 Comments

I'll see your CSS and raise you one:

.processing td.description {
background: #DADADA url(images/ajax-loader.gif) no-repeat center;
}

This applies only to the TD with class 'description', that is in the TR with class 'processing'. The other TDs in the row have additional properties, but I wanted the animated background only in one of the TD elements.

Thanks for the post, you got me most of the way to this one.


Feb 17, 2010 at 9:09 PM // reply »
11,246 Comments

@Jim,

Nested classes are very cool and extremely powerful!


Apr 16, 2010 at 7:20 PM // reply »
1 Comments

Thank you so much, it's very very very useful, you cool!


Jun 4, 2010 at 4:48 AM // reply »
1 Comments

great! thanks for the helpful information. keep it up.


May 19, 2011 at 12:55 PM // reply »
1 Comments

This is a very exciting time for us web developers, what with HTML 5 finally becoming a viable option. I've been switching all my applications over lately. Unfortunately some of the eyecandy causes performance problems on touch screen devices. So here's my idea, combine this concept with modernizr.js and you can get a css rule like:

html.no-touch.boxshadow div.panel{
box-shadow: 10px 10px 5px #888;
}


Feb 12, 2012 at 5:33 PM // reply »
1 Comments

Are you aware that your name is the anagram of ben laden ?


Jul 2, 2012 at 8:10 AM // reply »
1 Comments

Wooow...this is just fantastic, its everything I was looking for. A lot of javascripts going to trash now!! :D


Jul 29, 2012 at 7:46 PM // reply »
1 Comments

that's not a union selector, it's an exclusion selector - `.one.two {}` targets one AND two, versus one OR two

(and you're SEO'd very strongly for "css union selector" which is annoying)


Nov 9, 2012 at 10:45 PM // reply »
1 Comments

That ".a.b" a AND b selector is not mentioned in my CSS "missing manual" 2 book (couldn't find it anyway). How come? This is a great - really great - addition to the "traditionally" used selectors.

I needed such a feature to simplify some rules and wondered if it was even possible until I found your site. The great news is it seems to work in all browsers! (who cares about ie6 anymore?)



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 24, 2013 at 5:39 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam Oops! My mistake! I hadn't gotten that far in my testing - I'm still baby stepping my way through the process. ... read »
May 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools