How to combine class and ID in CSS selector?

CssCss Selectors

Css Problem Overview


If I have the following div:

<div class="sectionA" id="content">
    Lorem Ipsum...
</div>

Is there a way to define a style that expresses the idea "A div with id='content' AND class='myClass'"?

Or do you have simply go one way or the other as in

<div class="content-sectionA">
    Lorem Ipsum...
</div>

Or

<div id="content-sectionA">
    Lorem Ipsum...
</div>

Css Solutions


Solution 1 - Css

In your stylesheet:

div#content.myClass

Edit: These might help, too:

div#content.myClass.aSecondClass.aThirdClass /* Won't work in IE6, but valid */
div.firstClass.secondClass /* ditto */

and, per your example:

div#content.sectionA

Edit, 4 years later: Since this is super old and people keep finding it: don't use the tagNames in your selectors. #content.myClass is faster than div#content.myClass because the tagName adds a filtering step that you don't need. Use tagNames in selectors only where you must!

Solution 2 - Css

There are differences between #header .callout and #header.callout in css.

Here is the "plain English" of #header .callout:
Select all elements with the class name callout that are descendants of the element with an ID of header.

And #header.callout means:
Select the element which has an ID of header and also a class name of callout.

You can read more here css tricks

Solution 3 - Css

There's nothing wrong with combining an id and a class on one element, but you shouldn't need to identify it by both for one rule. If you really want to you can do:

#content.sectionA{some rules}

You don't need the div in front of the ID as others have suggested.

In general, CSS rules specific to that element should be set with the ID, and those are going to carry a greater weight than those of just the class. Rules specified by the class would be properties that apply to multiple items that you don't want to change in multiple places anytime you need to adjust.

That boils down to this:

 .sectionA{some general rules here}
 #content{specific rules, and overrides for things in .sectionA}

Make sense?

Solution 4 - Css

Well generally you shouldn't need to classify an element specified by id, because id is always unique, but if you really need to, the following should work:

div#content.sectionA {
    /* ... */
}

Solution 5 - Css

Ids are supposed to be unique document wide, so you shouldn't have to select based on both. You can assign an element multiple classes though with class="class1 class2"

Solution 6 - Css

You can combine ID and Class in CSS, but IDs are intended to be unique, so adding a class to a CSS selector would over-qualify it.

Solution 7 - Css

I think you are all wrong. IDs versus Class is not a question of specificity; they have completely different logical uses.

IDs should be used to identify specific parts of a page: the header, the nav bar, the main article, author attribution, footer.

Classes should be used to apply styles to the page. Let's say you have a general magazine site. Every page on the site is going to have the same elements--header, nav, main article, sidebar, footer. But your magazine has different sections--economics, sports, entertainment. You want the three sections to have different looks--economics conservative and square, sports action-y, entertainment bright and young.

You use classes for that. You don't want to have to make multiple IDs--#economics-article and #sports-article and #entertainment-article. That doesn't make sense. Rather, you would define three classes, .economics, sports, and .entertainment, then define the #nav, #article, and #footer ids for each.

Solution 8 - Css

use: tag.id ; tag#classin the tag variables in your css.

Solution 9 - Css

Continuing Ajm's answer:

For a dynamic id or class selection combination ->

div[id*='**myStandarKey-**'].myClass{

}

/* This is to validate something like: */

div[id*='myStandarKey-**12**'].myClass{

}

/* AND */

div[id*='myStandarKey-**13**'].myClass{

}

/* etc. */

Solution 10 - Css

.sectionA[id='content'] { color : red; }

Won't work when the doctype is html 4.01 though...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLarsenalView Question on Stackoverflow
Solution 1 - CssajmView Answer on Stackoverflow
Solution 2 - CssReza AbbasiView Answer on Stackoverflow
Solution 3 - CssGabriel HurleyView Answer on Stackoverflow
Solution 4 - CssBlixtView Answer on Stackoverflow
Solution 5 - CssBen HughesView Answer on Stackoverflow
Solution 6 - CssEric WendelinView Answer on Stackoverflow
Solution 7 - CssGregView Answer on Stackoverflow
Solution 8 - CssLan...View Answer on Stackoverflow
Solution 9 - CssAntonisView Answer on Stackoverflow
Solution 10 - CssRuss HuntingtonView Answer on Stackoverflow