Magiranu Magiranu - 26 days ago 13
CSS Question

CSS ID-Selector in combination with Class-Selector not working

I'm not able to use an ID-selctor in combination with a class-selector. It baffles me as I also used a CSS Validator to check for erors. So, why is only the latter working in my code?

This is my Code:



#result .red1 {
color: #FF0000;
}

.red2 {
color: #FF0000;
}

<span id="result" class="red1">Test of Color</span>
<br>
<span id="result" class="red2">Test of Color</span>




Answer Source

Remove the space between the ID and the class selector

#result.red1 {
  color: #FF0000;
}

"#result .red1" means find the class red1 INSIDE the result element. It is better not to use ID as CSS selectors though.