Sub topic 10
Writing and hiding preheader text
This chapter assumes you know basic HTML and CSS, and that you have read the earlier chapters on table-based layout and inline CSS. You should be comfortable with a template that has a <body>, an outer table, and inline styles.
What a preheader is
A preheader is the short summary line that many email clients show next to the subject line in the inbox list. Gmail, Apple Mail, Outlook, and most mobile clients show it. Some clients show it on the same line as the subject, and others show it below.
The client does not read a special field. It takes the first text it finds in the body of the message. If you do nothing, that text is often "View this email in your browser" or a navigation link, and the preview is wasted.
A good preheader does three things:
- It extends the subject line instead of repeating it.
- It fits in the space the client allows.
- It stays out of sight once the message is opened.
How clients choose preview text
Clients build the preview by stripping the HTML tags and collecting the visible-looking text from the start of the body. They do not render the message first, so they have limited ability to tell which text you meant to hide. This is why the usual technique is to put a hidden block at the very start of the <body> and rely on the client reading the text anyway.
The result is a trade-off. The text must be hidden from the person reading the opened message, but it must still be present in the HTML so the client can read it for the preview.
Preview length varies by client, device, and window width. As a working rule, plan for about 40 to 90 characters of useful text. Put the key words first, because anything past the limit is cut off.
The hiding technique
Here is the preheader block from the template used throughout this tutorial.
<div style='display:none;max-height:0;overflow:hidden;opacity:0;mso-hide:all;font-size:1px;line-height:1px;color:#f4f4f4;'>
Your order has shipped and should arrive on Friday. ‌ ‌ ‌
</div>Each declaration covers a different client behavior. Using several together is deliberate, because no single one works everywhere.
| Declaration | Purpose |
|---|---|
| `display:none` | Removes the block from the layout in clients that honor it. |
| `max-height:0` | Collapses the block if `display:none` is ignored or stripped. |
| `overflow:hidden` | Clips any content that would spill out of a zero-height box. |
| `opacity:0` | Makes any remaining rendering invisible. |
| `mso-hide:all` | Hides the block in desktop Outlook, which uses the Word rendering engine. |
| `font-size:1px; line-height:1px` | Shrinks the text so that, if it does show, it takes almost no space. |
| `color:#f4f4f4` | Matches the page background, so a leaked block is hard to see. |
The color should match the background behind the block. In this template the page background is #f4f4f4, so that is the value used. If you change the page background, change this color too.
Where to place it
Put the block immediately after the opening <body> tag and before the outer table. The client reads the body from the top, so anything placed earlier becomes the preview. Do not put a logo, a skip link, or a "view in browser" link above it. Those may be text, and the client will use them first.
<body style='margin:0;padding:0;background-color:#f4f4f4;'>
<div style='display:none;max-height:0;overflow:hidden;opacity:0;mso-hide:all;font-size:1px;line-height:1px;color:#f4f4f4;'>
Your order has shipped and should arrive on Friday. ‌ ‌ ‌
</div>
<table role='presentation' width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td align='center'>
Main content goes here.
</td>
</tr>
</table>
</body>The padding characters
The sequence ‌ repeated at the end of the preheader is padding. If your sentence is short, the client fills the rest of the preview space with the next text it finds, which is usually the first line of the body, such as a greeting or the header text. Padding prevents that.
is a non-breaking space. It counts as content but shows as blank.‌is a zero-width non-joiner. It has no width, and it stops some clients from collapsing neighboring spaces into one.
Alternating the two keeps clients from treating the run as ordinary whitespace and trimming it. Without that, the padding could be stripped and the body text would show again.
The number of pairs you need depends on the preview length of the clients you target. Some templates repeat the pair more than a hundred times. For a short sentence, generate the padding rather than typing it by hand.
def build_preheader(text, target_length=150):
"""Return preheader HTML padded to roughly target_length characters."""
pad_unit = " ‌"
# Each pair shows up as two characters in the rendered preview.
pairs_needed = max(0, (target_length - len(text)) // 2)
padding = pad_unit * pairs_needed
style = (
"display:none;max-height:0;overflow:hidden;opacity:0;"
"mso-hide:all;font-size:1px;line-height:1px;color:#f4f4f4;"
)
return f"<div style='{style}'>\n{text}{padding}\n</div>"
if __name__ == "__main__":
print(build_preheader("Your order has shipped and should arrive on Friday."))Run this when your build step produces the template. It keeps the text and the padding consistent as the copy changes.
Writing the text
The preheader works with the subject line as a pair. Write them together.
- Do not repeat the subject. If the subject is "Your order has shipped", a preheader of "Your order has shipped" wastes the space.
- Add the next useful fact, such as a delivery day, a deadline, or the main benefit.
- Lead with the important words, because the end may be cut.
- Keep it specific. "Arrives Friday. Track it here." gives the reader more than "Click to read more."
- Match the tone of the message, and avoid text that reads like template filler.
Compare these pairs:
| Subject | Weak preheader | Stronger preheader |
|---|---|---|
| Your order has shipped | Your order has shipped | Arriving Friday, with tracking inside |
| Reset your password | Having trouble viewing this email? | This link expires in 30 minutes |
| Your invoice for March | Invoice | Total due: $42.00 by April 5 |
Escaping and special characters
The preheader is HTML, so escape &, <, and > in the text, the same as anywhere else. Dynamic values such as a customer's name or an order number should be escaped before they go into the block. An unescaped value can break the markup, and because the block is hidden you may not notice during a visual check.
Edge cases and failure modes
The block shows when it should not
Some clients strip <style> blocks or ignore certain properties. This is why the block uses inline styles and several hiding properties. If you see the text, check that the inline style attribute was not removed by an editor or an email service provider, and that your build step did not move it.
The preview shows the wrong text
If the preview shows your navigation or header text, something sits above the preheader, or the client is skipping the hidden block. Check the order of elements at the top of the body. Some clients ignore text that is hidden with display:none and use the first visible text instead. In that case, test other hiding methods against your target clients, and accept that you may not control the preview in every client.
Dark mode and background color
The matching color trick depends on the background. In dark mode, a client may change your background color, and the preheader color can then stand out if the block leaks. Because the block is hidden in most clients, this is rare, but check it during testing. The dark mode chapter covers the color overrides.
Screen readers
Hidden content with display:none is generally not read aloud by screen readers when the message is open, which is what you want. The preheader is usually read in the inbox list as part of the message summary, depending on the client. Write it as a plain sentence, not a string of keywords, so it makes sense whichever way it is read.
Emoji and long text
Emoji work in most clients but may display differently, and they take more preview space in some clients. Test them. Do not put important information only in the preheader, because some clients show it and others do not. The same information should also appear in the visible body.
A visible preheader pattern
Some designs want a visible line above the header, such as "View this email in your browser", as part of the layout. If you do this, put the hidden preheader first and the visible line second. The hidden block then supplies the preview, and the visible line appears after it.
<body style='margin:0;padding:0;background-color:#f4f4f4;'>
<div style='display:none;max-height:0;overflow:hidden;opacity:0;mso-hide:all;font-size:1px;line-height:1px;color:#f4f4f4;'>
Arriving Friday, with tracking inside. ‌ ‌ ‌
</div>
<table role='presentation' width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td align='center' style='padding:8px;font-family:Arial,Helvetica,sans-serif;font-size:12px;line-height:16px;color:#555555;'>
<a href='https://example.com/view' style='color:#555555;'>View this email in your browser</a>
</td>
</tr>
</table>
</body>How the pieces fit
Checklist
- Place the hidden block directly after
<body>, before any other text. - Write text that extends the subject and puts the key words first.
- Use inline styles with
display:none,max-height:0,overflow:hidden,opacity:0, andmso-hide:all. - Match the text color to the background behind the block.
- Add
‌padding so the body text does not fill the preview. - Escape dynamic values.
- Check the inbox preview in your target clients, including dark mode.
Exercise
A template's preview in the inbox reads "View this email in your browser | Order #4821". The hidden preheader block is present, but it was placed after the table that holds the "View this email in your browser" link. What is wrong, and how do you fix it? Also name one reason to add ‌ padding after the preheader sentence.
Show answer
The client builds the preview from the first text in the body. The "View this email in your browser" link comes before the hidden block, so the client uses it first. Move the hidden preheader div to directly after the opening body tag, above the table that holds the link. The padding fills the remaining preview space, so the client does not append the next body text, such as the greeting or header text, after a short preheader sentence.
Exercise
You change the page background from #f4f4f4 to #ffffff. Which part of the preheader block should you update, and why? Which declaration hides the block in desktop Outlook?
Show answer
Update the text color in the preheader block to #ffffff so that, if a client ignores the hiding styles, the text still blends into the background. The declaration that hides the block in desktop Outlook is mso-hide:all.