CSS Tables
HTML builds the grid of rows and cells. CSS decides whether borders look clean, how wide the table sits, how text sits in a cell, and whether rows are easy to scan.
A table CSS toolkit
📊 Table styles at a glance
| Property | Everyday job |
|---|---|
border-collapse |
Merge cell borders (collapse) or keep them apart (separate) |
border-spacing |
Gap between cells when borders are separate |
width |
Stretch the table (often 100% in a card or article) |
padding on th/td |
Breathing room inside cells |
text-align / vertical-align |
Horizontal and vertical cell alignment |
Style the table, th, td, and sometimes tr — not every cell one by one — so a whole data grid stays consistent.
Borders that do not double up
Without help, adjacent cell borders can look thick or doubled. border-collapse: collapse merges them into one clean line — the everyday choice for data tables.
table.data {
width: 100%;
border-collapse: collapse;
}
table.data th,
table.data td {
border: 1px solid #ccc;
padding: 0.5rem 0.75rem;
}
Separate borders and spacing
If you want a visible gap between cells, use border-collapse: separate and set border-spacing. Spacing is ignored when borders are collapsed.
table.cards {
border-collapse: separate;
border-spacing: 0.5rem;
}
table.cards td {
border: 1px solid #ddd;
padding: 0.75rem;
}
Width, headers, and alignment
A full-width table often reads better in articles. Bold headers, center or left-align text, and use vertical-align when cell heights differ.
table.data th {
text-align: left;
background: #f5f5f5;
font-weight: 700;
}
table.data td {
text-align: left;
vertical-align: top;
}
Zebra rows and calm hover
Alternating row backgrounds help the eye track across wide tables. A light :hover on tr can highlight the active row without neon noise.
table.data tbody tr:nth-child(even) {
background: #fafafa;
}
table.data tbody tr:hover {
background: #eef5ff;
}
A calm data-table habit
Collapse borders, give cells real padding, keep header contrast modest, and add zebra or hover only when the table is wide enough to need help scanning.
article table {
width: 100%;
border-collapse: collapse;
margin: 1rem 0;
}
article th,
article td {
border: 1px solid #e0e0e0;
padding: 0.6rem 0.8rem;
}
Try it yourself
- Put
border-collapse: collapseand a light border onth/td. - Add comfortable
paddinginside cells. - Left-align headers; try
vertical-align: topon multi-line cells. - Stripe even rows with
tr:nth-child(even). - Add a soft
tr:hoverbackground — then compare withborder-spacingon a second demo table.
Common mistakes
- Leaving default separate borders so lines look doubled and messy
- Setting
border-spacingwhileborder-collapse: collapseis on (spacing is ignored) - Tiny padding that makes dense data hard to read
- Neon hover or heavy zebra that fights the content
- Mixing this lesson with
list-stylemarkers (previous session)
Summary
border-collapse: collapseis the calm default for data tables- Use
separate+border-spacingwhen you want gaps between cells - Width, padding, and alignment shape readability
- Zebra rows and light hover help scanning on wide tables
- Keep structure in HTML; polish appearance with CSS
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top