301

I have this @media setup:

HTML:

<head>
  <meta name="viewport" content="width=device-width, user-scalable=no" />
</head>

CSS:

@media screen and (min-width: 769px) {
    /* STYLES HERE */
}

@media screen and (min-device-width: 481px) and (max-device-width: 768px) { 
    /* STYLES HERE */
}

@media only screen and (max-device-width: 480px) {
    /* STYLES HERE */
}

With this setup it works on the iPhone but it does not work in the browser.

Is it because I already have device in the meta, and maybe have max-width:480px instead?

jacefarm
  • 6,747
  • 6
  • 36
  • 46
rallybilen
  • 3,077
  • 3
  • 15
  • 7
  • 1
    WHat's the issue - Default styles will be applicable to screens wider than 769 px. – Gurpreet Singh Nov 25 '12 at 11:25
  • just remove the `@media screen and (min-width:769px){` around your normal browser styles – Zoltan Toth Nov 25 '12 at 11:27
  • What do you mean Zoltan? And I think I might solved it. First, the issue was when my I resized my browser with max-device-with it worked on the phone but not in the broswer, without "device" it works in both. – rallybilen Nov 25 '12 at 19:56
  • So basiclly with out the "device" it works in both mobile/browser but when I add device I can not see it in the browser\ – rallybilen Nov 25 '12 at 20:29
  • 960px is better, I remember reading that you should save 20px for the vertical scroll bar on some browsers that don't play well. – Medeno Nov 09 '13 at 02:36
  • Plus it divides into 12 a little nicer (80px instead of 81px) – Craig Moore Apr 02 '14 at 02:11
  • Do not use the `device` keyword as it will be removed an your site might break. https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries – Robert Aug 08 '16 at 02:02
  • Just a note, 'screen and' is optional. – Scott Romack Sep 11 '17 at 19:30

6 Answers6

456

I've found the best method is to write your default CSS for the older browsers, as older browsers (including IE 5.5, 6, 7 and 8) can't read @media. When I use @media, I use it like this:

<style type="text/css">
    /* default styles here for older browsers. 
       I tend to go for a 600px - 960px width max but using percentages
    */
    @media only screen and (min-width: 960px) {
        /* styles for browsers larger than 960px; */
    }
    @media only screen and (min-width: 1440px) {
        /* styles for browsers larger than 1440px; */
    }
    @media only screen and (min-width: 2000px) {
        /* for sumo sized (mac) screens */
    }
    @media only screen and (max-device-width: 480px) {
       /* styles for mobile browsers smaller than 480px; (iPhone) */
    }
    @media only screen and (device-width: 768px) {
       /* default iPad screens */
    }
    /* different techniques for iPad screening */
    @media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait) {
      /* For portrait layouts only */
    }

    @media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
      /* For landscape layouts only */
    }
</style>

But you can do whatever you like with your @media. This is just an example of what I've found best for me when building styles for all browsers.

iPad CSS specifications.

Also! If you're looking for printability you can use @media print{}.

Nat Riddle
  • 928
  • 1
  • 10
  • 24
bashleigh
  • 8,813
  • 5
  • 29
  • 49
  • 26
    Why have you put 'mac' screens? It's not only "amazing" Macs that can have large high resolution screens! – Luke Jan 08 '14 at 10:36
  • Can I ask where you got these numbers? And does anyone know of more scientific basis for these? This is the closest I found based on device statistics: https://www.freecodecamp.org/news/the-100-correct-way-to-do-css-breakpoints-88d6a5ba1862/ https://gs.statcounter.com/#desktop+mobile+tablet-resolution-ww-monthly-201608-201610-bar and: https://www.responsivejs.com/wiki/breakpoints – alchemy Nov 27 '22 at 03:50
  • 1
    I found another source that uses very similar standards to yours. https://www.freecodecamp.org/news/css-media-queries-breakpoints-media-types-standard-resolutions-and-more/ . I think I'm going to keep my layout simple enough though that Flexbox flux can just make the sections flow vertically on mobile devices. – alchemy Nov 27 '22 at 19:31
55

The underlying issue is using max-device-width vs plain old max-width.

Using the "device" keyword targets physical dimension of the screen, not the width of the browser window.

For example:

@media only screen and (max-device-width: 480px) {
    /* STYLES HERE for DEVICES with physical max-screen width of 480px */
}

Versus

@media only screen and (max-width: 480px) {
    /* STYLES HERE for BROWSER WINDOWS with a max-width of 480px. 
       This will work on desktops when the window is narrowed.  */
}
jpostdesign
  • 2,548
  • 2
  • 15
  • 15
39

If website on small devices behavior like desktop screen then you have to put this meta tag into header before

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

For media queries you can set this as

this will cover your all mobile/cellphone widths

 @media only screen and (min-width: 200px) and (max-width: 767px)  {
    //Put your CSS here for 200px to 767px width devices (cover all width between 200px to 767px //
   
    }

For iPad and iPad pro you have to use

  @media only screen and (min-width: 768px) and (max-width: 1024px)  {
        //Put your CSS here for 768px to 1024px width devices(covers all width between 768px to 1024px //   
  }

If you want to add css for Landscape mode you can add this

and (orientation : landscape)

  @media only screen and (min-width: 200px) and (max-width: 767px) and (orientation : portrait) {
        //Put your CSS here for 200px to 767px width devices (cover all mobile portrait width //        
  }
Rohit Kaushik
  • 1,182
  • 12
  • 18
9

The correct value for the content attribute should include initial-scale instead:

<meta name="viewport" content="width=device-width, initial-scale=1">
                                                   ^^^^^^^^^^^^^^^
jacefarm
  • 6,747
  • 6
  • 36
  • 46
Ksenia Titova
  • 91
  • 1
  • 1
7

If you want to include both min and max width for responsiveness in the browser, then you can use the following:

 @media (min-width: 768px) and (max-width: 992px){...}
 @media (min-width: 480px) and (max-width: 767px) {...}
-1

for some iPhone you have to put your viewport like this

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, shrink-to-fit=no, user-scalable=0" />
NuggaN85
  • 1
  • 1
  • Why? There are several attributes there, and no information on why you would "have" to do this for "some" iPhones. Which iPhones? And which browsers on iPhone?! – tjbp Jun 08 '23 at 13:09