Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why media queries has less priority than no media queries css

Tags:

html

css

I want to know why the media queries has less priority than normal css? How to work around to make the media queries more important?

@media screen and (min-width: 100px) and (max-width: 1499px) {    .logo img {       width: 120%;    }    }  .logo img{      width: 100%;  }
<div class="logo">    <a href="/#!/"><img src="http://www.menucool.com/slider/jsImgSlider/images/image-slider-2.jpg" alt="image"></a>  </div>
like image 963
Alvaro Joao Avatar asked Sep 14 '15 21:09

Alvaro Joao


People also ask

Does it matter if the media query comes first in CSS?

It means that, if you apply two rules that collide to the same elements, it will choose the last one that was declared, unless the first one has the ! important marker or is more specific (e.g. html > body vs just body , the latter is less specific). and the same window width, the background will be red.

What is the benefit of using use media query instead of CSS?

Media queries allow you to not only vary viewport dimensions based on screen size, but they can also help you set different style properties for different devices, including color schemes, font styles, motion settings and animations, borders and spacing, and almost any other CSS property you can think of.

Does order matter in media query?

Media queries add no specificity to the selectors they contain, but source order still matters. The above will work because they are ordered correctly. Swap that order and at browser window widths above 800px the background would be red, perhaps unintuitively.

Is it important to override media queries?

Media queries and @media rules do not affect the behavior of ! important in any way, because they do not have any effect on any part of the cascade.


1 Answers

This has to do with the way the Cascade in CSS works. When two conflicting rules target the same element, the browser uses the rules of the cascade to determine which one to apply.

Selector specificity is the most important part of this: styles with a more specific selector will override those with a less-specific selector... but media queries do not change the specificity of your selectors. This means that your two selectors have the same specificity. When that happens, the one appearing later in your stylesheet will override the earlier one.

Your easiest and best fix is to swap the order of your rulesets:

.logo img{     width: 100%; }  @media screen and (min-width: 100px) and (max-width: 1499px) {   .logo img {      width: 120%;   } } 

This way, the media query comes later, and will override the earlier rule when the media query matches the viewport size.


If that's not an option for some reason, you will need to increase the selector specificity of the rule you want to win. Changing it to the following would work:

@media screen and (min-width: 100px) and (max-width: 1499px) {   .logo img {      width: 120%;   }  } .logo a img{     width: 100%; } 

This way the selector now has two tags and a class, or [0,1,2], making it more specific than one tag and one class, or [0,1,1] (the zero in each of those indicates no ids, which are highly specific).


Do not use !important to fix specificity issues like this. If you need to override the style again elsewhere, the only way to do it is to add another !important. This will eventually lead to !importants all over the place, and then you will still need to deal with the specificity of the selectors.

like image 162
keithjgrant Avatar answered Sep 22 '22 09:09

keithjgrant