Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why does a CSS rule inside a media query not apply?

Quick answer

Check the rule in this order: confirm the media condition matches the current viewport or other feature, confirm the selector matches the element, then inspect competing declarations and cascade precedence. A nonmatching media query contributes no applicable style; a matching rule can still lose to a declaration with higher cascade precedence. Use a minimal test rule and change one condition at a time.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

My CSS rule is inside an @media block, but I don’t see the change. What should I check first?

Geez

Check three gates, in order: the media condition must match, the selector must match the element, and the declaration must win against competing declarations. A media query applies only when its media type matches and all its feature expressions are true. A width condition, for example, tests the browser viewport width—not necessarily the width of the element you are styling. If the condition does not match, the styles inside it do not apply. [S1]

Start by reading the condition literally. Compare a width threshold with the current viewport width, or check the relevant feature for a different kind of condition. Then inspect the element and selector before changing specificity or adding !important.

Follow-up question

How can I tell whether the media condition itself is the problem?

Geez

Reduce the test to one condition and one obvious declaration. For example, assuming the goal is to make a box blue when the viewport is at most 600 pixels wide:

@media (max-width: 600px) {
  .notice {
    color: blue;
  }
}

Check the actual viewport width and whether the box has the notice class. If the viewport is wider than 600 pixels, that condition is false, so this rule is not eligible to apply. If the condition becomes true only after resizing, that points toward the condition rather than the property value. This example illustrates the test; it does not establish how a particular page is configured.

Follow-up question

The viewport meets the condition. What should I check about the selector and the rule’s location?

Geez

Inspect the exact element you expect to change and compare it with the selector. In the example, .notice is the selector to check against the intended element. A selector that refers to a different element or does not match the intended one will not make that element a candidate for the declaration. [S2]

Also check that the declaration is inside the media block you intend to test. Temporarily simplify the example to the media block above, using the selector and property from your page. If that works, add back the surrounding CSS in small pieces to narrow down what changes the result.

Follow-up question

The selector matches and the condition is true, but another style still appears. How do I find what wins?

Geez

Compare the competing declarations for the same element and property. The cascade filters declarations for selector match and relevant media conditions, then considers origin and importance, specificity, scoping proximity, and order of appearance. So a matching declaration inside a media query can still lose; source order decides only when the earlier cascade criteria tie. [S2]

A practical sequence is: identify the declaration that supplies the visible value; check whether each rule is eligible; then compare importance and specificity before relying on which rule appears later. Test one edit at a time, and remove temporary test styles afterward. Without the stylesheet and the exact element, it is not possible to identify which declaration wins on your page.

A practical checklist

  • Write down the media condition and check whether every part matches the current viewport or feature.

  • Confirm the expected element matches the selector, including its class, ID, or other selector details.

  • Check that the declaration is inside the intended media block.

  • Find other eligible declarations for the same element and property; compare cascade precedence before source order.

  • Test a minimal version, then restore surrounding CSS in small pieces and remove temporary test rules.

A few more useful answers

Does a matching media query guarantee its declaration will be used?

No. The selector must also match, and the declaration must win against competing declarations under the cascade. [S2]

If the rule appears later in the stylesheet, should it win?

Not necessarily. Order of appearance is considered after earlier cascade criteria, including origin, importance, and specificity. [S2]

Does a width media condition check the width of the element?

A width condition tests the browser viewport width, according to the reference; it does not by itself establish the element’s width. [S1]

Sources

References used for this guide.

  1. [S1] Using media queries — CSS | MDN
    developer.mozilla.org · Checked
  2. [S2] Introduction to the CSS cascade — CSS | 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