Wouldn’t it’s good if each electronic mail regarded the identical in each electronic mail consumer, and on each system?
An electronic mail developer can dream. However since that’s not the case, we’ll cowl probably the most helpful instruments in any developer’s toolbox to optimize designs for various gadgets and viewports: the media question.
If you happen to’re acquainted with media queries for internet coding, they work just about the identical method, besides you can’t nest them. In any other case, there are just a few different use instances for media queries in electronic mail that may show useful.
Leap to a piece on this article:
What are media queries?
Media queries are a part of CSS3 and permit builders to customise their content material for various shows or gadgets. These blocks of code are positioned within the <type> block of your electronic mail that controls how your electronic mail seems at totally different sizes. They usually seem like this:
/* When the browser is at the very least 600px and under */ @media display and (max-width: 600px) { .factor { /* Apply some kinds */ } }
A media question consists of an non-compulsory media sort (all, handheld, print, TV, and so forth) and any variety of non-compulsory expressions that restrict when the question will set off, similar to width, pixel-density, or orientation.
Chances are high, you’ve seen media queries work in the true world. They’re the most effective methods builders and designers can create responsive emails optimized for cell. Media queries do that by detecting system or show width and altering the design of the e-mail accordingly.
Right here’s how that works:
Utilizing media queries for responsive emails
Responsive emails are the most typical motive why you’d use a media question in an electronic mail, so let’s discuss by means of what that appears like step-by-step.
Say you’re coding a e-newsletter utilizing a two-column format, with a picture on the left and textual content on the precise. That gained’t work on most cell screens — or at the very least, isn’t the greatest expertise — so that you’re going to wish to use a media question to regulate the design for cell. On this case, most designers would stack the photographs on high of the textual content for cell designs.
Setting min-width and max-width
Media queries inform the code when and the way this could occur, often utilizing two attributes: min-width and max-width. A easy media question units each of those for a given system or viewport measurement.
Right here is an instance of a min-width question:
@media solely display and (min-width: 600px) {...}
What this question actually means, is “If [device width] is bigger than or equal to 600px, then do {…}” On this case, you’ll default to the two-column type.
So, if the e-mail is opened on an iPhone 13 Professional, with a viewport width of 390px, the media question won’t set off and the kinds contained in { … } won’t take impact.
Then, you’ll set your max-width question:
@media solely display and (max-width: 600px) {...}
What this question actually means, is “If [device width] is lower than or equal to 600px, then do {…}” On this case, you’ll stack the photographs into one column.
So, if the e-mail is opened on an iPhone 13 Professional, with a viewport width of 390px, the media question will set off and all the kinds contained in { … } will take impact.
You don’t need to do them one after the other, although. Right here’s what it appears like mixed:
@media solely display and (min-width: 400px) and (max-width: 600px) {...}
You should utilize these queries collectively to focus on a particular vary of display sizes.
The question above will set off just for screens which can be between 400px and 600px broad. You should utilize this technique if you wish to goal a design for a particular system with a identified width.
Managing totally different breakpoints
However that may get repeatable and clumsy if you happen to’re making an attempt to carry each single current iPhone, Android, iPad, and Google Pixel in the identical question. Androids particularly range broadly in display measurement, and most producers launch new telephones yearly. You will have loads of subscribers nonetheless hanging on to their outdated iPhone 8s!
Most media queries are set to set off at sure display widths or breakpoints, to offer a spread slightly than goal a particular system. Right here’s an instance supplied by W3schools:
Breakpoint Setting | For System |
max-width 320 px | Smartwatches |
max-width 420 px | Smaller gadgets |
max-width 600px | Telephones |
min-width 600px | Tablets and Giant Telephones |
min-width 768px | Tablets |
min-width 992px | Laptops and Desktops |
min-width 1200px | Displays, Desktops |
Coding for media queries in observe
However most builders don’t take into consideration each single breakpoint of their queries. When coding a responsive electronic mail utilizing media queries, a typical approach is to create tables with align = “left” and a particular class to focus on contained in the media queries. For instance, a two-column part would possibly seem like this:
<desk function=”presentation” border="0" cellpadding="0" cellspacing="0" align="middle" class="deviceWidth"> <tr> <td type="padding:10px 0"> <desk align="left" width="49%" border="0" class="deviceWidth"> <tr> <td> </td> </tr> </desk> <desk align="left" width="49%" border="0" class="deviceWidth"> <tr> <td> </td> </tr> </desk> </td> </tr> </desk>
Every of the tables with 49% width can match side-by-side when on “desktop” view. We use 49% as an alternative of fifty% as a result of Outlook might be very choosy about what suits side-by-side and what doesn’t. You should utilize 50% width if you happen to set all of your kinds proper (no border, padding, and so forth).
If you wish to create a three-column part utilizing related code, set every desk to 32% width.
When the responsive code kicks in, we’ll wish to make these content material blocks 100% width for telephones, in order that they fill the entire display. A single media question will deal with this for many telephones:
@media solely display and (max-width: 414px) { .deviceWidth {width:280px!vital; padding:0;} .middle {text-align: middle!vital;} }
You possibly can proceed so as to add media queries with particular kinds to cowl as many various display sizes as you’d like. You also needs to add code to your media queries to optimize font-size and line-height for every display measurement. It will enhance readability in your recipients and ensure your electronic mail appears like this:
However that’s not the one main use case for media queries — we’ll dive into how and why to make use of them under.
Utilizing media queries for system orientation
You too can use media queries to focus on system orientation, not simply viewport measurement. Going again to that two-column structure instance, it would work if a cellphone or pill is in panorama mode, however not in portrait mode. Right here’s an instance of what that appears like:
@media display and (orientation: panorama) { ... }
In most shoppers, the panorama media question will all the time set off no matter orientation. So, if the e-mail is opened on a cellphone or pill in portrait mode, the e-mail will look similar to the responsive electronic mail we coded above, stacking photographs above each other. But when it’s opened in panorama mode, the e-mail will seem in a two-column format.
Sadly, this question doesn’t work properly in iOS Mail.
Utilizing media queries to focus on Yahoo!
Each consumer is barely totally different, and media queries are one technique to handle these variations.
In terms of Yahoo! Mail inboxes, there’s a media question you possibly can strive utilizing that particularly targets this electronic mail consumer. You could possibly use this media question and add kinds that solely set off in Yahoo!
<type sort="textual content/css"> @media display yahoo{ { /* Place your kinds right here */ } } </type>
This can be utilized to handle structure or rendering points that you just see solely in Yahoo! Mail or to incorporate messages supposed just for Yahoo! Customers.
Utilizing media queries for darkish mode emails
One other utility of media queries that may be fairly helpful is defensively coding for darkish mode. Some electronic mail shoppers gained’t change your electronic mail design in any respect, leading to a very poor electronic mail expertise — both by utterly inverting the colours of your electronic mail, rendering it inaccessible and unreadable or by retaining it vibrant after they’ve initiated darkish mode, hurting their eyes.
To stop this, you possibly can show totally different designs for darkish mode utilizing this media question:
@media (prefers-color-scheme: darkish )
E mail consumer help for media queries
Sadly, not each electronic mail consumer helps media queries. What would the lifetime of an electronic mail developer be with out curveballs thrown by particular electronic mail shoppers? (Yeah, it’s Outlook and Gmail…it all the time is.)
Right here’s a rundown of which electronic mail shoppers help media queries, and which don’t:
Shopper | Supported? |
Apple Mail macOS | Sure |
Apple Mail iOS | Sure |
Gmail (internet/desktop) | Partial |
Gmail (Android) | Partial |
Gmail (iOS) | Partial |
Gmail (cell app) | No |
Outlook (macOS) | Sure |
Outlook (iOS) | Partial |
Outlook.com | Partial |
Outlook (Android) | Partial |
Outlook (Home windows Mail) | No |
Outlook (Desktop 2007 – 2019) | No |
AOL | Partial |
Yahoo! Mail | Partial |
Samsung E mail | Sure |
Mozilla Thunderbird (60.3) | Sure |
(Because of our buddies at Can I E mail for the data. Verify them out for the most recent on media queries and extra.)
You’ll discover sturdy help from shoppers utilizing WebKit, however you’ll want to make use of workarounds for inconsistent help in Gmail. For instance, you should utilize min-width and max-width for responsiveness, however not (prefers-color-scheme) for darkish mode emails.
Media queries: One more reason to check
With inconsistent help throughout electronic mail shoppers and totally different designs for cell and internet, testing turns into important. Particularly for finicky shoppers like Gmail, the place something fallacious within the CSS renders the e-mail unreadable. E mail on Acid presents limitless electronic mail testing in main mailbox suppliers and the preferred gadgets. Meaning you can also make certain your electronic mail appears good earlier than it hits the inbox. Wish to see for your self? Reap the benefits of our free, seven-day trial.
This text was final up to date in November 2021. It was beforehand up to date in February 2019 and first printed in October 2016.
Writer: The E mail on Acid Group
The E mail on Acid content material workforce is made up of digital entrepreneurs, content material creators, and straight-up electronic mail geeks.
Join with us on LinkedIn, observe us on Fb, and tweet at @EmailonAcid on Twitter for extra candy stuff and nice convos on electronic mail advertising and marketing.
Writer: The E mail on Acid Group
The E mail on Acid content material workforce is made up of digital entrepreneurs, content material creators, and straight-up electronic mail geeks.
Join with us on LinkedIn, observe us on Fb, and tweet at @EmailonAcid on Twitter for extra candy stuff and nice convos on electronic mail advertising and marketing.