280

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>
Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
Larsenal
  • 49,878
  • 43
  • 152
  • 220
  • 3
    if you're going to select by id then there is no need to select by class also, because id is unique so only one element will match. So by adding the class to the same selector is just extra typing – TStamper Jun 22 '09 at 16:57
  • 24
    @TStamper: Not necessarily. You're thinking only in terms of static documents. Case in point, you may have an `img#Inbox` icon on your page that is normally set to 50% opacity. However, when the user has a message, you want it to be set to 100% opacity, and the backend indicates this by adding an `active` class to the element. In such a scenario, it does make sense to have a selector that combines both ID and classname. – Lèse majesté Jun 07 '12 at 22:33
  • @TStamper: Also, doesn't having the more specific id+class combo selector gives it more weight/precedence than the id version alone? AFAIK, it should. (IOW, the id+class combo selector's rules will override the id combo selector's rules. (This was my reason/need for finding this thread just now.) – govinda Jan 01 '13 at 06:30
  • So, I am having a similar situation. I have for example many products that have descriptions but also would like specific control over individual products, so, can I do: `code`

    Product 1

    Product 2

    `code` This to allow general styling of all products but also allow individual styling for specific products?
    – Tamer Ziady Mar 28 '15 at 19:17

10 Answers10

414

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!

ajm
  • 19,795
  • 3
  • 32
  • 37
  • 34
    Specifying that the ID belongs to a div can only be detrimental to the rendering performance (albeit pretty much negligible) without any benefit to it as an ID is already unique. I'd go with #content.myClass instead to make it clean. – zrooda Mar 29 '12 at 13:26
  • 3
    `div.class#id` should be equivalent though less recommended; use more unique parts of selector first. – SF. Aug 26 '13 at 09:33
  • what if I have an input tag/element inside my div called #myID, and want to get to that? will that be `#myID.input` ? – nmz787 Apr 13 '22 at 16:59
  • 1
    Thank you. I had forgotten that I already had a class for one of my tags, and this came in handy for keeping the tag classname and just adding an ID. Thank you. – CodingEE Feb 20 '23 at 06:31
105

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

Reza Abbasi
  • 1,521
  • 2
  • 15
  • 21
5

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 {
    /* ... */
}
Blixt
  • 49,547
  • 13
  • 120
  • 153
5

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?

Gabriel Hurley
  • 39,690
  • 13
  • 62
  • 88
1

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. */
Kristian
  • 2,456
  • 8
  • 23
  • 23
Antonis
  • 11
  • 5
0

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.

Greg
  • 51
  • 1
  • You can actually use semantic HTML tags instead of IDs for most of the elements you mention. The header, footer, nav, section etc. tags exist for a reason. – Walter Schwarz Feb 04 '14 at 16:24
  • But indeed you should only use IDs for elements that should be unique, singletons. But even then its only really useful if you need to access that element via JavaScript, because using ID's makes your CSS too specific and difficult to later merge with other CSS and/or HTML. There is also the argument that ID's are faster then classes, but the difference is truly negligible. – Walter Schwarz Feb 04 '14 at 16:39
0

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

Lan...
  • 105
  • 1
  • 6
-1

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"

Ben Hughes
  • 14,075
  • 1
  • 41
  • 34
  • 2
    The class can change. Per example, you can have div#id.active and div#id.inactive. – Daniel Moura Jun 22 '09 at 16:49
  • fair enough, come to think of it, I have used this functionality, but I misunderstood what the desired behavior was. – Ben Hughes Jun 22 '09 at 16:58
  • This is not an answer. It is a question that should have been written as a comment on the original post so the original poster could answer it. There are reasons to do what they ask and if you need to know them, make your own SO question about it. – David Rector Sep 02 '23 at 21:04
-1
.sectionA[id='content'] { color : red; }

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

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
-1

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.

Eric Wendelin
  • 43,147
  • 9
  • 68
  • 92
  • That's a good point. I'm thinking if maybe you have a set of very general classes (setting things like float, height/width, maybe things like font) and you'd like to override something in them in this unique exception? – ajm Jun 22 '09 at 16:49
  • 5
    Unless you use the same stylesheet with different pages. :-) – Patrick McElhaney Jun 22 '09 at 17:16
  • 5
    Another example: when classes are added dynamically with Javascript (#optionalFields.enabled) – Patrick McElhaney Jun 22 '09 at 17:22
  • Even if you use the same stylesheet with different pages, it'd really be better to put a class on the body or have one style for the ID and then separate class definitions for the classes with the appropriate overriding styles. – Eric Wendelin Jun 22 '09 at 20:49
  • Yeah, I think it is fairly common to do this. e.g li#SomeTab.state-selected – Yablargo Feb 27 '13 at 18:00
  • As Lese majeste says, behaviour-based classes render this answer incorrect. Classes such as 'hover', 'highlight', 'selected' may be added dynamically. Selecting on both ID and Class is meaningful here, since the class is not permanently present. – Thomas W May 02 '13 at 01:39
  • This is a terrible answer since it is not an answer - it doesn't show HOW to do this - and it implies that there are no good reasons to do it, which there are. – David Rector Sep 02 '23 at 21:06