CSS Basics

CSS Tables

Style HTML tables with modern designs, responsive layouts, and interactive features. Create data tables, pricing tables, and comparison charts.

How do you style an HTML table with CSS?

Use border-collapse: collapse on the table so cells share single borders, add padding to th and td, and stripe rows with tr:nth-child(even). On small screens, wrap the table in a container with overflow-x: auto so it scrolls horizontally instead of breaking the layout.

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .75rem 1rem; border-bottom: 1px solid #e5e7eb; text-align: left; }
tbody tr:nth-child(even) { background: #f9fafb; }
thead th { position: sticky; top: 0; background: white; }

Table Styling Demo

Code Editor

<div class="wrap">
<table>
  <thead><tr><th>Plan</th><th>Price</th><th>Projects</th></tr></thead>
  <tbody>
    <tr><td>Free</td><td class="num">$0</td><td class="num">3</td></tr>
    <tr><td>Pro</td><td class="num">$12</td><td class="num">50</td></tr>
    <tr><td>Team</td><td class="num">$49</td><td class="num">Unlimited</td></tr>
  </tbody>
</table>
</div>

Preview

Understanding Table Styling

CSS table styling transforms basic HTML tables into professional data presentations. Modern table design focuses on readability, responsiveness, and user interaction while maintaining accessibility and performance across all devices.

Core Properties

  • border-collapse - Border merging
  • border-spacing - Cell spacing
  • table-layout - Layout algorithm
  • caption-side - Caption position

Modern Techniques

  • Responsive stacking patterns
  • CSS Grid table alternatives
  • Sticky headers and columns
  • Interactive sorting and filtering

Table Structure & Layout

Border Models

Collapse vs Separate

  • collapse - Merges adjacent borders
  • separate - Maintains cell spacing

Best Practice

table {border-collapse: collapse;}

Table Layout Algorithms

Layout Types

  • auto - Content-based width (default)
  • fixed - Fixed column widths

Performance

Fixed layout renders faster for large tables

Responsive Table Design

Mobile Strategies

Horizontal Scroll

Wrap tables in scrollable containers for wide data

Stacked Layout

Convert rows to card-style blocks on mobile

CSS Grid Alternative

Use CSS Grid for complex responsive layouts

Best Practices & Accessibility

Do's

  • • Use proper table markup (thead, tbody, tfoot)
  • • Provide clear column headers
  • • Implement keyboard navigation
  • • Add scope attributes for complex tables
  • • Use caption elements for table descriptions
  • • Test with screen readers

Don'ts

  • • Don't use tables for layout purposes
  • • Avoid removing table semantics
  • • Don't rely solely on color for data meaning
  • • Avoid overly complex nested table structures
  • • Don't ignore mobile usability
  • • Avoid tables for non-tabular data

Performance & Modern Features

Large Tables

  • • Use table-layout: fixed for better performance
  • • Implement virtual scrolling for 1000+ rows
  • • Consider pagination for large datasets

Modern CSS

  • • Use CSS custom properties for theming
  • • Implement container queries when supported
  • • Leverage sticky positioning for headers

CSS Tables — step by step

1 / 4
1

border-collapse

Use border-collapse: collapse to merge adjacent cell borders into a single line. The default separate keeps individual borders with gaps between cells.

1 of 4
💡Wrap Tables for Mobile Scrolling
Tables do not naturally shrink on small screens. Wrapping your table in a div with overflow-x: auto is the simplest responsive fix — the table keeps its full width but becomes horizontally scrollable.

🎯 Quick Check

Which CSS property merges the borders between adjacent table cells?

Styling Tables with CSS: summary

Key points

  • table-layout: fixed makes column widths follow the first row, which is faster and more predictable.
  • Use tables for tabular data only, never for page layout.
  • Align numbers to the right and add font-variant-numeric: tabular-nums so digits line up.

Frequently asked questions

›How do I make a table responsive?

The simplest reliable method is to wrap it in a div with overflow-x: auto. For very narrow screens you can restyle rows as stacked cards with media queries.

›What does border-collapse do?

border-collapse: collapse merges adjacent cell borders into one, removing the double borders and gaps of the default separate mode.