Media Queries Demystified: CSS Min-Width and Max-Width

Now that Gmail is supporting media queries in some clients, we have seen a resurgence in interest about how to use them. Read on to learn about media queries in HTML email.

What are media queries?

A media query consists of an optional media type (all, handheld, print, TV and so on) and any number of optional expressions that limit when the query will trigger, such as width, pixel-density or orientation. Media queries are part of CSS3, and enable developers to customize their content for different presentations of their content.

At a basic level, media queries enable an email developer to create a responsive email by detecting the width of the display. For this purpose, the most commonly used query is max-width. At any width that is less than the max-width specified, all of the CSS within the query will take effect.

Media queries can also be used to target certain resolutions or even specific email clients. Media queries can be used instead of or in addition to fluid hybrid design.

How min and max width queries work

How media queries function can be a bit confusing. Let’s take a look at the queries which are commonly used in email.

Max-Width

Here is an example of a max-width query.

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

What this query really means, is “If [device width] is less than or equal to 600px, then do {…}”

So if the email is opened on an iPhone 5S, with a screen width of 320px, the media query will trigger and all of the styles contained in { … } will take effect.

Min-Width

Here is an example of a min-width query.

@media only screen and (min-width: 600px) {...}

What this query really means, is “If [device width] is greater than or equal to600px, then do {…}”

So if the email is opened on an iPhone 5S, with a screen width of 320px, the media query will not trigger and the styles contained in { … } will not take effect.

Combining Min-Width and Max-Width

These queries can be used together to target a specific range of screen sizes.

@media only screen and (max-width: 600px) and (min-width: 400px) {...}

The query above will trigger only for screens that are 600-400px wide. This can be used to target specific devices with known widths.

Breakpoints

Most media queries are set to trigger at certain screen widths or breakpoints. Exactly what these should be set to is a matter of some debate amongst email developers.

iPhones and iPads provide us with a few easy breakpoints to start from. Coding styles for these specific clients will make sure our emails look great on these screens. Androids, on the other hand, vary widely in screen size because there are so many different manufacturers and devices. I recommend creating two to four breakpoints, based on popular Apple devices, which will cover most devices.

Breakpoint 1 (iPhone 5S): 320px

Breakpoint 2 (iPhone 6+): 414px

Breakpoint 3 (iPad Mini): 703px

Breakpoint 4 (iPad Air): 768px

Breakpoints 3 and 4 are optional, as most emails will look fine showing the desktop version on an iPad or large tablet. You could even get away with using just breakpoint 2, if you code your container tables to expand to 100% width (and not a set width, which may or may not match the device well).

Taking advantage of precedence

Remember, CSS rules that appear later in the embedded styles override earlier rules if both have the same specificity. This means that you can set rules for tablets by putting the Breakpoint 4 media query first, then set styles for mobile devices with a Breakpoint 2 media query. Because the Breakpoint 2 styles come after the Breakpoint 4 styles, your mobile styles will override your tablet styles when the breakpoint 2 query is triggered. This means that you don’t have to set min-width for any of your media queries, as long as they are arranged in the correct order.

Here is an example order.

Desktop styles (not in a media query)

Tablet Styles (max-width: 768px)

Mobile Styles (max-width: 414px)

Coding for media queries

When coding an email to be responsive using media queries, a common technique is to create tables with align = “left” and a special class to target inside the media queries. For example, a two-column section might look like this.

Each of the tables with 49% width can fit side by side when on “desktop” view. 49% is used instead of 50% because Outlook can be very picky about what fits side-by-side and what doesn’t. You can make 50% width fit if you set all of your styles just right (no border, padding, etc). You can make a 3 Column section using similar code, but use three tables set to 32% width instead.

When the responsive code kicks in, we’ll want to make these content blocks 100% width for phones, so that they fill the whole screen. This can be accomplished for most phones with a single media query.

You can continue to add media queries with special styles to cover as many different screen sizes as you’d like. You should also add code to your media queries to optimize font-size and line-height for each screen size, improving readability for your recipients.

If you’d like to start working with a template like this, grab our “Emailology” template from our Resources section. You can get a free account to gain access to all of our resources (like templates, white papers, webinars and client tips and tricks).

Other Media Queries

You can do a few other interesting things with media queries. The below uses are most relevant to email, but check out MDN’s page for even more media query techniques.

Orientation

You can use the following media query to target device orientation. Unfortunately, this query doesn’t work well in iOS Mail. In most versions, the landscape media query will always trigger regardless of orientation.

@media screen and (orientation: landscape) { ... }

Targeting Yahoo! Mail

You can use this simple query to write styles that will trigger only in Yahoo! Mail. This can be used to address layout or rendering issues that you see only in Yahoo! Mail, or to include messages intended only for Yahoo! users.

@media (yahoo) { ... }

Pixel-density

This media query can be used to target only devices that have a certain pixel density. Combined with webkit, this can effectively let the email developer target only iOS devices. This can be useful when adding interactive code that is known only to work in iOS Mail.

@media screen and (-webkit-min-device-pixel-ratio: 2) { ... }

Media queries for print

By setting specific styles for print, you can ensure your email will be easy to print and look great. This is especially important for coupons or tickets. You can hide useless elements, like links and buttons, and display only the part of the email that is important to print.

@media print { ... }

Media query support chart

Media queries have fairly wide support now that Gmail has enabled classes, IDs and embedded styles. Check out the support chart below.

Environment

Email Client

Media Queries Supported?

Webmail

AOL

✔

Webmail

Gmail

✔

Webmail

Google Apps

✔

Webmail

Yahoo! Mail

✔

Webmail

Outlook.com

✕

Webmail

Office 365

✕

Desktop

Apple Mail

✔

Desktop

Lotus Notes

✕

Desktop

Outlook 2000-2016

✕

Desktop

Outlook 2011 (Mac)

✔

Desktop

Thunderbird

✔

Mobile

iOS Mail

✔

Mobile

Gmail App (Android)

✔

Mobile

Gmail App (iOS)

✔

Webmail (Regional)

BOL

✕

Webmail (Regional)

Comcast

✔

Webmail (Regional)

Free.fr

✔

Webmail (Regional)

Freenet.de

✔

Webmail (Regional)

GMX

✔

Webmail (Regional)

La Poste

✔

Webmail (Regional)

Libero

✕

Webmail (Regional)

Mail.ru

✕

Webmail (Regional)

Nate

✔

Webmail (Regional)

Naver

✕

Webmail (Regional)

Orange.fr

✔

Webmail (Regional)

QQ

✔

Webmail (Regional)

SFR.fr

✔

Webmail (Regional)

T-Online.de

✕

Webmail (Regional)

Telstra

✕

Webmail (Regional)

Terra

✕

Webmail (Regional)

Web.de

✔

Webmail (Regional)

Yandex.ru

✕

What do you do with media queries?

Let us know your favorite uses for media queries in the comments below! Or hit us up on Facebook and Twitter.

Don’t guess, test

Media queries can be confusing and take trial and error to perfect. That’s why we offer you seven days free with Email on Acid, so you can ensure your media queries, and your emails, are perfect before you hit send.

I think what makes this so confusing is that there are always 2 numbers in the condition: The returned device width and the min/max value you are passing. So you always have to step back and ask, what is the actual device width that is being returned?

I guess it’s just slightly confusing since we’re not privy to what styles are actually being applied in the examples. But given the media query in the min-width example, a screen width of 320 will mean any styles in there will be ignored. So, it won’t be true.

“@media only screen and (min-width: 330px) {…} Here’s what that actually means: If [device width] is greater than or equal to [specified #], then do {…}”

So if the actual “device width” is 320px this condition will return false.”

Then you go on to say the opposite:

“It would become true if you pass any value equal to or less than 320 as shown below: =<300 =<310 =<320 all equal true =<330 =<340 =<350 all equal false"

Even though the minimum width to create the "true" state is 330px. Therefore, how can anything less than 330px trigger true? The example should say the opposite: =<330, =<340, =<350 are all true, and the previous ones are actually false.

What you guys are saying is true, but got the wording to confusing. The specified # is being compare with the device width as the reference. Therefore we are comparing the specified # in the media query against the actual device width.

means “Is the [specified # –> 300] less the [device width–>320]. Notice how I switch specified # and device width. In your example you’re comparing the device width to the media query and that causes the confusion. You should change it to

“If [specified #] is less than [device width], then do {…}”.

This make things more clear because if you think in terms of a number line where 0 is the center and how big or small a number is determine how far it is from 0, then make the device width our 0.

REMEMBER IT NOT HOW BIG THE DEVICE WIDTH IS COMPARE TO THE NUMBER THAT WE SPECIFIED, IT IS HOW BIG THE NUMBER THAT WE SPECIFIED IS COMPARE TO THE DEVICE WIDTH.

“@media only screen and (min-width: 330px) {…} Here’s what that actually means: If [device width] is greater than or equal to [specified #], then do {…}”

So if the actual “device width” is 320px this condition will return false.”

Then you go on to say the opposite:

“It would become true if you pass any value equal to or less than 320 as shown below: =<300 =<310 =<320 all equal true =<330 =<340 =<350 all equal false"

Even though the minimum width to create the "true" state is 330px. Therefore, how can anything less than 330px trigger true? The example should say the opposite: =<330, =<340, =<350 are all true, and the previous ones are actually false.

Vikram, In the example image, each of those numbers, like “(=<330)" and so on, is tied to a different media query. As you can see, (=<330) is black, and therefore did not register as "true." This is because 320 (device width) is not equal to or greater than 330 (set number). If you look at (=<320), you'll see that it is true. This is because 320 (device width) is equal to than 320 (set number).

The examples were generated on actual devices using those media queries. We are just doing our best to explain how it works 😉

where the # for max is 1 less than the number for min, and then I used another 2 queries right after the first two where again the number for max is 1 less than the number for min.. Also my queries are ordered so that the small #’s are near the head of the css document and the larger #’s are near the foot of it… and this is working great for me… now if I can get my custom javascript drop-down menu to function in my cms I’d be really happy

Hi Everybody i am new in responsive web, Self learning to see tutorials and download demo and doing practice. i need some help to understand the css media query . i am confuse after read this article. Nobody doing my help in my country. it a request if you Guys please help me to understand the logic of media query using min and max width pleaseee

Hi, We have an old email newslettter template which has class=”mobile” and class=”desktop” for content to show hide accordingly. On yahoo mail (web) it showed both desktop and mobile sections, so i added media width 1200 and block/none accordingly and had to put this all the way near </body> tag. if its in the head then Gmail app on phones showed the desktop layout. But still yahoo email app is showing desktop layout and not the mobile layout. The class=”mobile” and class=”desktop” is assigned to the

I’ve struggled a little with media queries since they first arrived on the scene, and most authors with explanations I’ve come across seem to believe that by providing lots of extra in-depth technical information it’ll be helpful – that’s usually not the case – it usually adds to the confusion.

Your article however gave me exactly what I need for my current project – clear, concise and without all the usual overly-technical fluff – perfect!

Enterprise Solutions

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognizing you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

You can adjust all of your cookie settings through your browser settings.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

disable

If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.