Sub topic 4
Viewport meta tag and basic client resets
This chapter covers the <head> of an email template: the viewport meta tag and a small set of reset rules. I assume you know basic HTML and CSS, the box model, and what a CSS media query does. I also assume you have read the earlier chapters on why email HTML is constrained, table layout, and inline CSS versus embedded style blocks.
Why a starting baseline matters
Every email client applies its own default styles before it applies yours. Those defaults differ. Outlook desktop renders with Microsoft Word's engine. Apple Mail uses WebKit. Gmail rewrites your markup and strips parts of it. An unstyled <body> or <img> can therefore look different in each client.
A reset is a short set of rules that removes the most common differences. It gives every client the same starting point, so the layout you build in later chapters behaves the same way across clients. Email resets are much smaller than web resets, because most of your styling happens inline.
The viewport tag and the resets do different jobs:
- The viewport tag tells mobile rendering engines how wide the screen is.
- The resets neutralize quirks such as extra spacing, automatic text resizing, and automatic link styling.
The diagram shows why the reset rules are a baseline and not a guarantee. Some clients drop or rewrite the head, so anything essential must also exist inline.
The viewport meta tag
On the web, the viewport tag looks like this:
<meta name="viewport" content="width=device-width, initial-scale=1">Without it, a mobile browser assumes a virtual page width, often around 980 pixels, and shrinks the page to fit. Text becomes tiny and media queries written for narrow screens never match. The tag tells the engine to use the real device width.
Email clients treat the tag unevenly:
- Apple Mail on iOS and several Android clients render the message in a web view. They scale content to fit, and they use the tag as a hint.
- Some apps ignore the tag, because they size the message themselves.
- Gmail and Outlook apps may strip the head or never read it.
Include it anyway. It costs one line and does no harm where it is ignored. Where it is honored, it is the difference between a layout that scales properly and one that renders at a desktop width and shrinks.
The tag helps most when your layout already works at narrow widths. It does not make a fixed 800-pixel table fit a phone. That is the job of the fluid layouts you build in later chapters.
Companion meta tags
These tags usually appear alongside the viewport tag:
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="x-apple-disable-message-reformatting">charsetdeclares the character encoding. Without it, characters such as curly quotes and non-English letters may turn into garbage. Make sure the message's real encoding matches.X-UA-Compatibleasks old Outlook-era engines to use their newest mode. It is harmless and mostly a legacy habit.x-apple-disable-message-reformattingasks Apple's clients not to shrink or reflow the message on their own. Without it, Apple Mail sometimes rescales a message it thinks is too wide, which can break your sizing.
The reset rules
Here is a compact reset block, with each rule explained below. It belongs in a <style> element in the <head>.
<style>
body, table, td, a {
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
table, td {
mso-table-lspace: 0pt;
mso-table-rspace: 0pt;
border-collapse: collapse;
}
img {
-ms-interpolation-mode: bicubic;
border: 0;
outline: none;
text-decoration: none;
display: block;
}
body {
margin: 0 !important;
padding: 0 !important;
width: 100% !important;
}
a[x-apple-data-detectors] {
color: inherit !important;
text-decoration: none !important;
font-size: inherit !important;
font-family: inherit !important;
font-weight: inherit !important;
line-height: inherit !important;
}
u + #body a {
color: inherit;
text-decoration: none;
font-size: inherit;
font-family: inherit;
font-weight: inherit;
line-height: inherit;
}
#MessageViewBody a {
color: inherit;
text-decoration: none;
font-size: inherit;
font-family: inherit;
font-weight: inherit;
line-height: inherit;
}
</style>What each rule does
| Rule | Problem it addresses |
|---|---|
| `-webkit-text-size-adjust: 100%` | iOS may enlarge text it considers too small. This stops the automatic resizing. |
| `-ms-text-size-adjust: 100%` | The same protection for Windows Phone era clients and some Microsoft engines. |
| `mso-table-lspace` and `mso-table-rspace` | Outlook adds small gaps to the left and right of tables. Setting both to 0 removes them. |
| `border-collapse: collapse` | Prevents browser-default gaps between table cells. |
| `-ms-interpolation-mode: bicubic` | Makes resized images in older Outlook look smoother. |
| `border: 0` on `img` | Removes the blue border that some clients draw around linked images. |
| `display: block` on `img` | Removes the thin gap that appears under an inline image, because inline images sit on the text baseline. |
| `margin: 0` and `padding: 0` on `body` | Removes default body spacing so your outer table starts at the edge. |
| `a[x-apple-data-detectors]` | Undoes the blue underlined styling that Apple adds to detected dates, addresses, and phone numbers. |
| `u + #body a` | The same fix for Gmail, which wraps the message so that a `u` element precedes the body. |
| `#MessageViewBody a` | The same fix for the Samsung Mail app. |
The !important flags are deliberate. Several clients inject their own styles and you need to win against them. Use !important only in the reset, and keep it out of your normal inline rules.
Using the reset with a body id
The Gmail selector u + #body a only works if your <body> carries the matching id. Here is a minimal head and body that wires everything together:
<!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>
body, table, td, a { -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
table, td { mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-collapse: collapse; }
img { -ms-interpolation-mode: bicubic; border: 0; outline: none; text-decoration: none; display: block; }
body { margin: 0 !important; padding: 0 !important; width: 100% !important; }
a[x-apple-data-detectors], u + #body a, #MessageViewBody a {
color: inherit !important;
text-decoration: none !important;
font-size: inherit !important;
font-family: inherit !important;
font-weight: inherit !important;
line-height: inherit !important;
}
</style>
</head>
<body id="body" style="margin:0;padding:0;background-color:#f4f4f4;">
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="center" style="padding:24px;font-family:Arial,Helvetica,sans-serif;font-size:16px;line-height:24px;color:#222222;">
Hello. This text should not be resized, and this
<a href="https://example.com" style="color:#0b5fff;">link</a> keeps its color.
</td>
</tr>
</table>
</body>
</html>Notice that the body carries its margin and padding inline as well as in the reset. This is the pattern for the whole tutorial: a client that strips the style block still gets the essential values from inline styles.
Common mistakes
- Writing a web-style universal reset. A rule such as
* { margin: 0; padding: 0; box-sizing: border-box; }is risky in email. Some engines handle*poorly, and zeroing the padding of every element can break the table spacing attributes (cellpadding) you rely on. - Resetting only in the style block. If a client drops the style block, the reset disappears. Keep critical values, such as
border="0"on images andmargin:0on the body, inline or as HTML attributes as well. - Forgetting the body id. The Gmail link fix needs
id="body"on the<body>. Without it the selector never matches and no error is shown. - Treating the viewport tag as a responsive fix. It sets the starting width. It does not make a rigid layout flexible.
- Disabling text-size adjustment and then using tiny text. If you stop clients from enlarging small text, you take on the job of making it readable. The typography chapter covers minimum sizes.
- Assuming one client's result applies to all. A reset that looks right in Apple Mail may do nothing in Gmail. Always check the clients you care about.
Summary of the working model
- Declare encoding, viewport, and Apple reformatting hints in the head.
- Add a short reset block for text sizing, table spacing, image behavior, and auto-detected links.
- Repeat the essentials inline, because the head is not guaranteed to survive.
- Test, because each client interprets the head differently.
Exercise
A teammate's template looks right in Apple Mail, but in the Gmail app the phone numbers and addresses appear as blue underlined links that ignore the template's text color. The reset already contains a[x-apple-data-detectors]. What is missing, and what must be true of the <body> for the fix to work?
Show answer
Gmail does not use the x-apple-data-detectors attribute, so that selector has no effect there. Add the Gmail-specific rule u + #body a { color: inherit; text-decoration: none; font-size: inherit; font-family: inherit; font-weight: inherit; line-height: inherit; }. It only matches if the <body> element has id="body", because the selector depends on that id.
Exercise
Why does an image placed inside a table cell often show a thin gap below it, and which reset rule removes the gap?
Show answer
Images are inline elements by default, so they sit on the text baseline and the cell reserves space below them for descenders. Setting display: block on img removes the gap. The border: 0 rule is separate. It removes the border that some clients draw around linked images.
Exercise
You add <meta name="viewport" content="width=device-width, initial-scale=1"> to a template whose main table is a fixed width="800". On a phone in a client that honors the tag, what happens, and what does it tell you about the tag's role?
Show answer
The client uses the device width as the viewport, but the 800-pixel table is still wider than the screen, so the content overflows or is scaled down. The viewport tag only sets the starting width. It does not make a fixed layout flexible. You need a fluid or hybrid layout, such as width:100%; max-width:600px, to adapt to the screen.