Should I use max-device-width or max-width?

With CSS media queries you can use max-device-width to target a device width (such as an iPhone or Android device) and/or a max-width that targets a page width.

If you use max-device-width, when you change the size of the browser window on your desktop, the CSS won’t change, because your desktop doesn’t change size.

If you use max-width, when you change the size of the browser window on your desktop, you might be shown mobile-orientated styling, such as touch-friendly elements and menus and that kind of thing.

Targeting specific browsers (and devices?) is now deprecated and you should be a little more agnostic with what you target. Does that apply to media queries too?

Why would you target one over the other? Which one is the recommended one?

This is an example of a media query I use on a production website:

@media only screen and (min-device-width: 320px) and (max-device-width: 480px) and (min-device-height: 480px) and (max-device-height: 640px) {
  /* Change a menu to fit the screen better, etc... */
}

I tend to use both max-device-width and max-width.


solution

TL;DR

If you’re making a responsive website, you will probably want to use min-width/max-width rather than min-device-width/max-device-width in order to target a wider range of screen sizes.

Remember to specify a viewport meta tag in the <head> section of your document:

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

Explanation

Due to all the different possible screen resolutions and pixel densities a given device can have, a pixel is not a pixel because there are several things to take into consideration (zoom, pixel density, screen resolution and size, device orientation, aspect ratio, etc..). In this case, a pixel is actually referred to as a “optical reference unit” rather than a physic hardware pixel.

Fortunately, you can specify a viewport meta tag in the <head> section of your document in order to control the width and scaling of the browser’s viewport. If this tag has a content value of width=device-width, the screen’s width will match the device independent pixels and will ensure that all the different devices should scale and behave consistently.

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

In terms of media queries, you will probably want to use max-width rather than max-device-width, since max-width will target the viewport (current browser window), whereas max-device-width will target the device’s actual full screen size/resolution.

In other words, if you are using max-device-width, you will not see different media queries applied when resizing your desktop browser, because unlike max-width, only the device’s actual full screen size is taken into consideration; not the current size of the browser window.

This makes a huge difference if you’re trying to create an adaptive layout because the site won’t be responsive when resizing the browser. In addition, if you’re using max-device-width the media queries you’re using to target devices with smaller screens will not apply to desktops even when resizing the browser window down to match said smaller screen size.

This article on Google Developers highly discourages the usage of max-device-width:

Google Developers - Web Fundamentals - Responsive CSS media queries

It is also possible to create queries based on *-device-width; though this practice is strongly discouraged.

The difference is subtle but very important: min-width is based on the size of the browser window, whereas min-device-width is based on the size of the screen. Unfortunately some browsers, including the legacy Android browser may not report the device width properly and instead report the screen size in device pixels instead of the expected viewport width.

In addition, using *-device-width can prevent content from adapting on desktops or other devices that allow windows to be resized because the query is based on the actual device size, not the size of the browser window. ### Further Reading: