Home  >  Q&A  >  body text

Decoding CSS selector priority/specificity

I want to understand how CSS selectors handle property conflicts. How to select one property over another?


div {
  background-color: red;
}

div.my_class {
  background-color: black;
}

div#my_id {
  background-color: blue;
}

body div {
  background-color: green;
}

body>div {
  background-color: orange;
}

body>div#my_id {
  background-color: pink;
}
<div id="my_id" class="my_class">hello</div>


How does selector priority work?

P粉715274052P粉715274052334 days ago423

reply all(2)I'll reply

  • P粉852114752

    P粉8521147522023-10-20 14:12:01

    In order, 1 is the lowest specificity and 5 is the highest specificity. https://youtu.be/NqDb9GfMXuo Demo details will be displayed.

    reply
    0
  • P粉384366923
  • Cancelreply