-1

Is there any difference between those two methods?

#myId, .myClass p, #myidTwo p:first-child a {
color;blue;
}

versus:

#myId {
color:blue;
}

.myClass p {
color:blue;
}

#myidTwo p:first-child a {
color:blue;
}

Can I target classes and ID's at the same time?

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • 1
    http://stackoverflow.com/questions/1028248/how-to-combine-class-and-id-in-css-selector have a look – Manish Sep 08 '16 at 03:52
  • Not sure what you mean. But I think you are doing OK. – vothaison Sep 08 '16 at 03:52
  • 1
    Both versions do the same thing. Use the comma to chain together multiple selectors that have the same styles. Read about [**grouping**](https://www.w3.org/TR/CSS22/selector.html#grouping) in the CSS specification. – Michael Benjamin Sep 08 '16 at 03:52
  • 1
    did you try it? – zzzzBov Sep 08 '16 at 03:59
  • What would you imagine the difference might be? This behavior is the essence of grouped selectors, described quite clearly [here](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_Started/Readable_CSS#Grouped_selectors). –  Sep 08 '16 at 04:39
  • Possible duplicate of [What do commas and spaces in multiple classes mean in CSS?](http://stackoverflow.com/questions/3344284/what-do-commas-and-spaces-in-multiple-classes-mean-in-css) –  Sep 08 '16 at 04:40

1 Answers1

2

Yes both of those methods are valid. The first having the advantage of having a single point to change the color on multiple elements, the second has the advantage of greater flexibility. It depends on your needs what method is best.

Alex
  • 1,535
  • 2
  • 16
  • 26