Learn more about CSS selector priority and specificity
P粉036800074
P粉036800074 2023-08-22 22:06:31
0
2
397

I want to understand how CSS selectors work in property conflicts. How is one attribute selected 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; }
hello


How does selector priority work?

P粉036800074
P粉036800074

reply all (2)
P粉739706089

In order, 1 is the lowest specificity and 5 is the highest.https://youtu.be/NqDb9GfMXuoDetails will be shown for demonstration.

    P粉384244473

    I'll just add a link here to the CSS 2.1 specification itself, and how browsers should calculate specificity:

    CSS 2.1 Section 6.4.3:

    If specificities are equal,CSS 2.1 Section 6.4.1:

    will be used

    Please note that this is discussed when the style is defined, not when used. If the specificities of classes.aand.bare equal, the class last defined in the stylesheet will win.

    ...

    and

    ...

    will Set in the same style according to the definition order of.aand.b.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!