3

How could i apply different media queries in a single CSS file.I am applying the below queries but only the latest one works..

@media only screen and (max-width: 500px){
      css styling here
}

@media only screen and (max-width: 600px){
     css styling here
}
Rayees AC
  • 4,426
  • 3
  • 8
  • 31
Shehryar Tanoli
  • 393
  • 2
  • 4
  • 17

2 Answers2

8

If you're defining the same CSS properties of the same elements, the last definition will have priority.

In this case, here is a solution :

@media only screen and (max-width: 500px){
    /* CSS apply on width between 0 and 500px */
}
@media only screen and (max-width: 600px) and (min-width: 501px){
    /* CSS apply on width between 501px and 600px */
}
SeeoX
  • 565
  • 3
  • 18
6

The meta tag should be added in the <head> tag in HTML document. Please check that have you added

<meta name="viewport" content="width=device-width, initial-scale=1">

The sequential order of css code also matters. Please change sequence by following:

@media only screen and (max-width: 600px){
      body{ background:green;}
}

@media only screen and (max-width: 500px){
    body{ background:red;}
}
Priya jain
  • 1,127
  • 2
  • 10
  • 22