Sub topic 4
Viewport meta tag and basic client resets
This chapter assumes you know basic HTML and CSS, and that you have read the earlier chapters on email client constraints and table layouts. It covers the small block of code at the top of a template that tells mobile clients how to size the message and removes the default styling clients add.
What the viewport tag does in email
On the web, the viewport meta tag tells a mobile browser to use the device width instead of a simulated desktop width. Email works differently. Most mobile clients already render a message at device width. Some clients ignore the tag, and some strip the <head> entirely.
The tag is still worth including, because clients that read it (for example, some Android mail apps and webmail opened in a mobile browser) otherwise scale a 600px layout down as if it were a desktop page.
<meta name='viewport' content='width=device-width, initial-scale=1'>Treat it as cheap insurance. Never design a template that works only if this tag is honored.
The head block to start from
Three other tags belong next to the viewport tag.
charsetdeclares UTF-8 so accented letters, currency symbols and curly quotes display correctly.x-apple-disable-message-reformattingstops Apple Mail from resizing or reflowing your layout.<title>gives the document a name, which some clients and assistive tools use.
<!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>
</head>
<body style='margin:0;padding:0;'>
</body>
</html>Why resets are needed
Every email client applies its own default styles. One client adds spacing around tables. Another enlarges small text. A third recolors phone numbers and addresses. A reset is a short set of rules that cancels these defaults, so your layout starts from the same baseline everywhere.
A basic reset
Put the reset in a <style> block in the head. The body margin and padding are also set inline, because some clients remove or ignore the style block.
<!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 {
border: 0;
height: auto;
line-height: 100%;
outline: none;
text-decoration: none;
-ms-interpolation-mode: bicubic;
}
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;
}
</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' style='font-family:Arial,Helvetica,sans-serif;font-size:16px;line-height:24px;color:#222222;'>Hello</td>
</tr>
</table>
</body>
</html>What each rule does
| Rule | Purpose |
|---|---|
| `-webkit-text-size-adjust`, `-ms-text-size-adjust` | Stops iOS and Windows clients from changing your font sizes on their own |
| `mso-table-lspace`, `mso-table-rspace` | Removes extra space Outlook adds beside tables |
| `border-collapse: collapse` | Removes gaps between table cells |
| `img` rules | Removes borders on linked images and smooths scaling in old Outlook |
| `a[x-apple-data-detectors]` | Keeps auto-detected phone numbers, dates and addresses in your link style |
Common errors
- Relying on the style block alone. Some clients drop it. Keep essential values, such as body margin and text color, inline.
- Setting
height: autoon every image without awidthattribute. Keep an HTMLwidthattribute on images so Outlook sizes them correctly. - Resetting too much. A universal selector such as
* { margin: 0 }is unnecessary and can break client-specific behavior. Reset only the elements listed above. - Treating the viewport tag as a layout tool. It only sets the starting width. The fluid layout is what makes the template adapt.
Exercise
Why do the reset examples set margin:0;padding:0 inline on the body, even though the style block could do it too?
Show answer
Some email clients strip or ignore the style block in the head. Inline styles on the element survive in far more clients, so the essential baseline values stay in effect even when the embedded styles are lost.