Sub topic 5
Fluid and hybrid layouts that scale to the screen
This chapter assumes you can write table-based email markup, use inline CSS, and know how max-width and the box model work. Here you will combine them so one template adapts to any screen width.
Fixed, fluid, and hybrid
A fixed layout uses a 600px table and nothing else. On a 375px phone it forces zooming or sideways scrolling.
A fluid layout sizes containers in percentages, so they shrink with the viewport. Fluid alone has a weakness: on a wide desktop window, 100% width makes lines far too long to read.
A hybrid layout caps a fluid container with max-width. It fills a phone screen and stops growing at the desktop width you chose. It also does not depend on media queries, which some clients, such as Gmail with non-Google accounts, ignore.
| Approach | Phone | Wide window | Needs media queries |
|---|---|---|---|
| Fixed width | Overflows | Fine | No |
| Fluid (percent only) | Fine | Lines too long | No |
| Hybrid (percent plus max-width) | Fine | Capped | No |
A fluid, capped container
Set the width attribute to the desktop size, then override it with width:100% and max-width:600px in the style. Clients that read the style get the fluid behavior, and clients that ignore it fall back to 600.
<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;">
<tr>
<td style="padding:24px;font-family:Arial,Helvetica,sans-serif;font-size:16px;line-height:24px;">
This content never grows past 600px and shrinks to fit a phone.
</td>
</tr>
</table>
</td>
</tr>
</table>The outer table stretches across the whole window and centers the inner one. This is the usual wrapper pattern.
Columns that stack without media queries
For side-by-side columns, use div elements with display:inline-block instead of table cells. Each column has width:100% and a max-width of its share. When the container is too narrow for two columns, the second one wraps beneath the first, so nothing needs a media query.
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="max-width:600px;">
<tr>
<td align="center" style="font-size:0;">
<div style="display:inline-block;width:100%;max-width:300px;vertical-align:top;">
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td style="padding:12px;font-family:Arial,Helvetica,sans-serif;font-size:16px;line-height:24px;">Left column</td>
</tr>
</table>
</div>
<div style="display:inline-block;width:100%;max-width:300px;vertical-align:top;">
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td style="padding:12px;font-family:Arial,Helvetica,sans-serif;font-size:16px;line-height:24px;">Right column</td>
</tr>
</table>
</div>
</td>
</tr>
</table>The font-size:0 on the parent removes the small gap that whitespace creates between inline-blocks. Because of it, each column must set its own font size, as above. The padding sits on a table cell inside each div, so the div widths stay exact.
Common errors
- Setting only
width:600pxin CSS and nomax-width. The container stays fixed on phones. - Forgetting
width:100%on the columns. They shrink to their content and do not wrap predictably. - Putting padding on the 300px divs. Padding adds to the width and can push the pair past the container, so they wrap too early.
- Leaving out
vertical-align:top. Columns of unequal height then line up at the baseline.
Exercise
A two-column hybrid layout wraps to one column on a 700px-wide window, even though the container is 600px with two 300px columns. Each column div has padding:12px. What is the likely cause and the fix?
Show answer
Padding on the div adds to its max-width:300px, so each column is 324px wide and the pair needs 648px of space inside a container that may be narrower after its own padding. Move the padding onto a table cell inside each div, leaving the div at exactly 300px, so both columns fit side by side.