Sub topic 6
Media queries and stacking columns on phones
This chapter assumes you know basic HTML and CSS, the box model, and how a CSS media query works. It also assumes you have read the earlier chapters on table layout, inline CSS, and fluid layouts with max-width. Here you will add the layer that rearranges a two-column layout into one column on a phone.
Why media queries are an enhancement
A media query is a CSS rule that applies only when a condition is true, such as the screen being 600 pixels wide or less. Email clients differ in how well they support them.
- Apple Mail, iOS Mail, and most Samsung and Outlook mobile apps support embedded
<style>blocks and media queries. - Gmail supports them in its own apps and on the web in most cases. Gmail does not support them when it can't read your account's style block, as with some non-Gmail accounts in the Gmail app (GANGA).
- Desktop Outlook on Windows ignores most of them.
Because of this, build the fluid layout first. It must look acceptable with no media queries at all. The query then improves the result where it works.
The stacking pattern
The idea is simple. On wide screens, two table cells sit side by side. On narrow screens, you make each cell display: block and width: 100%. Block cells fall onto separate lines, so the columns stack.
The rule has to live in a <style> block in the <head>, because inline styles can't contain media queries. The cells already have inline width values, and inline styles win over a style block. That is why the query needs !important.
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<meta name='x-apple-disable-message-reformatting'>
<title>Order confirmation</title>
<style>
@media screen and (max-width: 600px) {
.stack { display: block !important; width: 100% !important; }
}
</style>
</head>
<body style='margin:0;padding:0;background-color:#f4f4f4;'>
<table role='presentation' width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td align='center'>
<table role='presentation' width='600' cellpadding='0' cellspacing='0' border='0' style='width:100%;max-width:600px;background-color:#ffffff;'>
<tr>
<td class='stack' width='50%' valign='top' style='padding:24px;font-family:Arial,Helvetica,sans-serif;font-size:16px;line-height:24px;color:#222222;'>Left column</td>
<td class='stack' width='50%' valign='top' style='padding:24px;font-family:Arial,Helvetica,sans-serif;font-size:16px;line-height:24px;color:#222222;'>Right column</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>The outer table is fluid. The inner table is width:100% with max-width:600px, so it shrinks on narrow screens even without the query. The stack class is the hook the query uses.
How the decision flows
Choosing the breakpoint
A breakpoint is the width at which the layout changes. Use the container width, here 600px, as the breakpoint. Below it, the container is already shrinking, so the columns get cramped. Avoid many breakpoints. One is enough for most emails, and each extra one adds more to test.
Common errors
- Forgetting
!important. The inlinewidth='50%'and style values override the query, so nothing stacks. - Putting the query in an inline style. Media queries only work in a
<style>block. - Missing viewport meta tag. Some mobile clients then scale the email down as if it were a desktop page, and the query never matches.
- Relying on the query for basic usability. If the email only works when stacked, desktop Outlook and GANGA users get a broken layout.
- Column order surprises. Stacked cells follow source order. Put the more important content first in the HTML.
Exercise
A two-column template has inline width='50%' on each cell and a media query that sets .stack { display: block; width: 100%; }. On a phone in Apple Mail the columns stay side by side. What is the most likely cause, and how do you fix it?
Show answer
The inline width on the cells has higher priority than the style block rule, so the query's width: 100% is ignored (and display: block can also be overridden by inline styles). Add !important to the declarations in the media query: .stack { display: block !important; width: 100% !important; }.
Exercise
Why should you build the fluid layout before adding the stacking media query?
Show answer
Some clients, such as desktop Outlook and Gmail with non-Gmail accounts, ignore embedded styles and media queries. The fluid layout (width:100% with max-width) is what those readers see, so it must work without the query.