Media Queries

Media queries are an obvious tool in designing the user interface. However, I believe there are very important, often overlooked ways to use media queries.

I believe media queries should be used to provide great experiences to most devices and delightful experiences to more capable devices—all in ways that fit a user's needs. The experience does not need to be the identical for all users, but they must all have the same access to the same information.

Don't provide all of the bells and whistles to everyone and then remove them with media queries. Do just the opposite—add enhancements if the user wants and can benefit from them.

Mobile First

Exactly what the name implies. Design for mobile first! I know that sounds obvious and repetitive, but it is often the secondary focus when building a website. How many times have you seen the CSS file of a site and they add media queries later in the code to accomodate smaller and smaller screens. That's doing it backwards! Code should be done first to look good on smaller screens, and then use media queries to enhance the layout of content as the screen size increases.

Touchscreen, Mouse or Trackpad?

Are you accomodating devices where the pointer is a finger instead of a mouse or trackpad? Are you using :hover to create unique effects that can't be easily seen on touchscreen devices? If you are, you should be accomodating those devices to present any information they might miss out on. For example, hovering over an image shows an overlay with text. On a touchscreen device, the user is less likely to be able to see that feature. Again, it comes back to Mobile First.

Accomodate ALL Users

Your interface might be awesome with cool effects, but please make sure you are sensitive to your users. Add motion effects only if the user hasn't set prefers-reduced-motion to reduce. Add transparency effects only for users that haven't set prefers-reduced-transparency to reduce. Do the same for prefers-contrast and prefers-color-scheme. And don't forget to adjust your layout for the ambient light level of the device.

In all cases listed above, you can link to external style sheets that add enhancements using media queries as they align with the user's preferences.

Sample

Nothing on the user's end to see here. :).

Code

The code below is the magic that makes the above sample happen.

<link rel="stylesheet" media="(hover: hover) and (pointer: fine)" href="mouse-trackpad.css"> <link rel="stylesheet" media="(prefers-reduced-motion: no-preference)" href="motion.css"> <link rel="stylesheet" media="(prefers-reduced-transparency: no-preference)" href="transparency.css"> <link rel="stylesheet" media="(prefers-color-scheme: dark)" href="color-scheme-dark.css"> <link rel="stylesheet" media="(prefers-color-scheme: light)" href="color-scheme-light.css"> <link rel="stylesheet" media="(light-level: dim)" href="light-level-dim.css"> <link rel="stylesheet" media="(light-level: washed)" href="light-level-washed.css"> <style> /* DEVICE WIDTH */ @media only screen (min-width: 500px) { } /* DEVICE HEIGHT */ @media only screen (min-height: 500px) { } /* HOVER CAPABILITIES */ @media only screen (hover: none) /* smartphones, touchscreens ('hover' for mouse, touchpad) */ { } /* POINTER CAPABILITIES */ @media only screen (pointer: coarse) /* smartphones, touchscreens ('fine' for mouse, touchpad) */ { } /* SCREEN ORIENTATION */ @media only screen (orientation: portrait) /* ('landscape') */ { } /* REDUCE MOTION */ @media only screen (prefers-reduced-motion: reduce) /* ('no-preference') */ { } /* REDUCE TRANSPARENCY */ @media only screen (prefers-reduced-transparency: reduce) /* ('no-preference') */ { } /* PREFERS CONTRAST */ @media only screen (prefers-contrast: high) /* ('no-preference' or 'low') */ { } /* COLOR SCHEME (DARK MODE) */ @media only screen (prefers-color-scheme: dark) /* ('light' or 'no-preference') */ { } /* LIGHT LEVEL */ @media only screen (light-level: dim) /* ('normal' or 'washed' in bright light) */ { } </style> <p>Nothing on the user's end to see here. :).</p>