Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS specificity, Media Queries and min-width

I'm redesigning my blog with responsive web design in mind, and the "mobile first" method - In short I'm trying to use min-width to avoid any kind of replication or css.. no display:none's etc..

My problem is that when I do need to over-write a CSS value, the lower min-width takes precedence. Example:

@media only screen and (min-width: 600px) {     h2 { font-size: 2.2em; } }  @media only screen and (min-width: 320px) {     h2 { font: normal 1.7em/2.1em Helvetica, sans-serif; } } 

I'd expect when I'm in resolutions of 600px and above to get a 2.2em h2, but instead I get 1.7em.. In my Dev tools I see that the 2.2em declaration is there, but the other one takes precedence.. It doesn't make sense!

Could I keep using min-widths and effectively overwrite declarations in higher resolutions without using stronger selectors or max-width..?

like image 886
George Katsanos Avatar asked Apr 24 '12 22:04

George Katsanos


People also ask

Do media queries affect specificity?

The media query does not affect specificity. If it feels like selectors are increasing specificity and overriding other styles with the same selector, it's likely just because it comes later in the stylesheet.

What is min-width in CSS media query?

Taking a look at the CSS file, you'll notice that the media query has a minimum width of 320px and a maximum width of 576px . This just means that all the styles that will go into this rule will only be applicable to devices with extra-small and small widths.

Should I use max width or min-width in media queries?

If you are designing your website for smaller devices first then set your initial breakpoints with min-width, whereas, if you are designing for larger devices first then use max-width. This post discusses min-width, and max-width media features in detail along with relevant examples.

Should I use min-width or max width in CSS?

With min-width, styles START and continue forever as long as min-width is met, and no max-width is specified. Max-width is the maximum width at which a style will continue to be applied. After that, the style will STOP being applied. (Have to be ordered from largest to smallest to work properly, regular styles first).


1 Answers

I'd expect when I'm in resolutions of 600px and above to get a 2.2em h2, but instead I get 1.7em.. In my Dev tools I see that the 2.2em declaration is there, but the other one takes precedence.. It doesn't make sense!

It makes sense. If the media fulfills min-width: 600px, then it should also fulfill min-width: 320px; in other words, anything that's at least 600 pixels wide is also at least 320 pixels wide, because 600 is greater than 320.

Since both media queries evaluate to true, the rule that occurs last takes precedence in the cascade, making your code equivalent to this:

h2 { font-size: 2.2em; } h2 { font: normal 1.7em/2.1em Helvetica, sans-serif; } 

That explains why the 2.2em appears in your developer tools but doesn't take apparent effect.

The easiest fix is to switch your @media blocks around so your rules cascade in the correct order:

@media only screen and (min-width: 320px) {     h2 { font: normal 1.7em/2.1em Helvetica, sans-serif; } }  @media only screen and (min-width: 600px) {     h2 { font-size: 2.2em; } } 
like image 93
BoltClock Avatar answered Sep 26 '22 01:09

BoltClock