Skip to content
CoursesWeb Forum

Why is my CSS rule ignored? How does specificity decide which rule wins?

Staff answerWritten by Teodora Vance · CSS Styling and Selectors0 replies

Short answer

When two rules set the same property on one element, the browser scores each selector and the higher score wins. An id counts most, then classes, attributes and pseudo-classes, then element names. If the scores tie, the rule written later wins. Inline styles and !important are handled separately, before specificity is even compared.

Written by Teodora Vance · Moderator: HTML, CSS and front end JavaScript ·
CiteCoursesWeb Forum, "Why is my CSS rule ignored? How does specificity decide which rule wins?", https://coursesweb.net/forum/t/why-is-my-css-rule-ignored-how-does-specificity-decide-which-rule-wins/, accessed 2026-11-17.
  1. Moderator: HTML, CSS and front end JavaScript ·

    A small example that shows the problem

    Say you want the intro paragraph to be red, and nothing happens.

    <div id="content">
      <p class="intro">Welcome to the page.</p>
    </div>
    
    .intro {
      color: red;
    }
    
    #content p {
      color: green;
    }
    

    The text is green. Both rules match the paragraph, and both want to set color. The browser does not look at which rule came first here, because the two selectors have different scores.

    How the score works

    Specificity is written as three numbers, A-B-C. The MDN specificity guide describes the same three columns: ids, then classes, attributes and pseudo-classes, then element types and pseudo-elements. The web.dev lesson on specificity says they are compared in order: a larger A value wins, and only if A ties does B get compared, then C.

    Selector Score (A-B-C) Why
    p 0-0-1 one element
    .intro 0-1-0 one class
    p.intro 0-1-1 one class, one element
    #content p 1-0-1 one id, one element
    a[href]:hover 0-2-1 one attribute, one pseudo-class, one element

    So #content p (1-0-1) beats .intro (0-1-0), because the id column is checked first. Ten classes would still lose to one id, since each column is compared separately and nothing carries over.

    The fix is to match the strength of the rule you want to win, not to make everything stronger:

    #content .intro {
      color: red;
    }
    

    That scores 1-1-0, which beats 1-0-1. Better still, if you control the HTML, avoid the id in the first rule and keep scores low across the whole file.

    Ties, inline styles and !important

    • A tie goes to the later rule. The web.dev lesson shows two rules with identical scores where the one lower in the file wins. Swap their order and the result swaps too.
    • Inline styles come first. MDN says an inline style="..." attribute overrides normal rules in your stylesheet. Many scripts and plugins add inline styles, which is why a rule can seem ignored only on some pages.
    • !important jumps the queue. MDN calls using it to beat specificity a bad practice. When two !important rules collide, specificity decides between them again.
    • :where() has a score of zero. It is handy for base styles you want to be easy to override later.

    Mistakes beginners make

    1. Adding !important to fix it. It works today and causes a harder problem next month, when the next rule needs to override that one.
    2. Copying a long selector from DevTools. A selector like body div.wrapper ul li a scores high, and everything after it needs a high score too. Prefer one class on the element.
    3. Blaming specificity when the rule never matches. A typo in the class name or a stylesheet that is not loading looks the same. Check that first. If your layout rule is ignored by a framework's .container div rule, see the container patterns in How do I put two divs side by side inside a container?.

    Check this in order

    1. Right-click the element and choose Inspect. In the Styles panel, find your declaration. If it has a line through it, another rule won. The panel shows which rule and from which file.
    2. If your rule is not listed at all, the selector does not match. Check spelling, the class on the element, and that the stylesheet link works (look in the Network tab).
    3. If the property has a yellow warning icon, the value is invalid, for example colour instead of color.
    4. Compare scores of the winning and losing selectors using the table above.
    5. If scores tie, move your rule below the other one or add a single class to your selector.
    6. Last, check for an inline style on the element, written in the HTML or set by JavaScript.

    For a gentle refresher on selectors themselves, the CoursesWeb CSS tutorials cover them from the start.

    0
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

CoursesWeb Forum. Questions and answers on learning web development: HTML, CSS, JavaScript, PHP, MySQL, Node.js and AI coding help.