htmlcsswordpress

@ media Screen CSS not working


I have added the following code to my style.css file in my wordpress theme but it doesn't work. I want the body background to change for screen widths between 768 and 1024px

CSS:

@media screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
{ 
    body {
        background: #fff;
        border-top: 2px solid #DDDDDD;
    }
}

Solution

  • You might have an issues with the order of the media query in which you mentioned the styles

    check this fiddle, change the browser width to see the the media query in action

    @media screen and (max-width : 1500px) {
        body {
            background: #000;
            border-top: 2px solid #DDDDDD;
        }
    }
    
    @media screen and (min-width : 768px) and (max-width : 1024px) {
        body {
            background: #fff;
            border-top: 2px solid #DDDDDD;
        }
    }
    

    This fiddle works fine, but if you change the order of the media queries it wont work...try it for yourself!

    CSS always selects the last style that was declared if multiple style are found for an attrib.

    for e.g :

    @media (max-width: 1024px) {
      body {
        background: black;
      }
    }
    
    @media (max-width: 768px) {
      body {
        background: white;
      }
    }
    

    for 765px ( since both m.q cover this width ) color selected would be white