Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why does a later CSS rule lose even though it appears last?

Quick answer

A later rule does not automatically win: the cascade considers origin and importance before specificity, and declarations are compared within the relevant cascade context. Normal unlayered author styles also outrank normal layered author styles. If competing declarations remain tied, later steps such as scoping proximity and source order can decide the result. Inspect the matched declarations in your browser’s Styles panel instead of guessing from file order. [S1][S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I put a new color rule at the bottom of my CSS, but the element still has the old color. Why?

Geez

Because “last in the file” is only one possible deciding factor. The cascade considers origin and importance before specificity; specificity is compared among declarations in the origin and layer that has precedence. If declarations are otherwise tied, later cascade steps—including scoping proximity and order of appearance—may decide the result. [S1]

Start by checking which declarations actually match the element. In your browser’s developer tools, open the element’s Styles panel and find the property, such as color. Look for the winning declaration and any competing declarations crossed out. The panel can show declarations that matched but lost through cascade, specificity, or source order. [S2]

Follow-up question

Can you show me how a later rule can lose because of cascade layers?

Geez

Suppose a paragraph is inside an element with class card, and both rules match that paragraph:

@layer components {
  .card p { color: navy; }
}

.card p { color: tomato; }

Even though the unlayered rule appears later, this example illustrates a layer difference: normal unlayered author styles outrank normal layered author styles. [S2] Moving the layered rule later would not, by itself, change that precedence.

For a practical fix, first decide which rule should own the style. You might put the intended rule in the appropriate layer or remove an obsolete competing declaration. Avoid changing selectors until you have confirmed the actual conflict.

Follow-up question

I checked the layers. What else should I compare?

Geez

Check origin and importance before comparing specificity. Specificity is compared only among declarations in the origin and layer that has precedence. If declarations are otherwise tied, later cascade steps such as scoping proximity and order of appearance can decide the result. [S1]

In the Styles panel, inspect the winning and crossed-out declarations, including their cascade context. Comparing selectors alone may not settle the conflict if an earlier cascade step already determines which declaration takes precedence. The precise outcome depends on the declarations and context on your page, so use the panel’s evidence rather than guessing from line order.

Follow-up question

What should I do step by step, and how would that look for my example?

Geez

Use this order of investigation:

  1. Select the affected element and identify the exact property that looks wrong.
  2. In Styles, find the declaration taking effect and any crossed-out declarations for that property. [S2]
  3. Check origin and importance before comparing specificity. [S1]
  4. Check the layer context, including whether a normal author declaration is unlayered. Normal unlayered author styles outrank normal layered author styles. [S2]
  5. Compare specificity among declarations in the relevant origin and layer. If they are otherwise tied, consider later cascade steps such as scoping proximity and source order. [S1]
  6. Make one focused change and recheck the same property in Styles.

For the example above, if the unlayered .card p rule is active and the layered declaration is crossed out, the layer difference explains why the later rule wins. If a different declaration is active, follow the cascade context shown for that conflict instead. Choose a fix for the cause you identify; do not add !important just because the first attempt failed.

A practical checklist

  • Select the affected element and name the exact property that is wrong.

  • Find the active and crossed-out matching declarations in the Styles panel.

  • Check origin and importance before comparing specificity.

  • Check layer context, including whether a normal author declaration is unlayered.

  • Compare specificity in the relevant cascade context, then consider later tie-breakers.

  • Make one focused edit and confirm the winning declaration in the panel.

A few more useful answers

Does moving a rule to the bottom of the stylesheet ever help?

It can help when competing declarations are otherwise tied at the cascade steps that matter. It will not by itself overcome an earlier precedence difference. [S1]

Why are some matching declarations crossed out in developer tools?

The Styles panel can show declarations that matched but lost because of cascade, specificity, or source order. Use the active declaration and crossed-out rules to investigate the conflict. [S2]

Does a more specific selector always win?

No. Origin and importance are considered before specificity, and specificity is compared among declarations in the origin and layer that has precedence. [S1]

Sources

References used for this guide.

  1. [S1] Specificity — MDN
    developer.mozilla.org · Checked
  2. [S2] Cascade layers — MDN Learn web development
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work