Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Media Query Styles Not Overriding Original Styles

I'm attempting to use some media queries for a website I'm building. The problem I'm having however, is while the media query styles are actually being applied, they're being overridden. I can't for the life of me tell why because I'm using the same exact selectors. Can anyone point out something that I'm not seeing?

ORIGINAL CSS

#global-wrapper-outer > #global-wrapper-inner {     width: 85%;     height: 100%;     margin: 0 auto;     position: relative; }     #global-wrapper-outer > #global-wrapper-inner > nav {         background: #fff;         padding-bottom: 20px;         box-shadow: 0 4px 2px -2px gray;     } 

MEDIA QUERY CSS

@media screen and (max-width:1024px) {     #global-wrapper-outer > #global-wrapper-inner {         width: 100%;     }     #global-wrapper-outer > #global-wrapper-inner > nav {         display: none;     } } 

The second media query is working fine, where I set the nav to have a display of none. However, when I try to set the width of #global-wrapper-inner to 100% it doesn't apply. I can see the style being "applied" when I press F12 and select that element. However, the style itself is crossed out and not actually applied and it still has the original width of 85%.

like image 463
Ben Black Avatar asked Sep 26 '13 20:09

Ben Black


People also ask

Do media queries override?

The media query with the narrower max-width should fall later in the cascade than the media query with the wider max-width so it overrides properties assigned in the wider media query.

Why won't my media queries work?

If media queries work on desktop and not on mobile devices, then you most likely haven't set the viewport and default zoom. Note: You only need to add one of the code lines above, and usually, the first one does the job.

Do media queries work with style tags?

Media queries work fine inside style tags and always have. They don't work in style attributes as only property/values are allowed in style attributes. You should avoid using the style attribute anyway unless you have a special case reason.


1 Answers

The selectors in your original CSS have the same specificity as the selectors within your media queries (the first declarations are also targeting the same property - width) and because the media query rule set is being overridden I'm going to assume that it appears before the original rule set.

The second media query selector works because it's targeting a property that wasn't set in your original CSS, so specificity isn't relevant.

To have the first media query selector take precedence, prepend an ancestor element to it:

@media screen and (max-width:1024px) {     body #global-wrapper-outer > #global-wrapper-inner {          width: 100%;     }     #global-wrapper-outer > #global-wrapper-inner > nav {         display: none;     } } 
like image 167
Adrift Avatar answered Nov 09 '22 17:11

Adrift