CSS Tables

Learn how to style tables with CSS to create beautiful, accessible, and responsive data displays

What is CSS Table Styling?

CSS table styling allows you to customize the appearance and behavior of HTML tables. Well-styled tables improve data readability, user experience, and the overall aesthetics of your website. CSS provides numerous properties to control table layout, borders, spacing, and responsiveness.

Table Styling Categories:

  • Layout & Structure - borders, spacing, alignment
  • Visual Enhancement - colors, zebra striping, hover effects
  • Responsive Design - mobile-friendly tables
  • Accessibility - ensuring tables are usable by everyone
  • Advanced Features - caption, colgroup, fixed headers
NameValue
Item 1100
Item 2200

CSS Table Properties Reference

PropertyDescriptionCommon Values
border-collapseSets whether table borders are collapsedcollapse, separate
border-spacingSets distance between borders when separatedlength values
caption-sideSets position of table captiontop, bottom
empty-cellsSets whether to display borders on empty cellsshow, hide
table-layoutSets table layout algorithmauto, fixed
vertical-alignSets vertical alignment of cell contenttop, middle, bottom
text-alignSets horizontal alignment of cell contentleft, center, right, justify
paddingSets space inside table cellslength values
widthSets width of table or columnspx, %, auto
borderSets border properties for table elementswidth style color

CSS Table Styling in Action

Example Code

/* CSS Table Properties */

/* Basic table styling */
table {
  width: 100%; /* Full width table */
  border-collapse: collapse; /* Remove space between borders */
  margin: 20px 0; /* Add spacing around table */
  font-family: Arial, sans-serif; /* Set font */
}

/* Table caption */
caption {
  caption-side: top; /* Position above table */
  font-weight: bold;
  font-size: 1.2em;
  margin-bottom: 10px;
  text-align: left;
}

/* Table headers and cells */
th, td {
  padding: 12px 15px; /* Cell padding */
  text-align: left; /* Text alignment */
  border-bottom: 1px solid #ddd; /* Bottom border */
}

/* Table header specific styling */
th {
  background-color: #f8f9fa; /* Header background */
  font-weight: bold;
  color: #333;
}

/* Table row styling */
tr {
  transition: background-color 0.3s ease; /* Smooth hover effect */
}

/* Row hover effect */
tr:hover {
  background-color: #f5f5f5;
}

/* Zebra striping */
tr:nth-child(even) {
  background-color: #f9f9f9;
}

/* Table borders */
.bordered {
  border: 1px solid #ddd; /* Outer border */
}

.bordered th, 
.bordered td {
  border: 1px solid #ddd; /* Cell borders */
}

/* Compact table */
.compact th,
.compact td {
  padding: 6px 10px; /* Reduced padding */
  font-size: 0.9em;
}

/* Vertical alignment */
.middle-align th,
.middle-align td {
  vertical-align: middle; /* Center vertically */
}

/* Text alignment */
.text-center {
  text-align: center;
}

.text-right {
  text-align: right;
}

/* Fixed layout for predictable column widths */
.fixed {
  table-layout: fixed;
}

/* Responsive table container */
.table-container {
  overflow-x: auto; /* Horizontal scrolling */
  max-width: 100%;
}

/* Themed tables */
.primary th {
  background-color: #007bff;
  color: white;
}

.success th {
  background-color: #28a745;
  color: white;
}

/* Using CSS variables for table styling */
:root {
  --table-border-color: #dee2e6;
  --table-header-bg: #f8f9fa;
  --table-header-color: #495057;
  --table-hover-bg: rgba(0,0,0,.075);
  --table-stripe-bg: rgba(0,0,0,.02);
  --table-padding: 12px 15px;
}

table {
  --border-width: 1px;
  border-color: var(--table-border-color);
}

th {
  background-color: var(--table-header-bg);
  color: var(--table-header-color);
}

td, th {
  padding: var(--table-padding);
}

tr:hover {
  background-color: var(--table-hover-bg);
}

tr:nth-child(even) {
  background-color: var(--table-stripe-bg);
}

Table Styling Techniques

Basic Table Styles

Fundamental styling techniques for clean, readable tables.

PropertyValue
border-collapsecollapse
padding12px 15px

Zebra Striping

Alternating row colors improve readability of large tables.

NameValue
Item 1100
Item 2200
Item 3300

Hover Effects

Interactive hover states improve user experience.

NameValue
Hover Row 1100
Hover Row 2200

Responsive Tables

Tables that adapt to different screen sizes.

NamePositionOffice
John DoeSoftware EngineerNew York
Jane SmithProduct ManagerSan Francisco

Best Practices for Table Styling

Usability Tips

  • Use adequate padding for readable cell content
  • Ensure sufficient contrast between text and background
  • Use zebra striping for large tables to improve readability
  • Provide visual feedback with hover effects
  • Align numerical data to the right for easier comparison
  • Use clear, descriptive headers for each column

Common Mistakes to Avoid

  • Using tables for layout instead of data presentation
  • Overly complex tables with too many columns
  • Insufficient spacing between cells and content
  • Not making tables responsive for mobile devices
  • Using colors that don't have enough contrast
  • Forgetting to style empty cells appropriately

Accessibility Considerations

  • Use proper table structure with <th> elements for headers
  • Add scope attributes to indicate header direction (row/col)
  • Provide captions or summaries for complex tables
  • Ensure keyboard navigation works correctly
  • Test with screen readers to ensure proper reading order
  • Use ARIA attributes when necessary for complex tables

Ready to Try It Yourself?

Experiment with CSS table styling in our interactive editor. See your changes in real-time and build your understanding through practice.

< PreviousNext >