5

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.

1
  • This is limiting the scope to screens with a width of 980px or less. Works for me. Commented Dec 17, 2012 at 14:53

1 Answer 1

5

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!

Sign up to request clarification or add additional context in comments.

9 Comments

It is working for me here with min-width: http://codepen.io/joe/full/znecI. Just to be clear, you only want .welcome-msg to display on screens less than 980px, correct?
@lota Please try with max-width as I am trying to hide the element .welcome-msg upto screen width:980px; i.e when the screen reaches 980px; I would like to hide this element.
@lota if the screen is larger than 980px the element displays but if it is 980px or lower it is not visible!
I think i've achieved what you want by using two min width queries: codepen.io/joe/pen/znecI or using one max-width codepen.io/joe/pen/ymjvI is that not working for you?
@lota Thanks for your efforts I think there must be a bug in my CSS or summick as neither solutions work for max width. I'm going to keep investigating as I really do need to hide this element...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.