Falytom logoFALYTOM
Games
ToolsUI ComponentsServicesProductsAbout
LoginSign Up
menu
  1. Home
  2. /Web Development
  3. /Html
  4. /Styling Tables
Your Progress0%
0 of 76 completed

Topics

Getting Started

  • What is HTML?
  • How the Web Works
  • Setting Up Your Environment
  • Your First HTML Page

HTML Document Structure

  • The DOCTYPE Declaration
  • The HTML Element
  • The Head Section
  • Meta Tags
  • The Body Element
  • HTML Comments

Text and Formatting

  • Headings (h1-h6)
  • Paragraphs
  • Line Breaks and Horizontal Rules
  • Text Formatting (Bold, Italic, etc.)
  • Quotations and Citations
  • Preformatted Text and Code
  • Subscript and Superscript

Links and Navigation

  • Creating Links
  • Absolute vs Relative URLs
  • Link Targets and Attributes
  • Email and Phone Links
  • Internal Page Links

Images and Media

  • Adding Images
  • Image Attributes
  • Image Formats
  • Figure and Figcaption
  • Responsive Images
  • Audio Elements
  • Video Elements
  • Embedding Content

Lists

  • Unordered Lists
  • Ordered Lists
  • Description Lists
  • Nested Lists

Tables

  • Table Basics
  • Table Headers and Footers
  • Spanning Columns and Rows
  • Table Captions
  • Styling Tables

Forms - Part 1 (Basics)

  • Introduction to Forms
  • Text Input Fields
  • Labels and Placeholders
  • Textarea Element
  • Checkboxes
  • Radio Buttons

Forms - Part 2 (Advanced)

  • Select Dropdowns
  • File Upload Inputs
  • HTML5 Input Types
  • Form Validation
  • Form Buttons
  • Fieldsets and Legends
  • Form Accessibility

Semantic HTML

  • What is Semantic HTML?
  • Header and Footer
  • Nav Element
  • Main and Article
  • Section and Aside
  • Div vs Semantic Elements
  • Time and Mark Elements

HTML5 Features

  • HTML5 Overview
  • Canvas Element
  • SVG in HTML
  • Geolocation API
  • Drag and Drop
  • Local Storage
  • Web Workers

Web Accessibility

  • Introduction to Accessibility
  • ARIA Attributes
  • Keyboard Navigation
  • Alt Text Best Practices
  • Color Contrast and Typography
  • Testing for Accessibility

Best Practices & SEO

  • HTML Best Practices
  • SEO Fundamentals
  • Meta Tags for SEO
  • Structured Data

Styling Tables

Best practices for table presentation

While HTML provides the structure for tables, CSS is what makes them visually appealing and easy to read. In this lesson, we'll explore best practices for styling tables to create professional, user-friendly data presentations.

Basic Table Styling

Without any CSS, HTML tables look plain with no borders. Let's start with fundamental styling that makes tables readable.

basic-styled-table.html
<style>
  table {
    width: 100%;
    border-collapse: collapse;
    font-family: Arial, sans-serif;
  }
  
  th, td {
    padding: 12px;
    text-align: left;
    border: 1px solid #ddd;
  }
  
  th {
    background-color: #4CAF50;
    color: white;
    font-weight: bold;
  }
</style>

<table>
  <thead>
    <tr>
      <th>Product</th>
      <th>Price</th>
      <th>Stock</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Laptop</td>
      <td>$999</td>
      <td>15</td>
    </tr>
    <tr>
      <td>Mouse</td>
      <td>$25</td>
      <td>150</td>
    </tr>
  </tbody>
</table>

Understanding border-collapse

The border-collapse property is crucial for table styling. It determines whether table borders are separated or merged.

border-collapse.css
/* Collapsed borders - cleaner look */
table {
  border-collapse: collapse;
}

/* Separated borders - distinct cells */
table {
  border-collapse: separate;
  border-spacing: 5px; /* Gap between cells */
}

Striped (Zebra) Rows

Alternating row colors improve readability, especially in tables with many rows. This pattern is called "zebra striping."

striped-table.html
<style>
  table {
    width: 100%;
    border-collapse: collapse;
  }
  
  th, td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid #ddd;
  }
  
  th {
    background-color: #2c3e50;
    color: white;
  }
  
  /* Stripe every other row */
  tbody tr:nth-child(even) {
    background-color: #f2f2f2;
  }
  
  /* Hover effect */
  tbody tr:hover {
    background-color: #e0e0e0;
  }
</style>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Email</th>
      <th>Department</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>John Doe</td>
      <td>john@example.com</td>
      <td>Sales</td>
    </tr>
    <tr>
      <td>Jane Smith</td>
      <td>jane@example.com</td>
      <td>Marketing</td>
    </tr>
    <tr>
      <td>Bob Johnson</td>
      <td>bob@example.com</td>
      <td>IT</td>
    </tr>
  </tbody>
</table>

Responsive Tables

Tables can be challenging on small screens. Here are strategies to make them mobile-friendly.

Horizontal Scroll Method

responsive-table.html
<style>
  .table-container {
    overflow-x: auto;
    margin: 20px 0;
  }
  
  table {
    width: 100%;
    min-width: 500px; /* Prevents table from getting too narrow */
    border-collapse: collapse;
  }
  
  th, td {
    padding: 10px;
    border: 1px solid #ddd;
    white-space: nowrap; /* Prevents text wrapping */
  }
</style>

<div class="table-container">
  <table>
    <thead>
      <tr>
        <th>Date</th>
        <th>Transaction</th>
        <th>Amount</th>
        <th>Status</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>2024-01-15</td>
        <td>Office Supplies</td>
        <td>$245.50</td>
        <td>Completed</td>
      </tr>
    </tbody>
  </table>
</div>

Stacked Layout for Mobile

stacked-mobile.css
/* Desktop view - normal table */
table {
  width: 100%;
  border-collapse: collapse;
}

/* Mobile view - stack cells */
@media screen and (max-width: 600px) {
  table, thead, tbody, th, td, tr {
    display: block;
  }
  
  thead tr {
    display: none; /* Hide header on mobile */
  }
  
  tr {
    margin-bottom: 15px;
    border: 1px solid #ddd;
  }
  
  td {
    text-align: right;
    padding-left: 50%;
    position: relative;
  }
  
  td::before {
    content: attr(data-label);
    position: absolute;
    left: 10px;
    font-weight: bold;
    text-align: left;
  }
}

Professional Table Styling

Here's a complete example with modern, professional styling that you can adapt for your projects.

professional-table.html
<style>
  .data-table {
    width: 100%;
    border-collapse: collapse;
    background-color: white;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    border-radius: 8px;
    overflow: hidden;
  }
  
  .data-table thead {
    background: linear-gradient(to right, #667eea, #764ba2);
  }
  
  .data-table th {
    padding: 16px;
    text-align: left;
    color: white;
    font-weight: 600;
    text-transform: uppercase;
    font-size: 14px;
    letter-spacing: 0.5px;
  }
  
  .data-table td {
    padding: 14px 16px;
    border-bottom: 1px solid #e5e7eb;
  }
  
  .data-table tbody tr:last-child td {
    border-bottom: none;
  }
  
  .data-table tbody tr:hover {
    background-color: #f9fafb;
    transition: background-color 0.2s ease;
  }
  
  /* Status badges */
  .status-badge {
    padding: 4px 12px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 600;
  }
  
  .status-active {
    background-color: #d1fae5;
    color: #065f46;
  }
  
  .status-pending {
    background-color: #fed7aa;
    color: #92400e;
  }
</style>

<table class="data-table">
  <thead>
    <tr>
      <th>Customer</th>
      <th>Order ID</th>
      <th>Amount</th>
      <th>Status</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Alice Cooper</td>
      <td>#12345</td>
      <td>$342.00</td>
      <td><span class="status-badge status-active">Active</span></td>
    </tr>
    <tr>
      <td>Bob Wilson</td>
      <td>#12346</td>
      <td>$198.50</td>
      <td><span class="status-badge status-pending">Pending</span></td>
    </tr>
  </tbody>
</table>

Accessibility Best Practices

Good table styling should enhance accessibility, not hinder it. Keep these principles in mind:

♿ Accessibility Principles:

  • Sufficient contrast: Ensure text has enough contrast against backgrounds (minimum 4.5:1 ratio for normal text).
  • Readable font sizes: Use at least 14-16px for body text.
  • Clear hover states: Help users track rows with visible hover effects.
  • Adequate padding: Give content breathing room with proper cell padding.
  • Avoid color-only indicators: Don't rely solely on color to convey information (use icons or text too).

Common Styling Mistakes to Avoid

avoid-these.css
/* DON'T: Too little padding - hard to read */
td {
  padding: 2px;
}

/* DO: Adequate padding */
td {
  padding: 12px 16px;
}

/* DON'T: Fixed pixel widths on mobile */
table {
  width: 800px;
}

/* DO: Flexible widths */
table {
  width: 100%;
  max-width: 800px;
}

/* DON'T: Overcomplicated borders */
table {
  border: 5px double red;
}

/* DO: Simple, clean borders */
table {
  border: 1px solid #ddd;
}

Practical Styling Tips

Align Numbers to the Right

For better readability, align numeric data to the right:

number-alignment.css
td.number {
  text-align: right;
  font-variant-numeric: tabular-nums; /* Equal-width numbers */
}

Sticky Table Headers

Keep headers visible when scrolling through long tables:

sticky-headers.css
thead th {
  position: sticky;
  top: 0;
  background-color: #2c3e50;
  z-index: 10;
}

Key Takeaways

  • Use border-collapse: collapse for cleaner tables
  • Add padding to cells for better readability (12-16px recommended)
  • Use zebra striping for tables with many rows
  • Add hover effects to help users track rows
  • Make tables responsive with scrolling or stacking techniques
  • Ensure sufficient color contrast for accessibility
  • Align numbers to the right for easier comparison
  • Keep styling simple and consistent

What's Next?

Congratulations! You've completed the Tables section. Next, we'll move into Forms, where you'll learn how to create interactive elements that collect user input. Forms are essential for any website that needs to gather information from visitors.

Know someone who'd find this guide helpful? Please share!

Previous
Table Captions
Next
Introduction to Forms

Never Miss a New HTML Tutorial

Join 1,000+ developers learning HTML step-by-step. Get new tutorials, tips, and exclusive resources delivered to your inbox - completely FREE.

You might also like

UI Components

Ready-made components for your projects

Developer Tools

Boost productivity with handy generators

HTML Game

Learn HTML concepts through interactive gameplay

Join Our Web Development Training Program

Master frontend & backend development with hands-on projects.

Menu

Topics

Getting Started

  • What is HTML?
  • How the Web Works
  • Setting Up Your Environment
  • Your First HTML Page

HTML Document Structure

  • The DOCTYPE Declaration
  • The HTML Element
  • The Head Section
  • Meta Tags
  • The Body Element
  • HTML Comments

Text and Formatting

  • Headings (h1-h6)
  • Paragraphs
  • Line Breaks and Horizontal Rules
  • Text Formatting (Bold, Italic, etc.)
  • Quotations and Citations
  • Preformatted Text and Code
  • Subscript and Superscript

Links and Navigation

  • Creating Links
  • Absolute vs Relative URLs
  • Link Targets and Attributes
  • Email and Phone Links
  • Internal Page Links

Images and Media

  • Adding Images
  • Image Attributes
  • Image Formats
  • Figure and Figcaption
  • Responsive Images
  • Audio Elements
  • Video Elements
  • Embedding Content

Lists

  • Unordered Lists
  • Ordered Lists
  • Description Lists
  • Nested Lists

Tables

  • Table Basics
  • Table Headers and Footers
  • Spanning Columns and Rows
  • Table Captions
  • Styling Tables

Forms - Part 1 (Basics)

  • Introduction to Forms
  • Text Input Fields
  • Labels and Placeholders
  • Textarea Element
  • Checkboxes
  • Radio Buttons

Forms - Part 2 (Advanced)

  • Select Dropdowns
  • File Upload Inputs
  • HTML5 Input Types
  • Form Validation
  • Form Buttons
  • Fieldsets and Legends
  • Form Accessibility

Semantic HTML

  • What is Semantic HTML?
  • Header and Footer
  • Nav Element
  • Main and Article
  • Section and Aside
  • Div vs Semantic Elements
  • Time and Mark Elements

HTML5 Features

  • HTML5 Overview
  • Canvas Element
  • SVG in HTML
  • Geolocation API
  • Drag and Drop
  • Local Storage
  • Web Workers

Web Accessibility

  • Introduction to Accessibility
  • ARIA Attributes
  • Keyboard Navigation
  • Alt Text Best Practices
  • Color Contrast and Typography
  • Testing for Accessibility

Best Practices & SEO

  • HTML Best Practices
  • SEO Fundamentals
  • Meta Tags for SEO
  • Structured Data
Falytom logoFALYTOM

Learn technologies like HTML, CSS, JavaScript, React, NodeJS and more through interactive games, tutorials, components and free tools. Transform your businesses with AI chatbots, SEO-optimized websites, and professional development services.

© 2025 Tomilola Group
  • Facebook logo
  • Twitter logo
  • Instagram logo
  • LinkedIn logo
  • GitHub logo
  • YouTube logo