Academic Block

CSS Tables Get Certified

What are CSS Tables?

CSS Tables allow you to style HTML tables with colors, borders, spacing, hover effects, zebra striping, and responsive behavior. Proper styling makes tables more readable and professional.

Step 1: Basic Table Styling

What this code does: Turns a plain HTML table into a neat grid. Collapsing the borders gives single thin lines between cells, padding gives every cell room to breathe, and the table stretches across the full width. In the preview you will see a two-column table (Name and Age) with a header row and one data row, all aligned to the left.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-table {
    border-collapse: collapse;
    width: 100%;
}
.demo-table th, .demo-table td {
    border: 1px solid #ddd;
    padding: 12px;
    text-align: left;
}
</style>
</head>
<body>
<table class="demo-table">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Alice</td>
<td>25</td>
</tr>
</table>
</body>
</html>

Step 2: Style the Table Header

What this code does: Makes the header row stand out from the data. The header cells (th) get a blue background and white text, while the data cells (td) keep a plain white look. In the preview you will see a blue header (Student and Marks) above the row for Emma.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-table {
    border-collapse: collapse;
    width: 100%;
}
.demo-table th {
    background: #007BFF;
    color: white;
    padding: 12px;
    border: 1px solid #ddd;
    text-align: left;
}
.demo-table td {
    padding: 12px;
    border: 1px solid #ddd;
}
</style>
</head>
<body>
<table class="demo-table">
<tr>
<th>Student</th>
<th>Marks</th>
</tr>
<tr>
<td>Emma</td>
<td>92</td>
</tr>
</table>
</body>
</html>

Step 3: Add Zebra Striping

What this code does: Colours every second row light gray (zebra striping) with :nth-child(even), so the eye can follow a row across the table. The header counts as row 1, so in the preview the Laptop row (row 2) is gray and the Mouse row (row 3) is white, under a green header.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-table {
    border-collapse: collapse;
    width: 100%;
}
.demo-table th {
    background: #28a745;
    color: white;
}
.demo-table th, .demo-table td {
    padding: 12px;
    border: 1px solid #ddd;
    text-align: left;
}
.demo-table tr:nth-child(even) {
    background: #f2f2f2;
}
</style>
</head>
<body>
<table class="demo-table">
<tr>
<th>Product</th>
<th>Price</th>
</tr>
<tr>
<td>Laptop</td>
<td>$900</td>
</tr>
<tr>
<td>Mouse</td>
<td>$25</td>
</tr>
</table>
</body>
</html>

Step 4: Add Hover Effect

What this code does: Highlights the row under the mouse pointer in light yellow, so readers can follow one row across the table. Move the pointer over the Paris and Tokyo rows in the preview: the highlight follows the pointer, while the orange header never changes.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-table {
    border-collapse: collapse;
    width: 100%;
}
.demo-table th {
    background: #ff9800;
    color: white;
}
.demo-table th, .demo-table td {
    border: 1px solid #ddd;
    padding: 12px;
    text-align: left;
}
.demo-table tr:hover {
    background: #fff3cd;
}
</style>
</head>
<body>
<table class="demo-table">
<tr>
<th>City</th>
<th>Country</th>
</tr>
<tr>
<td>Paris</td>
<td>France</td>
</tr>
<tr>
<td>Tokyo</td>
<td>Japan</td>
</tr>
</table>
</body>
</html>

Step 5: Create a Responsive Table

What this code does: Puts the table inside a container that scrolls sideways when the table is wider than the screen, instead of breaking the page layout. The table has a minimum width of 500px, so make the preview window narrower than that and a horizontal scrollbar appears under the table.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-table-container {
    overflow-x: auto;
}
.demo-table {
    width: 100%;
    min-width: 500px;
    border-collapse: collapse;
}
.demo-table th {
    background: #007BFF;
    color: white;
}
.demo-table th, .demo-table td {
    border: 1px solid #ddd;
    padding: 12px;
    text-align: left;
}
</style>
</head>
<body>
<div class="demo-table-container">
<table class="demo-table">
<tr>
<th>Employee</th>
<th>Department</th>
<th>Salary</th>
</tr>
<tr>
<td>David</td>
<td>Sales</td>
<td>$4500</td>
</tr>
</table>
</div>
</body>
</html>

Complete CSS Table Example

What this code does: Combines the earlier techniques in one table: a blue header, zebra stripes on even rows, a light blue hover highlight, and a scrollable container for small screens. In the preview the HTML and JavaScript rows are striped, and the row under the mouse pointer turns light blue.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
    background: #f5f9ff;
}
.demo-table-container {
    overflow-x: auto;
}
.demo-table {
    width: 100%;
    min-width: 480px;
    border-collapse: collapse;
}
.demo-table th {
    background: #007BFF;
    color: white;
}
.demo-table th, .demo-table td {
    border: 1px solid #ddd;
    padding: 12px;
    text-align: left;
}
.demo-table tr:nth-child(even) {
    background: #f8f9fa;
}
.demo-table tr:hover {
    background: #dbeafe;
}
</style>
</head>
<body>
<div class="demo-table-container">
<table class="demo-table">
<tr>
<th>Course</th>
<th>Level</th>
<th>Duration</th>
</tr>
<tr>
<td>HTML</td>
<td>Beginner</td>
<td>2 Weeks</td>
</tr>
<tr>
<td>CSS</td>
<td>Intermediate</td>
<td>3 Weeks</td>
</tr>
<tr>
<td>JavaScript</td>
<td>Advanced</td>
<td>6 Weeks</td>
</tr>
</table>
</div>
</body>
</html>

Advantages of CSS Tables

Advantage
Benefit
Description
Better Readability
Easy Viewing
Makes table data easier to understand.
Professional Design
Modern Look
Improves the appearance of HTML tables.
Responsive Layout
Mobile Friendly
Tables can adapt to smaller screens.
Interactive
Better User Experience
Hover effects make rows easier to identify.

Best Practices

  • Use border-collapse: collapse; for clean borders.
  • Add enough padding for better readability.
  • Highlight table headers with a different background color.
  • Use zebra striping for large tables.
  • Add hover effects for interactive tables.
  • Keep text aligned consistently.
  • Make tables responsive for mobile devices.
  • Avoid using too many colors and borders.

Web Resources on CSS Tables

1. MDN Web Docs : CSS table
2. MDN Web Docs : Styling tables
3. CSS Tricks.com : Accessible, Simple, Responsive Tables
4. Dev.to : A Guide to Styling Tables

๐Ÿงช Test Your CSS Code

Edit the CSS code on the left and click “Run Code” to see the result on the right.

๐Ÿ“ CSS Code
๐Ÿ‘๏ธ Preview (HTML rendered with your CSS)

Click “Run Code” to see the result here.