47

There are a lot different media queries for mobile screen sizes. It can be overwhelming to accomodate all of them when designing a responsive mobile site. Which are the most important ones to use when designing for mobile? I found this article that does a pretty good job of outlining the available media queries: http://css-tricks.com/snippets/css/media-queries-for-standard-devices/.

/* Smartphones (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 480px) {
/* Styles */
}

/* Smartphones (landscape) ----------- */
@media only screen 
and (min-width : 321px) {
/* Styles */
}

/* Smartphones (portrait) ----------- */
@media only screen 
and (max-width : 320px) {
/* Styles */
}

/* iPads (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) {
/* Styles */
}

/* iPads (landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape) {
/* Styles */
}

/* iPads (portrait) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : portrait) {
/* Styles */
}

/* Desktops and laptops ----------- */
@media only screen 
and (min-width : 1224px) {
/* Styles */
}

/* Large screens ----------- */
@media only screen 
and (min-width : 1824px) {
/* Styles */
}

/* iPhone 4 ----------- */
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}
Matt
  • 2,317
  • 7
  • 41
  • 52
  • 1
    There's no correct answer to this really, it's pretty much up you you to decide. Personally I only set my personal website to have one media query of max-width:480px for mobile devices but that's just what I was happy with. You may want to have different layouts for many different sizes. – Billy Moat Aug 20 '12 at 22:15
  • 1
    This can't really be answered, it's a very subjective question. You should create breakpoints in your design when it looks bad rather than trying to target device widths. Responsive design is about looking past devices and make the sites future friendly. As a 'best practice' your styles should be defined without a media query and build up using min-width to override styles as the screen gets larger. This is a mobile first approach and is the most widely backed approach to responsive design. – justinavery Aug 21 '12 at 02:38
  • I don't believe this is a truly subjective question, devices can be readily grouped into the standardised media query groups and devices with a higher screen density have a pixel ratio which fits them into those groups - for now. Considering the fact that the vast majority of responsive sites use frameworks such as bootstrap, foundation or skeleton which also use these media queries it seems safe to also do so for the time being. This can definitely be answered, within the scope of ever changing standards. – Alex Nov 20 '13 at 10:32

3 Answers3

92

I'd recommend taking after Twitter's Bootstrap with just these four media queries:

/* Landscape phones and down */
@media (max-width: 480px) { ... }

/* Landscape phone to portrait tablet */
@media (max-width: 767px) { ... }

/* Portrait tablet to landscape and desktop */
@media (min-width: 768px) and (max-width: 979px) { ... }

/* Large desktop */
@media (min-width: 1200px) { ... }

Edit: The original answer (above) was taken from Bootstrap version 2. Bootstrap has since changed their media queries in version 3. Notice that is there is no explicit query for devices smaller than 768px. This practice is sometimes called mobile-first. Everything outside of any media query is applied to all devices. Everything inside a media query block extends and overrides what is available globally as well as styles for all smaller devices. Think of it as progressive enhancement for responsive design.

/* Extra small devices (phones, less than 768px) */
/* No media query since this is the default in Bootstrap */

/* Small devices (tablets, 768px and up) */
@media (min-width: 768px) { ... }

/* Medium devices (desktops, 992px and up) */
@media (min-width: 992px) { ... }

/* Large devices (large desktops, 1200px and up) */
@media (min-width: 1200px) { ... }

Check it out on Bootstrap 3's docs.

cjlarose
  • 1,240
  • 10
  • 9
  • 4
    Maybe a stupid question, but what happends to the people with a desktop of 1024 width? or people with a resized window to 1000px – JP Hellemons Jan 04 '13 at 15:41
  • 3
    If there is no backdrop css which is used by default (rules without @media(...){...} filter) they are going to see no css styles at all IMHO. It would be better if the code was like `/* Large desktop */ @media (min-width: 980px) { ... }` – Philipp Gfeller Feb 12 '13 at 16:54
  • 2
    As phippu states, you should address these default sizes like 1024 with default CSS without media queries. Lots of machines with this resolution are old and ignore media queries at all, so its a good idea to do it like suggested from Twitter's Bootstrap. – Sebastian vom Meer Jul 25 '13 at 09:44
  • The Bootstrap 4 docs, https://getbootstrap.com/docs/4.3/layout/overview/#responsive-breakpoints – GWorking Aug 04 '19 at 17:17
  • Is pixel ratio (or pixel density) no longer a concern? If it is, all these answers on this page are misleading for mobile and for a top Google response on this issue. I'm asking because I am really confused myself. – Post Impatica Sep 28 '22 at 21:27
  • @PostImpatica Great question! CSS pixels are not quite the same as physical device pixels and take into account pixel density and how far the user is likely to be away from the screen. For this reason, I think it’s safe to use px measurements for boundary conditions in media queries. See https://hacks.mozilla.org/2013/09/css-length-explained/ – cjlarose Sep 30 '22 at 01:07
10
  1. Design in percentages and initially optimized for a 15"+ screen.

  2. Review what components you want to see on a phone - just keep essential content and remove elements that don't work or clutter the small screen. These styles can be contained within @media (max-width: 480px) { ... }

  3. As things move to 10" or less, redesign your buttons and interactive components for fingers rather than mouse. @media (max-width: 767px) { ... }

  4. Shrink the width of your browser. When things don't look so good, get in to the console and figure out what styles can be changed or items that need to be redesigned or removed. Mark what screen width they occur at and create a media query.

  5. At the end, review your media queries to see if some of them can be grouped together (ie if you have one at 750 and 767 pixels width, you might just as well with combining them in the 767).

If you are comfortable w jQuery you can add

$(window).resize(function(){
  console.log($(window).width());
}); 

to get the current screen size. Add a few extra pixels for good measure.

Jonathan Tonge
  • 1,520
  • 19
  • 25
7

The first Twitter Bootstrap code referenced by @cjlarose assumes that you've built your main CSS for a display that is between 980px and 1200px wide, so you're essentially starting with the desktop design and adapting all of the others from it.

I'm glad to see Twitter has changed to "mobile first" in Bootstrap 3. It's one of the most popular approaches to media queries, and the way I prefer to do it. You start from the smallest size rather than from the desktop out.

Note that your particular site may need different queries than what are listed there or on any other list. You should add queries as your content demands, not based on any set template.

Here are some media queries I've found most useful. These are just some examples:

/* Start with baseline CSS, for the smallest browsers. 
   Sometimes I put this into a separate css file and load it first.
   These are the "mobile first" styles. */

...


/* Then progressively add bigger sizes from small to large */

/* Smartphones start somewhere around here */
@media (min-width: 300px) {
}

/* You might do landscape phones here if your content seems to need it */
@media (min-width: 450px) {
}

/* Starting into tablets somewhere in here */
@media (min-width: 600px) {
}

/* Perhaps bigger tablets */
@media (min-width: 750px) {
}

/* Desktop screen or landscape tablet */
@media (min-width: 900px) {
}

/* A bit bigger if you need some adjustments around here */
@media (min-width: 1100px) {
}

/* Widescreens */
@media (min-width: 1500px) {
}

The most important thing is that you may not need all of these, or you might want to change the numbers depending on what your content looks like. I don't think there are any really hard rules about how many or where to put your breakpoints. I'm doing a site right now that happens to only need one breakpoint because the content is pretty simple, but I've also done sites that look more like the code above.

I didn't include the retina display code. That's useful if you're switching out normal-resolution images for high-resolution images on hi-res displays, but otherwise it's not really that useful.

nwalton
  • 2,033
  • 21
  • 22