57

(It works on other browsers but not chrome)

I want to apply a style only when the browser size is less than 1400px

with max-width not working

@media only screen and (max-width:1400px)  {
.heading-left {
    left: -0.5%;
  }
}

with min-width its working

@media only screen and (min-width:480px)  {
.heading-left {
    left: -0.5%;
   }
}

But also alters when browser width is above 1400px (I know thats how it works but max-width is not working)

Fiddle for this

https://jsfiddle.net/j4Laddtk/

Stewartside
  • 20,378
  • 12
  • 60
  • 81
Divya Barsode
  • 778
  • 1
  • 5
  • 15

12 Answers12

163

Have you tried adding the viewport in?

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

Working JSFiddle

Viewport is used when rendering responsive pages and is therefore mostly used when dealing with mobile websites, but when dealing with media queries it helps tell the CSS what the actual device-width is.

Stewartside
  • 20,378
  • 12
  • 60
  • 81
  • 25
    Without this tag, media queries just MATCH regardless of the condition. e.g. max-width: x px always returns true. – Gishu Nov 19 '16 at 04:09
51

Is your browser zoom-ed at different than 100% level ? If so, zoom to 100% (Ctrl+MouseWheel)

T.Todua
  • 53,146
  • 19
  • 236
  • 237
15

Try this method.

This will target based on device

@media screen 
 and (max-device-width: 1400px) 
 and (min-device-width: 480px) 
{ 
   .heading-left {
    left: -0.5%;
   }

}

To target based on browser window area

@media screen 
 and (max-width: 1400px) 
 and (min-width: 480px) 
{ 
   .heading-left {
    left: -0.5%;
   }

}
Prime
  • 3,530
  • 5
  • 28
  • 47
  • 1
    Not working. max-device-width will check the width of the device and not the window right? – Divya Barsode Jun 08 '15 at 10:22
  • @DivyaBarsode, Yes, Try `max-width` if you want to target display browser. – Prime Jun 08 '15 at 10:28
  • @DivyaBarsode, edited my answer, added `max-width`. Its works fine in my local machine. – Prime Jun 08 '15 at 10:34
  • @DivyaBarsode, Can you try by adding `!important` to left property like `left:-0.5% !important;` . – Prime Jun 08 '15 at 10:38
  • I just checked on opera browser.. It works fine there. But in chrome it isnt – Divya Barsode Jun 08 '15 at 10:43
  • @DivyaBarsode, Might be cache issue. @media should work fine on both browsers. Try `CTRL +F5` on your chrome and see the results, if still doesn't work then go through this link http://stackoverflow.com/questions/30197932/cant-see-css-style-changes-in-browser/30198600#30198600 – Prime Jun 08 '15 at 10:45
  • Did that before only but didnt work. In dev tools Elements tab, It doesnt show the media query. But when i go to dev tools-> Source -> css it shows the media query written – Divya Barsode Jun 08 '15 at 10:49
12

You need to place the @media queries after you declare your standard

user2977311
  • 139
  • 1
  • 2
11

Another thing that can happen is that you do something really stupid like:

@media only screen and (max-width: 1400)  { ... }

Make sure you put the px to identify what the quantity of your max-width is.

@media only screen and (max-width: 1400px)  { ... }

Not that I've ever been stuck for an hour on something so simple..

Bryan Lee
  • 949
  • 8
  • 19
Glahf
  • 131
  • 1
  • 7
9

This problem caused me several hours to figure it out with Bootstrap 3 when it just doesn't work. The reason is in the header of each web page, it needs this meta view element.

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

More details https://www.w3schools.com/css/css_rwd_viewport.asp

Bằng Rikimaru
  • 1,512
  • 2
  • 24
  • 50
4

This worked for me

@media screen and (max-width: 700px) and (min-width: 400px) { 
    .heading-left { left: -0.5%; }
}
4

If you've tried everything and you're still stuck, remember that media queries need to be at the bottom because CSS is applied from top-down.

If you have

.container {
  color: white;
}

and you want the font to be pink for screens less than 600px wide, your other media query needs to be below the original .container style.

.container {
  color: white;
}

@media only screen and (max-width: 600px) {
  .container {
    color: pink;
  }
}

So if your media queries are at the top the default colour of white will override the media query for pink.

Bryan Lee
  • 949
  • 8
  • 19
1

Please Add the Following line, this works for me

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

Set these in your head tag

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
Jawad Altaf
  • 75
  • 1
  • 7
0
@media only screen and (max-width: 1000px) {
  /*Don't forget to add meta viewport in your html*/
}

If it's not working try to inspect elements in the browser by navigating to the network in developer tools and toggling disable cache.

Sometimes it's not working because of the browser cache.

Bryan Lee
  • 949
  • 8
  • 19
Md. A. Barik
  • 429
  • 5
  • 12
0

There is one thing I would like to add here, which is only applicable if you have different CSS files. If some values do not seem to be having any effect then check if the CSS file that has the media queries is at the bottom inside the element or not. It is best to always put the media queries CSS file (if made in a different file) at the bottom of all other CSS links.

Capricorn1
  • 819
  • 9
  • 17