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.
The question, the details, the next step.
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.
- [S1] Using media queries — CSS | MDN developer.mozilla.org · Checked
- [S2] Introduction to the CSS cascade — CSS | MDN developer.mozilla.org · Checked
