Force table column widths to always be fixed regardless of contents
Set the column widths with CSS table-layout: fixed;
and define width
for <th>
/<td>
. Content won't stretch columns:
Applying table-layout: fixed;
sets equal width: 100px;
for each cell, overriding content size.
Going modern with table configuration
Ancient HTML attributes may still lurk in your code. But, for web standards stake (and the health of your site), switch to CSS for table dimensions. This means no width
attribute on the table:
Go modern:
Dealing with content overflow
Despite your best efforts, long texts or images might try to break free from their cells. Luckily, you have word-wrap: break-word;
and overflow: hidden;
:
Designing fixed tables for the small screens
Want your fixed layout table to play nice with different screens? Use percentages for width:
Ensure that the percentages sum up to 100%. Else, your layout might start to look like a Picasso painting (abstract and messy!).
Act upon the cross-browser quirkiness
Just like cats, browsers have personalities too. Test your code in different browsers before launching it into the web wilderness:
Being flexible
You're not always stuck with rigid columns. Mix some flexible columns for a hybrid table that's strong and flexible:
In this code, the chef's surprise is the .flexible-width
column that adjusts to the display, while the .fixed-width
columns stay the same.
Let's be semantic
Prevent div zombies from taking over your table. Container divs
in cells can make things complex and stray from good old semantic HTML.
Instead, apply styles to th
or td
elements. This clause doesn't div-iate from HTML sanity!
Catering to emails
Designing tables for emails is like playing a game on hard mode: email clients can be tricky. Stick to inline styles and expansive CSS. And test, test, and test some more!
Prioritize accessibility
Tables should be accessible to all. Balance fixed column widths and readability. Use appropriate scope
for headers and aria
roles to make tables usable for assistive technologies.
Was this article helpful?