Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css3 Media Query not working with max-width

I have been trying to hide an element at a max-width of 980px using media queries but for some reason it is still displaying.

If I use a media query with min-width the element disappears but with this code it is still showing and I can figure out why?

@media (max-width: 980px) {
    .welcome-msg  {
        display:none;
    }
}

Can anyone see anything wrong with my code? I'm using FF responsive design view fro testing at the moment.

like image 913
user1704524 Avatar asked Dec 17 '12 14:12

user1704524


People also ask

Why my @media is not working?

Media process errors on Android can be triggered by a long list of factors. For example, maybe you're running low on RAM and storage space. Clear the cache, check for updates, remove the SD card and check if the error is gone. As a last resort, reset your device to factory settings.

How do you write a media query for max width?

Combining media query expressions Max-width and min-width can be used together to target a specific range of screen sizes. @media only screen and (max-width: 600px) and (min-width: 400px) {...} The query above will trigger only for screens that are 600-400px wide.

Why my media query is not working in CSS?

Media Query Not Working on Mobile Devices 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.

Is media query max width inclusive?

The min- and max- queries are inclusive of the specified values, for example min-width: 400px tests for a width of 400px or greater.


1 Answers

With your current max-widthmedia query, display:none is going to apply until the document reaches a width of 980px, rather than at 980px.

From your question, it seems like you want the opposite to happen, which is why you've had success with min-width. Switching from max-width to min-width should solve things.

Otherwise, you are going to have to set your element to display: none in your non-media query css, and use display:block in your max-width media query.

CSS

/* Only applies while screen is 980px or less */
@media (max-width: 980px) {
.welcome-msg  {
        display:none;
    }
}

/* only applies while screen is 980px or greater */
@media (min-width: 980px) {
.welcome-msg  {
        display:none;
    }
}

/* if you must use max-width, this is a solution */
/* otherwise, use min-width IMHO */
.welcome-msg  {
        display:none;
}

@media (max-width:980px) {
  .welcome-msg  {
   display:block; /* element will only show up if width is less than or equal to 980px */
  }
}

If that's not what you are trying to accomplish, It would be helpful to have a Codepen example for us to better answer your question.

Good luck!

like image 139
Nick Tomlin Avatar answered Sep 22 '22 14:09

Nick Tomlin