Styling HTML tables with CSS makes them visually appealing, easier to read, and more interactive. Techniques like zebra stripes, vertical stripes, horizontal dividers, and hoverable rows enhance the table’s appearance and usability. This tutorial explains how to apply these styling methods with examples and previews, helping beginners create professional-looking tables.
Table styling involves using CSS to customize the appearance of <table>, <th>, <td>, and <caption> elements. Common styling techniques include alternating row colors (zebra stripes), column highlights (vertical stripes), dividing lines, and hover effects to improve readability and interaction.
Let’s explore key styling techniques with examples and previews, including zebra stripes, vertical stripes, horizontal dividers, and hoverable tables.
Zebra stripes use alternating background colors for rows to improve readability. The CSS :nth-child
pseudo-class targets odd or even rows.
<table style="border: 1px solid black; border-collapse: collapse; width: 100%;">
<caption style="padding: 8px;">Employee Data</caption>
<tr style="background-color: #f2f2f2;">
<th style="border: 1px solid black; padding: 8px;">Name</th>
<th style="border: 1px solid black; padding: 8px;">Role</th>
</tr>
<tr style="background-color: #ffffff;">
<td style="border: 1px solid black; padding: 8px;">Alice</td>
<td style="border: 1px solid black; padding: 8px;">Designer</td>
</tr>
<tr style="background-color: #f2f2f2;">
<td style="border: 1px solid black; padding: 8px;">Bob</td>
<td style="border: 1px solid black; padding: 8px;">Developer</td>
</tr>
</table>
Name | Role |
---|---|
Alice | Designer |
Bob | Developer |
Using CSS :nth-child
for cleaner code:
<style>
table.zebra tr:nth-child(even) { background-color: #f2f2f2; }
table.zebra tr:nth-child(odd) { background-color: #ffffff; }
</style>
<table class="zebra" style="border: 1px solid black; border-collapse: collapse; width: 100%;">
<tr>
<th style="border: 1px solid black; padding: 8px;">Name</th>
<th style="border: 1px solid black; padding: 8px;">Role</th>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Alice</td>
<td style="border: 1px solid black; padding: 8px;">Designer</td>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Bob</td>
<td style="border: 1px solid black; padding: 8px;">Developer</td>
</tr>
</table>
Vertical stripes highlight alternating columns using :nth-child
on <th> and <td>.
<style>
table.vertical-stripes th:nth-child(odd),
table.vertical-stripes td:nth-child(odd) { background-color: #e6f0ff; }
table.vertical-stripes th:nth-child(even),
table.vertical-stripes td:nth-child(even) { background-color: #ffffff; }
</style>
<table class="vertical-stripes" style="border: 1px solid black; border-collapse: collapse; width: 100%;">
<tr>
<th style="border: 1px solid black; padding: 8px;">Product</th>
<th style="border: 1px solid black; padding: 8px;">Price</th>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Pen</td>
<td style="border: 1px solid black; padding: 8px;">$2</td>
</tr>
</table>
Product | Price |
---|---|
Pen | $2 |
Horizontal dividers emphasize row separation with thicker or styled borders.
<style>
table.dividers tr { border-bottom: 2px solid #333; }
table.dividers th, table.dividers td { border-bottom: 2px solid #333; }
</style>
<table class="dividers" style="border: 1px solid black; border-collapse: collapse; width: 100%;">
<tr>
<th style="border: 1px solid black; padding: 8px;">Item</th>
<th style="border: 1px solid black; padding: 8px;">Stock</th>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Notebook</td>
<td style="border: 1px solid black; padding: 8px;">50</td>
</tr>
</table>
Item | Stock |
---|---|
Notebook | 50 |
Hover effects highlight rows when a user’s mouse hovers over them, using the :hover
pseudo-class.
<style>
table.hoverable tr:hover { background-color: #d4eaff; }
</style>
<table class="hoverable" style="border: 1px solid black; border-collapse: collapse; width: 100%;">
<tr>
<th style="border: 1px solid black; padding: 8px;">Fruit</th>
<th style="border: 1px solid black; padding: 8px;">Color</th>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Apple</td>
<td style="border: 1px solid black; padding: 8px;">Red</td>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Banana</td>
<td style="border: 1px solid black; padding: 8px;">Yellow</td>
</tr>
</table>
Fruit | Color |
---|---|
Apple | Red |
Banana | Yellow |
Here are the main CSS properties for styling tables:
background-color: #f2f2f2
).tr:nth-child(even)
).border-bottom: 2px solid #333
).tr:hover { background-color: #d4eaff; }
).padding: 8px
).To make your table styling effective:
max-width: 100%
or scrollable containers for mobile devices.Beginners often make these errors:
:hover
may not work.Create a simple HTML file and experiment with table styling. Combine zebra stripes, vertical stripes, dividers, and hover effects.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
table.styled tr:nth-child(even) { background-color: #f2f2f2; }
table.styled tr:hover { background-color: #d4eaff; }
table.styled th, table.styled td { border-bottom: 2px solid #333; }
</style>
</head>
<body>
<h3>Course Schedule</h3>
<table class="styled" style="border: 1px solid navy; border-collapse: collapse; width: 100%;">
<caption style="padding: 8px; font-weight: bold;">Weekly Classes</caption>
<tr>
<th style="border: 1px solid navy; padding: 8px;">Day</th>
<th style="border: 1px solid navy; padding: 8px;">Class</th>
</tr>
<tr>
<td style="border: 1px solid navy; padding: 8px;">Monday</td>
<td style="border: 1px solid navy; padding: 8px;">Math</td>
</tr>
<tr>
<td style="border: 1px solid navy; padding: 8px;">Tuesday</td>
<td style="border: 1px solid navy; padding: 8px;">Science</td>
</tr>
</table>
</body>
</html>
Day | Class |
---|---|
Monday | Math |
Tuesday | Science |
By mastering CSS styling techniques like zebra stripes, vertical stripes, dividers, and hover effects, you can create visually appealing and user-friendly tables. Practice regularly to get comfortable!