Sub topic 2
Structuring a template with tables
This chapter shows how to build the skeleton of an email with nested tables. It assumes you know basic HTML and CSS, including the box model, and that you have read the previous chapter on why email clients restrict HTML and CSS. Fluid widths, media queries and Outlook fallbacks get their own chapters. Here the layout has a fixed 600-pixel width, and the focus is on the structure that makes the later techniques work.
Why tables still do the layout
On the web, you would build this layout with div elements, Flexbox or Grid. Email clients disagree about all three. Desktop Outlook renders messages with the Microsoft Word engine. That engine supports table, tr and td layout well, but it ignores or partly ignores float, display:flex, display:grid, and max-width on div elements. Several webmail clients also strip or rewrite CSS they don't recognize.
Tables are the one layout mechanism that every client renders the same way. A table cell has a predictable width and predictable padding, and it can hold a background color. You use tables for layout only. They carry no data meaning here, so you mark them as presentational (see below).
The anatomy of a template
A dependable template has four layers, nested from the outside in:
- The
body, with margin and padding set to zero. - A full-width wrapper table. Its job is to hold the page background color and center everything.
- A fixed-width container table inside one wrapper cell. This is the "page" the reader sees.
- Rows inside the container, one per content section: header, hero, body copy, columns, footer.
The wrapper exists because body styling is unreliable. Some clients drop the body element and wrap your markup in their own container, which can discard a background color set on body. A background on the wrapper table survives. You can still set it on body as well.
The attributes that matter
Email HTML leans on old presentational attributes because they are the safest option in the oldest clients. Put these on every layout table:
| Attribute | Value | Purpose |
|---|---|---|
| `role` | `presentation` | Tells screen readers this is layout, not a data table |
| `cellpadding` | `0` | Removes the default padding inside cells |
| `cellspacing` | `0` | Removes the default gap between cells |
| `border` | `0` | Removes the default border some clients draw |
| `width` | `100%` or a pixel number | Sets the table width in clients that ignore CSS width |
On cells, the useful attributes are align (horizontal alignment of the cell's content), valign (vertical alignment), width, and bgcolor (a background color fallback for clients that drop CSS).
A complete skeleton
The template below follows the four-layer model. It has a header, a text section, a two-column row built from a nested table, and a footer. Save it as an .html file and open it in a browser to see it.
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<title>Order confirmation</title>
</head>
<body style='margin:0;padding:0;background-color:#f4f4f4;'>
<table role='presentation' width='100%' cellpadding='0' cellspacing='0' border='0' bgcolor='#f4f4f4' style='background-color:#f4f4f4;'>
<tr>
<td align='center' style='padding:24px 0;'>
<table role='presentation' width='600' cellpadding='0' cellspacing='0' border='0' bgcolor='#ffffff' style='width:600px;background-color:#ffffff;'>
<tr>
<td align='center' style='padding:24px;font-family:Arial,Helvetica,sans-serif;font-size:22px;line-height:28px;font-weight:bold;color:#222222;'>Example Store</td>
</tr>
<tr>
<td style='padding:0 24px 24px 24px;font-family:Arial,Helvetica,sans-serif;font-size:16px;line-height:24px;color:#222222;'>
Thanks for your order. It ships tomorrow.
</td>
</tr>
<tr>
<td style='padding:0 24px 24px 24px;'>
<table role='presentation' width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td width='50%' valign='top' style='padding-right:12px;font-family:Arial,Helvetica,sans-serif;font-size:16px;line-height:24px;color:#222222;'>Shipping address</td>
<td width='50%' valign='top' style='padding-left:12px;font-family:Arial,Helvetica,sans-serif;font-size:16px;line-height:24px;color:#222222;'>Delivery estimate</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align='center' bgcolor='#eeeeee' style='padding:16px 24px;background-color:#eeeeee;font-family:Arial,Helvetica,sans-serif;font-size:13px;line-height:20px;color:#555555;'>
Example Store, 1 Main Street
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>Read the structure from the outside in. The wrapper has one row and one cell. That cell centers the container with align='center'. The container has four rows, and each row has one cell. The third row's cell holds a nested table, which creates the two columns.
Rules for spacing
Spacing is where table layouts usually break. Follow these rules:
- Use padding on
tdelements. Padding on a cell is honored in every major client. Margin on tables, paragraphs and divs is dropped or altered by some clients, including desktop Outlook for many elements. - Keep padding on the cell that holds the content. In the skeleton, the text cell carries its own padding. Do not rely on padding applied to an inner
divorp. - Make column gaps with padding on the cells. In the nested table, the left cell has
padding-rightand the right cell haspadding-left. Both cells keepwidth='50%', so the gap stays inside each half. - Use a spacer row only when padding cannot do the job. A spacer is a row such as
<tr><td height='16' style='height:16px;line-height:16px;font-size:16px;'> </td></tr>. Settingline-heightandfont-sizestops the cell from growing to fit a taller default text line. Prefer padding, since empty rows add markup and are noise for assistive technology.
Padding also adds to the width of a cell in some clients. If a cell is width='300' with padding:24px, the total can exceed 300 pixels. In a fixed layout, give the outer cell the full width and put the padded content in the nested element, or lower the width to allow for the padding.
Where to put styles
Because the layout uses tables, you put visual styling on cells:
- Set
font-family,font-size,line-heightandcoloron eachtdthat holds text. Do not rely on inheriting them frombodyor the wrapper, because some clients reset inheritance at the table boundary. - Set
background-colorin the style attribute andbgcoloras an attribute. The attribute is the fallback. - Write the inline CSS on each element. Later chapters cover why, and what a
styleblock in the head can still do.
How deep to nest
Every nested table adds markup and a chance for client quirks. A practical limit is the container table plus one level of nested tables for columns. If you need three columns, put them in one nested table with three cells. Do not nest a table inside each column unless the column holds something that needs its own layout, such as an image beside text.
Gmail clips messages whose HTML exceeds roughly 102 KB and replaces the rest with a "View entire message" link. Deep nesting and repeated inline styles count toward that size, so keep structure lean.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Thin gaps between rows or cells | Missing `cellpadding='0'` or `cellspacing='0'` | Add both attributes to every layout table |
| Template sits at the left edge | Wrapper cell has no `align='center'` | Add `align='center'` to the wrapper cell |
| Spacing missing in Outlook | Margin used instead of padding | Move spacing to `td` padding |
| Text uses a serif font in one client | Font set on `body` only | Set font properties on each text cell |
| Screen reader announces "table with 4 rows" | `role='presentation'` missing | Add it to every layout table |
| Background color missing | Color set only on `body` | Add it to the wrapper table and cells |
Exercise
A teammate's template looks right in a browser but shows a 2-pixel gap between the header row and the content row in a webmail client. The container table is <table width='600' style='background-color:#ffffff;'>. Name the change you would make, and explain why it fixes the gap.
Show answer
Add role='presentation', cellpadding='0', cellspacing='0' and border='0' to the table, with cellspacing='0' as the fix for the gap. Clients apply their own default cellspacing when the attribute is missing, and that default draws a space between cells. Setting it explicitly to 0 makes every client use the same value. The other attributes remove default padding and borders and mark the table as layout for screen readers.
Exercise
You need a row with three equal columns, each separated by a 16-pixel gap, inside the 600-pixel container. Describe how you would build it using the rules in this chapter.
Show answer
Put one nested table with width='100%' and the standard attributes inside a cell of the container. Give it one row with three td cells at width='33%'. Create the gaps with padding on the cells, not margin: for example, padding-right:8px on the left cell, padding:0 8px on the middle cell, and padding-left:8px on the right cell. Set valign='top' on each cell so the content aligns at the top, and set the font properties on each cell.