Lesson 24: Mastering CSS Tables

Learn how to create beautiful, responsive tables with advanced CSS techniques and best practices.

Start Learning

Understanding Table Styling

CSS provides powerful properties to customize the appearance of tables, making them more readable and visually appealing.

Key Properties: border, padding, background-color, text-align, vertical-align
Modern Approach: Use :nth-child selectors for zebra striping and :hover for interactive effects
Best Practice: Create responsive tables that work on all devices

Table Properties

Property Description
border Specifies the border of the table, usually in pixels
border-top Specifies the top border of table headers
border-bottom Specifies the bottom border of table headers
width Specifies the table width in pixels or percentage
padding Controls spacing between cell border and content
background-color Sets background color of cells or alternating rows
:hover Highlights a row when mouse moves over it
text-align Aligns text to left, center, or right in a cell
vertical-align Aligns content vertically in a cell (top, middle, bottom)

Basic Table Styling

The following example demonstrates fundamental table styling techniques:

/* Basic table styling */
.mytable {
  width: 90%;
  border: 2px solid blue;
  border-collapse: collapse;
}

.mytable th, 
.mytable td {
  padding: 8px 12px;
  border: 1px solid #ddd;
}

.mytable th {
  font-size: 110%;
  text-transform: uppercase;
  font-weight: bold;
  background-color: #f5f7fa;
  border-top: 2px solid #cc6699;
  border-bottom: 2px solid #cc0000;
}

.mytable tr:nth-child(even) {
  background-color: #e3f2fd;
}

.mytable tr:hover {
  background-color: rgba(255, 152, 0, 0.1);
}
Employee Directory
Name Position Department
John Smith Designer Creative
Sarah Johnson Developer Engineering
Michael Brown Manager Operations
Emily Davis Analyst Finance

Interactive Table Styler

Customize table properties and see the results in real-time:

Product Price Stock
Laptop $999.99 15
Smartphone $699.99 32
Tablet $399.99 22
Headphones $199.99 45
/* Generated CSS */
#tablePreview {
  border-collapse: collapse;
  width: 100%;
}

#tablePreview th, 
#tablePreview td {
  border: 1px solid #2196f3;
  padding: 8px;
  text-align: left;
}

#tablePreview th {
  background-color: #0d47a1;
  color: #ffffff;
  font-weight: bold;
}

#tablePreview tr:nth-child(even) {
  background-color: #e3f2fd;
}

#tablePreview tr:hover {
  background-color: #fff8e1;
}

Practical Table Examples

Here are practical implementations of CSS tables for real-world use cases:

Financial Data Table

Stock Price Change Volume
AAPL $172.35 +2.5% 15.2M
MSFT $328.79 +1.3% 8.7M
GOOGL $138.21 -0.8% 5.3M
AMZN $178.22 +0.4% 12.1M

Responsive Table

Name Email Phone Department Position Start Date
John Smith [email protected] (555) 123-4567 Marketing Manager 2022-01-15
Sarah Johnson [email protected] (555) 987-6543 Engineering Developer 2021-11-30
Michael Brown [email protected] (555) 456-7890 Operations Director 2020-05-22

Note: This table is horizontally scrollable on small screens

Minimalist Table

Task Assignee Status
Design homepage John Done
Implement API Sarah In Progress
Write documentation Mike Pending
Important: Always ensure your tables are accessible by using proper semantic HTML, headers, and ARIA attributes when necessary.

Next Steps

You've learned how to create beautiful and functional tables with CSS. Continue your learning journey with the next lesson.