Styling Tables with CSS
You'll learn how to transform plain HTML tables into polished, readable data displays using borders, spacing, and alternating row colors.
Why Tables Need Styling
Raw HTML tables work but look cramped and hard to read. CSS lets you add visual structure—borders define cells clearly, spacing gives content room to breathe, and alternating row colors (zebra-striping) help users track across columns without losing their place.
Core Styling Techniques
Borders and Spacing
Use border to outline your table, rows, and cells. The border-collapse property merges adjacent borders into clean lines instead of doubled-up gaps:
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
}
The padding inside cells creates breathing room between content and borders—like margins around text on a page.
Zebra-Striping for Readability
Alternate background colors help eyes track rows horizontally. Use the :nth-child() pseudo-class to style every other row:
tbody tr:nth-child(even) {
background-color: #f2f2f2;
}
This targets even-numbered rows within <tbody>, giving them a light gray background.
Header Styling
Make headers stand out with distinct colors and alignment:
th {
background-color: #4CAF50;
color: white;
text-align: left;
}
The Analogy
Think of an unstyled table like a spreadsheet with no gridlines or formatting—functional but exhausting to read. CSS is your formatting toolbar: adding borders (gridlines), padding (column width), and zebra-stripes (shading alternate rows) transforms raw data into a professional report.
Key Takeaway: CSS borders, padding, and zebra-striping with :nth-child() turn basic HTML tables into clear, scannable data displays that guide the reader's eye naturally across rows and down columns.