Wouldn’t it’s good if each electronic mail seemed the identical in each electronic mail shopper, and on each system?
An electronic mail developer can dream. However since that’s not the case, we’ll cowl one of the helpful instruments in any developer’s toolbox to optimize designs for various gadgets and viewports: the media question.
When you’re aware of media queries for net coding, they work just about the identical method, besides that you would be able to’t nest them. In any other case, there are a couple of different use circumstances 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 displays or gadgets. These blocks of code are positioned within the <model> block of your electronic mail that controls how your electronic mail seems at completely different sizes. They sometimes appear like this:
/* When the browser is a minimum of 600px and under */ @media display and (max-width: 600px) { .factor { /* Apply some kinds */ } }
A media question consists of an optionally available media kind (all, handheld, print, TV, and so forth) and any variety of optionally available expressions that restrict when the question will set off, reminiscent of width, pixel-density, or orientation.
Chances are high, you’ve seen media queries work in the true world. They’re probably the greatest methods builders and designers can create responsive emails optimized for cellular. 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 cause why you’d use a media question in an electronic mail, so let’s speak by what that appears like step-by-step.
Say you’re coding a publication utilizing a two-column format, with a picture on the left and textual content on the precise. That received’t work on most cellular screens — or a minimum of, isn’t the greatest expertise — so that you’re going to wish to use a media question to regulate the design for cellular. On this case, most designers would stack the photographs on high of the textual content for cellular designs.
Setting min-width and max-width
Media queries inform the code when and the way this could occur, normally utilizing two attributes: min-width and max-width. A easy media question units each of those for a given system or viewport dimension.
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’d default to the two-column model.
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’d 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’d 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 should do them one by one, although. Right here’s what it seems to be like mixed:
@media solely display and (min-width: 400px) and (max-width: 600px) {...}
You should use these queries collectively to focus on a particular vary of display sizes.
The question above will set off just for screens which might be between 400px and 600px broad. You should use this technique if you wish to goal a design for a particular system with a recognized width.
Managing completely different breakpoints
However that may get repeatable and clumsy in the event you’re making an attempt to carry each single latest iPhone, Android, iPad, and Google Pixel in the identical question. Androids particularly fluctuate broadly in display dimension, and most producers launch new telephones yearly. You’ll 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 provide a variety somewhat 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 Massive Telephones |
min-width 768px | Tablets |
min-width 992px | Laptops and Desktops |
min-width 1200px | Screens, 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 standard 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 appear like this:
<desk position=”presentation” border="0" cellpadding="0" cellspacing="0" align="heart" class="deviceWidth"> <tr> <td model="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 a substitute of fifty% as a result of Outlook will be very choosy about what matches side-by-side and what doesn’t. You should use 50% width in the event you set all of your kinds proper (no border, padding, and many others).
If you wish to create a three-column part utilizing comparable 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, so 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!essential; padding:0;} .heart {text-align: heart!essential;} }
You’ll be able to proceed so as to add media queries with particular kinds to cowl as many alternative display sizes as you’d like. You must also add code to your media queries to optimize font-size and line-height for every display dimension. It will enhance readability to your recipients and ensure your electronic mail seems to be 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 dimension. Going again to that two-column format instance, it’d 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 purchasers, 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 photos 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 shopper is barely completely different, and media queries are one technique to deal with these variations.
With regards to Yahoo! Mail inboxes, there’s a media question you’ll be able to attempt utilizing that particularly targets this electronic mail shopper. You would use this media question and add kinds that solely set off in Yahoo!
<model kind="textual content/css"> @media display yahoo{ { /* Place your kinds right here */ } } </model>
This can be utilized to handle format or rendering points that you just see solely in Yahoo! Mail or to incorporate messages meant 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 purchasers received’t change your electronic mail design in any respect, leading to a extremely poor electronic mail expertise — both by fully inverting the colours of your electronic mail, rendering it inaccessible and unreadable or by protecting it shiny once they’ve initiated darkish mode, hurting their eyes.
To stop this, you’ll be able to show completely different designs for darkish mode utilizing this media question:
@media (prefers-color-scheme: darkish )
E-mail shopper help for media queries
Sadly, not each electronic mail shopper helps media queries. What would the lifetime of an electronic mail developer be with out curveballs thrown by particular electronic mail purchasers? (Yeah, it’s Outlook and Gmail…it all the time is.)
Right here’s a rundown of which electronic mail purchasers help media queries, and which don’t:
Consumer | Supported? |
Apple Mail macOS | Sure |
Apple Mail iOS | Sure |
Gmail (net/desktop) | Partial |
Gmail (Android) | Partial |
Gmail (iOS) | Partial |
Gmail (cellular 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 |
(Due to our buddies at Can I E-mail for the data. Test them out for the newest on media queries and extra.)
You’ll discover sturdy help from purchasers utilizing WebKit, however you’ll want to make use of workarounds for inconsistent help in Gmail. For instance, you need to use min-width and max-width for responsiveness, however not (prefers-color-scheme) for darkish mode emails.
Media queries: Another excuse to check
With inconsistent help throughout electronic mail purchasers and completely different designs for cellular and net, testing turns into important. Particularly for finicky purchasers like Gmail, the place something unsuitable within the CSS renders the e-mail unreadable. E-mail on Acid gives limitless electronic mail testing in main mailbox suppliers and the most well-liked gadgets. Which means you can also make certain your electronic mail seems to be good earlier than it hits the inbox. Need to see for your self? Benefit from 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 revealed in October 2016.
Writer: The E-mail on Acid Workforce
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.
Writer: The E-mail on Acid Workforce
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.