Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple classes in CSS Selector

I see a selector like this,

.class1 .class2 .class3 { } 

What does this mean?

I've used multiple class selectors without spaces. Space means descendant but it doesn't make sense for classes.

like image 337
ZZ Coder Avatar asked Dec 05 '10 03:12

ZZ Coder


People also ask

Can I select multiple classes in CSS?

We aren't limited to only two here, we can combine as many classes and IDs into a single selector as we want.

How do you select an element with multiple classes?

Use the getElementsByClassName method to get elements by multiple class names, e.g. document. getElementsByClassName('box green') . The method returns an array-like object containing all the elements that have all of the given class names.

How do I select multiple classes in SCSS?

SASS/SCSS code to select multiple select classes in the same item. In SCSS, parent selector & symbol is used. This & will be resolved side by side after compilation for CSS.


2 Answers

Let's say there's a page with the following markup,

<div class="class1">   <div class="class2">     <div class="class3">       Some page element(s).     </div>   </div> </div> 

The CSS you provided would style all elements under class3, which are under class2, which are under class1.

i.e. let's say this was the styling,

.class1 .class2 .class3{   color:red; } 

It would render the text as red, which is the equivalent of the following,

div.class1 div.class2 div.class3 {   color:red; } 

Finally, the following would do nothing,

.class1.class2.class3{   color:red; } 

Edit: If the markup instead was the following,

<div class="class1 class2 class3">       Some page element(s). </div> 

It would work and render the text in red.

Note: < IE7 might have issues with the above...

http://www.thunderguy.com/semicolon/2005/05/16/multiple-class-selectors-in-internet-explorer/ http://www.w3.org/TR/2004/CR-CSS21-20040225/selector.html#class-html

like image 138
Amit G Avatar answered Sep 25 '22 10:09

Amit G


The other answers provide you with the explanation you need; I'll chip in with a practical use case just to feed anyone's curiosity.

A common real-world use case for multiple class selectors separated by descendant combinators is when a site has a different body class for certain pages, or certain devices.

For example, consider this markup of a simple web site. Besides the header and footer, it also has a content column and two sidebars:

<!-- DTD, html, head... --> <body>     <div id="wrap">         <div id="header">           <h1>My Site</h1>         </div>          <div id="content">           <!-- ... -->         </div>          <div id="side1" class="sidebar"><!-- ... --></div>         <div id="side2" class="sidebar"><!-- ... --></div>          <div id="footer">           <p>Copyright LOLOLOL</p>         </div>     </div> </body> </html> 

The default setup might be to arrange #content between the .sidebars, done with some floating trickery I won't get to here.

On a mobile device, besides resizing the whole thing for the small resolution, perhaps the designer wants to do away with the sidebars to reclaim some horizontal screen estate. Aside from media queries, there's also the much simpler option to use server-side code to detect mobile browsers and tag body with a class accordingly, like this (ASP.NET C# example):

<% if (((HttpCapabilitiesBase) Request.Browser).IsMobileDevice) { %> <body class="mobi"> <% } else { %> <body> <% } %> 

That's where your example comes in handy. The designer just uses the following rule to hide the sidebars from mobile devices:

/* This would appear just beneath the .sidebar { ... } rule */  .mobi .sidebar {     display: none; } 

Of course, the same browser detection code could be used to hide the sidebar markup altogether, shaving page size and all that jazz, but again that's just another way of approaching this. I'm just giving a quick practical example of how multiple class selectors in a hierarchy can be used in CSS.

like image 40
BoltClock Avatar answered Sep 24 '22 10:09

BoltClock