Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why does a CSS declaration appear in the stylesheet but not take effect?

Quick answer

A declaration can be visible in a stylesheet but have no effect if its property or value is invalid or unrecognized, its selector does not match, or its rule is inactive. Even a valid, applicable declaration can lose in the cascade. Inspect the rule and the element’s computed value in browser developer tools, then check each possibility. A custom property can also produce an invalid value after substitution. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I can see color: bluish; in my CSS file, but the text isn’t bluish. Does the browser ignore the line just because it can’t understand the value?

Geez

That is one likely explanation. Browsers ignore a declaration when its property name or value is invalid or unrecognized, while continuing to parse later valid CSS; the stylesheet does not normally stop as if it had hit a JavaScript error. [S1]

For example, color: bluish; will not set the text color because bluish is not a recognized color value. Replace it with a valid value such as color: blue;. If you are unsure whether the declaration is being applied at all, temporarily try a value you know is valid for that property.

Also check the declaration’s punctuation and where it sits in the rule. In developer tools, select the affected element and inspect its rules: is the declaration recognized, crossed out, or absent from the rules applying to that element? These clues help distinguish a rejected declaration from one that is not applying for another reason.

Follow-up question

The property and value look right. How can I tell whether my browser supports them, or whether I just have a typo?

Geez

First compare the spelling and syntax with the property and value you intended to use. Then inspect the declaration in the browser’s developer tools. If it is rejected or not treated as valid, check whether that property and value are supported in the browser and context you are testing. A line in a source file alone does not establish that the browser accepts it.

You can narrow down the possibilities with a small test in the same page and browser: change only that declaration to a known-valid value for the same property. If the test value affects the element, investigate the original value’s spelling, syntax, or support. If it still has no effect, look beyond value support at the selector, any conditions on the rule, and the cascade. The test narrows the search; by itself, it does not prove the precise cause.

Follow-up question

Developer tools show the rule, but the computed value is different from the value I wrote. What should I check next?

Geez

A declaration can be valid yet fail to contribute because its selector does not match, its stylesheet or conditional rule is not currently applicable, or another valid declaration wins in the cascade. Browsers filter out declarations in those situations before determining the resulting value. If no declaration supplies a value, inheritance or an initial value may determine it. [S2]

Check in this order:

  1. Confirm that the selected element is the one you meant to style and that the rule’s selector matches it.
  2. Check whether the rule is inside a condition that currently applies.
  3. Look for other declarations of the same property and identify which one takes effect.
  4. Compare the computed value with the value you expected. It shows the result for the selected element, not just the text in the stylesheet.

For example, if a matching rule sets color: navy but another applicable declaration wins with color: black, the computed color can be black even though the navy declaration is present. The computed result alone does not explain why a declaration lost, so inspect the applicable rules too.

Follow-up question

What if the declaration uses a CSS custom property? Can I use the same checklist, and what should I check before changing the code?

Geez

Use the same checks, but also inspect the custom property’s value where it is defined and how it is substituted into the declaration. A declaration using a custom property can become invalid after substitution, unlike an ordinary declaration rejected during initial parsing. [S1] So the custom property appearing in the stylesheet does not by itself show that its eventual value is valid for the consuming property.

For example, if a rule uses color: var(--text-color);, check both that declaration and the value assigned to --text-color. Change one thing at a time, then check whether the consuming declaration is accepted and whether the computed color changes. The cause in a particular page depends on its code and browser, so use the rules and computed value there to narrow it down.

A practical checklist

  • Select the intended element in developer tools and confirm that the rule’s selector matches.

  • Check the property spelling, value syntax, and where the declaration sits in the rule.

  • Check whether the stylesheet or a condition containing the rule currently applies.

  • Look for other declarations of the same property and identify which one takes effect.

  • Inspect the computed value and, when using a custom property, trace the value used by var().

  • Change one thing at a time and recheck the rule and computed result.

A few more useful answers

Can a custom property cause a declaration to be invalid?

Yes. A declaration using a custom property can become invalid after the value is substituted, so check the value being substituted as well as the consuming declaration. [S1]

Does a different computed value always mean the declaration is invalid?

No. A declaration may be valid but not apply because its selector does not match or its rule is inactive, or it may lose in the cascade. Inspect the applicable rules as well as the computed value. [S2]

Sources

References used for this guide.

  1. [S1] CSS error handling — MDN
    developer.mozilla.org · Checked
  2. [S2] CSS property value processing — MDN
    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