Table Styling

Table Styling

Transform plain tables into beautiful, modern designs. Learn CSS techniques for styling tables with colors, borders, hover effects, and responsive layouts.

How do you make an HTML table look good?

Use CSS: border-collapse: collapse on the table, padding on the cells, a light border between rows, a distinct header background, and zebra stripes with tbody tr:nth-child(even). Wrap wide tables in a container with overflow-x: auto so they scroll on phones instead of breaking the layout.

<div class="table-wrap"><table>…</table></div>

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

Why Style Tables?

Table styling transforms basic HTML tables into visually appealing, professional data presentations. Good table design improves readability, user experience, and data comprehension.

Styling Techniques:

  • • Color schemes and gradients
  • • Border and spacing control
  • • Hover and focus effects
  • • Typography and alignment
  • • Progress bars and badges
  • • Responsive design patterns

Visual Effects:

  • • Smooth transitions
  • • Shadow and depth effects
  • • Interactive animations
  • • Theme variations
  • • Status indicators
  • • Loading animations

Try It Yourself

Create a beautifully styled table with modern CSS techniques:

Code Editor

<table>
  <caption>Top languages</caption>
  <thead><tr><th>Language</th><th>Year</th><th>Typed</th></tr></thead>
  <tbody>
    <tr><td>JavaScript</td><td>1995</td><td>Dynamic</td></tr>
    <tr><td>TypeScript</td><td>2012</td><td>Static</td></tr>
    <tr><td>Python</td><td>1991</td><td>Dynamic</td></tr>
  </tbody>
</table>
<style>
  table { border-collapse: collapse; width: 100%; font: 14px sans-serif; }
  th, td { padding: 8px 12px; text-align: left; border-bottom: 1px solid #e5e7eb; }
  thead th { background: #1e293b; color: white; }
  tbody tr:nth-child(even) { background: #f8fafc; }
  tbody tr:hover { background: #eff6ff; }
  caption { text-align: left; font-weight: bold; padding-bottom: 6px; }
</style>

Preview

What to Notice:

  • • Gradient header background for visual appeal
  • • Striped rows improve readability
  • • Hover effects provide interactivity
  • • Status badges add semantic meaning
  • • Box shadows create depth and elevation

Essential CSS Properties

Border Collapse

Collapsed Borders:

table { border-collapse: collapse; /* Merges adjacent borders */ } th, td { border: 1px solid #ddd; padding: 12px; }

Separated Borders:

table { border-collapse: separate; border-spacing: 5px; /* Adds space between cells */ }

Background Colors & Patterns

Striped Rows:

/* Even rows */ tbody tr:nth-child(even) { background-color: #f8f9fa; } /* Odd rows */ tbody tr:nth-child(odd) { background-color: white; }

Gradient Headers:

th { background: linear-gradient( 135deg, #667eea 0%, #764ba2 100% ); color: white; }

Interactive Hover Effects

/* Row hover effects */ tbody tr:hover { background-color: #e3f2fd; transform: translateY(-2px); transition: all 0.3s ease; box-shadow: 0 4px 8px rgba(0,0,0,0.1); cursor: pointer; } /* Cell hover effects */ td:hover { background-color: #fff3e0; }

Common Styling Patterns

Modern Clean Style:

  • • Minimal borders or borderless
  • • Subtle shadows for depth
  • • Clean typography
  • • Generous white space
  • • Soft hover transitions

Material Design:

  • • Elevated cards with shadows
  • • Rounded corners
  • • Material color palette
  • • Ripple click effects
  • • Floating action buttons

Dark Theme:

  • • Dark background colors
  • • Light text for contrast
  • • Subtle borders and dividers
  • • Glowing accent colors
  • • Reduced eye strain

Data Dashboard:

  • • Status indicators and badges
  • • Progress bars
  • • Color-coded data
  • • Interactive sorting
  • • Real-time updates

Responsive Table Styling

Make your styled tables work beautifully on all devices:

Mobile-First Approach:

@media (max-width: 768px) { table thead { display: none; } table tr { display: block; border: 1px solid #ddd; margin-bottom: 10px; border-radius: 8px; padding: 10px; } table td { display: block; text-align: right; border: none; padding: 8px 0; } table td:before { content: attr(data-label) ": "; float: left; font-weight: bold; } }

Scrollable Tables:

.table-wrapper { overflow-x: auto; border-radius: 8px; box-shadow: 0 0 20px rgba(0,0,0,0.1); } table { min-width: 600px; /* Ensures table doesn't get too narrow */ } /* Scroll indicator */ .table-wrapper::-webkit-scrollbar { height: 8px; } .table-wrapper::-webkit-scrollbar-track { background: #f1f1f1; } .table-wrapper::-webkit-scrollbar-thumb { background: #888; border-radius: 4px; }

Styling Best Practices

Do's:

  • • Maintain consistent spacing
  • • Use appropriate color contrast
  • • Ensure readable typography
  • • Implement smooth transitions
  • • Test on different devices
  • • Consider accessibility needs

Common Mistakes:

  • • Overcomplicated designs
  • • Poor mobile experience
  • • Insufficient color contrast
  • • Missing hover states
  • • Inconsistent styling
  • • Ignoring loading states

Pro Tips:

  • • Use CSS custom properties for theme consistency
  • • Implement focus states for keyboard navigation
  • • Consider print styles for tables
  • • Add loading skeletons for data tables
  • • Use CSS Grid for complex table layouts
  • • Test with screen readers

Quick Reference

Essential Properties:

/* Basic table styling */ table { border-collapse: collapse; width: 100%; margin: 20px 0; } th, td { padding: 12px 15px; text-align: left; border-bottom: 1px solid #ddd; } /* Hover effects */ tr:hover { background-color: #f5f5f5; }

Common Selectors:

/* Stripe pattern */ tr:nth-child(even) { } tr:nth-child(odd) { } /* First/last elements */ th:first-child { } td:last-child { } /* Header styling */ thead th { } tbody td { }

Table Styling — step by step

1 / 4
1

Remove default browser table styles

Add border-collapse: collapse to the table element to merge cell borders into single lines. Set border-spacing: 0 to remove gaps between cells.

1 of 4
💡caption Element for Table Titles
Add a caption element as the first child of table to give the table an accessible title. Screen readers announce this caption before the table data. It is also useful for sighted users to understand the table's purpose at a glance.

🎯 Quick Check

Which CSS property merges the borders between adjacent table cells into a single line?

Styling HTML Tables: summary

Key points

  • Right-align numeric columns and use font-variant-numeric: tabular-nums so digits line up.
  • A sticky header (position: sticky; top: 0) keeps column names visible in long tables.
  • Avoid deprecated attributes like bgcolor, border, cellpadding and align.

Frequently asked questions

›How do I add zebra stripes to a table?

Use tbody tr:nth-child(even) { background: #f9fafb; }.

›How do I make a table responsive?

Wrap it in an element with overflow-x: auto. On very small screens you can also restyle each row as a stacked card with media queries.