Table Styling

What You'll Learn

  • Master border-collapse and border-spacing for table borders
  • Create striped rows using :nth-child() pseudo-class
  • Add hover effects and transitions for interactive tables
  • Implement sticky table headers with position: sticky
  • Make tables responsive with horizontal scroll techniques
  • Control cell alignment with text-align and vertical-align
  • Style table captions with caption-side property
  • Build modern table designs with professional styling patterns

Introduction to Table Styling

Tables are essential for displaying structured data in rows and columns. While HTML provides the semantic structure with <table>, <thead>, <tbody>, and <th>/<td> elements, CSS transforms them into polished, professional components. Proper table styling improves readability and helps users quickly scan and compare data.

Modern table styling includes techniques for border control, alternating row colors, hover states, sticky headers, and responsive layouts. These patterns work together to create tables that are both functional and visually appealing across all device sizes.

border-collapse Property

The border-collapse property controls how table borders are rendered. By default, each cell has its own border with spacing between cells (separate). The collapse value merges adjacent borders into a single border, creating the clean table appearance most designs require.

/* Separate borders (default) */ table { border-collapse: separate; border-spacing: 10px; /* Gap between cells */ } /* Collapsed borders (most common) */ table { border-collapse: collapse; /* Adjacent borders merge into one */ } /* Example with collapsed borders */ .table { border-collapse: collapse; width: 100%; } .table th, .table td { padding: 12px; border: 1px solid #ddd; }

Striped Rows with :nth-child

Alternating row colors (zebra stripes) improve readability by helping users track data across columns. The :nth-child() pseudo-class makes this pattern trivial to implement without adding classes to every row.

/* Alternate row colors */ tbody tr:nth-child(odd) { background: #f9f9f9; } tbody tr:nth-child(even) { background: white; } /* Alternative: nth-child(2n) targets even rows */ tbody tr:nth-child(2n) { background: #f0f7ff; } /* Complete striped table */ .table-striped { border-collapse: collapse; width: 100%; } .table-striped th, .table-striped td { padding: 12px; text-align: left; border-bottom: 1px solid #ddd; } .table-striped thead { background: #3b82f6; color: white; } .table-striped tbody tr:nth-child(odd) { background: #f9f9f9; }

Hover Effects

Adding hover effects to table rows provides visual feedback and helps users focus on specific data. Combine :hover with transitions for smooth, professional interactions.

tbody tr { transition: background-color 0.3s ease; } tbody tr:hover { background: #f1f8f4; cursor: pointer; } /* Combine with striped rows */ .table-hover tbody tr:nth-child(odd) { background: white; } .table-hover tbody tr:nth-child(even) { background: #f9f9f9; } .table-hover tbody tr:hover { background: #e3f2fd; }

Sticky Headers

Sticky table headers remain visible as users scroll through long tables, maintaining context for each column. This requires position: sticky on header cells and a scrollable container with defined height.

/* Scrollable container */ .table-container { max-height: 400px; overflow-y: auto; } /* Sticky header */ thead th { position: sticky; top: 0; background: #9c27b0; color: white; z-index: 10; box-shadow: 0 2px 2px rgba(0, 0, 0, 0.1); } /* Important: parent needs overflow */ .table-container table { border-collapse: collapse; width: 100%; }

Responsive Tables

Wide tables with many columns need special handling on mobile devices. The horizontal scroll approach wraps the table in a scrollable container, allowing users to swipe through columns while maintaining the table layout.

.table-responsive { overflow-x: auto; -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */ } .table-responsive table { border-collapse: collapse; width: 100%; min-width: 600px; /* Ensures horizontal scroll */ } .table-responsive td, .table-responsive th { white-space: nowrap; /* Prevents text wrapping */ padding: 12px; }

Cell Alignment

Proper cell alignment improves readability and helps users compare values. Use text-align for horizontal alignment and vertical-align for vertical positioning within cells.

/* Horizontal alignment */ .align-left { text-align: left; } .align-center { text-align: center; } .align-right { text-align: right; } /* Vertical alignment */ .valign-top { vertical-align: top; } .valign-middle { vertical-align: middle; } .valign-bottom { vertical-align: bottom; } /* Common patterns */ th { text-align: left; /* Or center for numeric columns */ } td.number { text-align: right; /* Right-align numbers */ } td.actions { text-align: center; /* Center buttons/icons */ }

Caption Styling

The <caption> element provides a title or description for tables. Style it with caption-side to position it above or below the table, and apply standard text styling properties.

caption { caption-side: top; /* or bottom */ padding: 12px; font-size: 1.2em; font-weight: 600; text-align: left; color: #333; background: #f5f5f5; } /* Caption at bottom */ .table-caption-bottom caption { caption-side: bottom; font-style: italic; font-size: 0.9em; }

Modern Table Designs

Combine multiple techniques to create polished, professional table designs. These patterns include shadows, rounded corners, smooth transitions, and thoughtful color schemes.

Modern Card-Style Table

.table-modern { border-collapse: collapse; width: 100%; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); border-radius: 8px; overflow: hidden; } .table-modern thead { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); } .table-modern th { padding: 16px; color: white; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; font-size: 0.85em; } .table-modern td { padding: 16px; border-bottom: 1px solid #f0f0f0; } .table-modern tbody tr { transition: all 0.3s ease; } .table-modern tbody tr:hover { background: #f8f9fa; transform: scale(1.01); } .table-modern tbody tr:last-child td { border-bottom: none; }

Minimal Clean Table

.table-minimal { border-collapse: collapse; width: 100%; } .table-minimal th, .table-minimal td { padding: 12px 8px; text-align: left; } .table-minimal thead { border-bottom: 2px solid #333; } .table-minimal th { font-weight: 600; color: #333; } .table-minimal tbody tr { border-bottom: 1px solid #eee; } .table-minimal tbody tr:hover { background: #fafafa; }

Status Badges in Tables

Tables often display status information. Create reusable badge components with clear colors and consistent styling for status indicators.

.badge { display: inline-block; padding: 4px 12px; border-radius: 12px; font-size: 0.85em; font-weight: 600; } .badge-success { background: #d1fae5; color: #065f46; } .badge-warning { background: #fef3c7; color: #92400e; } .badge-error { background: #fee2e2; color: #991b1b; } .badge-info { background: #dbeafe; color: #1e40af; } <td> <span class="badge badge-success">Active</span> </td> <td> <span class="badge badge-warning">Pending</span> </td> <td> <span class="badge badge-error">Inactive</span> </td>

Key Takeaways