1

I'm new to css and I'm experimenting with css injections, where I change attributes of a homepage on my browser.

I have an element b which has an ancestor a ( not directly, there a few steps between).

I read multiple times that by using a space between classes e.g. .a .b{} you select all objects of class b that are an descendant of an object of class a.

So injecting.a .b{color: #ffffff;} is changing the color of my object to white as expected. But only using .b{color: #ffffff;} is not, which I don't understand, as it should affect all objects of class b, no matter of their ancestors?

Tobi La
  • 23
  • 2
  • Is there anything else changing the colour of b? In CSS, more specific styling overrides less specific styling. – Weirdali Apr 24 '21 at 14:18
  • 1
    Specificity of `.a .b{color: #ffffff;}` is more as compared to `.b{color: #ffffff;}` – decpk Apr 24 '21 at 14:20

1 Answers1

1

That is most likely because of selector specificity. Your .b is being applied, but there's another style overwriting it. You can inspect element and see what is the style being applied. If you want to make sure it's being applied, you can use .b {color: #fff !important;}

Edit: !important should be used as last resource, its not considered a good practice

iagowp
  • 2,093
  • 1
  • 19
  • 28
  • Didn't know about that, thank you very much! adding !important did it for me. – Tobi La Apr 24 '21 at 14:36
  • 1
    @TobiLa you do not need to use `!important`. That should be a last resort. Read up on specificity: https://stackoverflow.com/questions/2809024/how-are-the-points-in-css-specificity-calculated – disinfor Apr 24 '21 at 14:38
  • @TobiLa, yes I forgot to mention that !important is not a good practice! It's a last resort – iagowp Apr 24 '21 at 15:18
  • 1
    I guess it's okay for my css injection project, as I want to make sure that my injected params get applied no matter what (Currently experimenting with adding a dark mode to a website not providing one) But I keep that in mind for everything else, thanks! – Tobi La Apr 24 '21 at 16:59