Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do parent selectors take precedence over child selector in CSS?

Tags:

css

I had a question about how CSS selectors work between parent and children, and which one would take precedence over the other.

<div class="red">
  <div class="blue">
    <div class="green">
    </div>
  </div>
</div>

If you then have

   .red .green{
    border: 1px solid red;
    }

    .blue .green{
    border: 1px solid blue;
    }

Which one would take effect? And to override a CSS style does it have to be as specific a selector as the one you're trying to override?

like image 203
user2128225 Avatar asked Oct 20 '25 07:10

user2128225


1 Answers

You should read up on specificity.

To answer your immediate question, all your selectors carry the same specificity, so in the case of .green, the last rule takes precedence: your border would be blue.

like image 145
André Dion Avatar answered Oct 21 '25 20:10

André Dion