HTML Table Styling

Table Styling Overview

This guide will show you how to style tables using both traditional HTML attributes (for quick basic styling) and modern CSS methods (for professional, responsive designs). You'll learn how to add borders, control spacing, add colors, and create visually appealing tables that enhance your data presentation.

Styling Approaches

  • HTML Attributes - Traditional method using attributes like border, cellpadding, and bgcolor
  • CSS - Modern approach with more control, flexibility, and maintainability
  • Responsive Design - Techniques to make tables work well on all device sizes

Traditional HTML Styling Attributes

Before CSS became widespread, HTML attributes were used to style tables. While many of these are now deprecated in favor of CSS, it's helpful to know them for maintaining older code or for quick prototyping.

html
1<!-- Basic table with attribute-based styling -->
2<table border="1" cellpadding="5" cellspacing="0" width="100%">
3  <tr>
4    <th bgcolor="#f2f2f2">Product</th>
5    <th bgcolor="#f2f2f2">Price</th>
6    <th bgcolor="#f2f2f2">In Stock</th>
7  </tr>
8  <tr>
9    <td>Widget A</td>
10    <td align="right">$12.99</td>
11    <td align="center">Yes</td>
12  </tr>
13  <tr bgcolor="#f9f9f9">
14    <td>Widget B</td>
15    <td align="right">$24.99</td>
16    <td align="center">No</td>
17  </tr>
18</table>

Common HTML Table Attributes

  • border - Sets the border width in pixels
  • cellpadding - Sets the space between cell content and cell borders
  • cellspacing - Sets the space between cells
  • width - Sets the table width (in pixels or percentage)
  • bgcolor - Sets the background color
  • align - Sets horizontal alignment (left, center, right)

Basic CSS Table Styling

CSS provides much more control, flexibility, and maintainability for styling tables. Let's explore how to achieve professional table designs with CSS.

html
1<style>
2  .basic-table {
3    width: 100%;
4    border-collapse: collapse;
5    font-family: Arial, sans-serif;
6    margin: 1em 0;
7  }
8  
9  .basic-table th, .basic-table td {
10    border: 1px solid #ddd;
11    padding: 8px;
12    text-align: left;
13  }
14  
15  .basic-table th {
16    background-color: #f2f2f2;
17    font-weight: bold;
18  }
19  
20  .basic-table tr:nth-child(even) {
21    background-color: #f9f9f9;
22  }
23  
24  .basic-table tr:hover {
25    background-color: #e6f7ff;
26  }
27</style>
28
29<table class="basic-table">
30  <tr>
31    <th>Product</th>
32    <th>Price</th>
33    <th>In Stock</th>
34  </tr>
35  <tr>
36    <td>Widget A</td>
37    <td>$12.99</td>
38    <td>Yes</td>
39  </tr>
40  <tr>
41    <td>Widget B</td>
42    <td>$24.99</td>
43    <td>No</td>
44  </tr>
45</table>

Key CSS Properties

  • border-collapse - Controls whether borders are collapsed into a single border or separated
  • padding - Sets space between cell content and cell borders
  • nth-child - Selects specific elements based on their position (e.g., for zebra striping)
  • :hover - Applies styles when the user hovers over an element

Advanced CSS Table Styling

For more professional tables, you can add enhanced styles with CSS.

html
1<style>
2  .advanced-table {
3    width: 100%;
4    border-collapse: separate;
5    border-spacing: 0;
6    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
7    box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
8    border-radius: 6px;
9    overflow: hidden;
10  }
11  
12  .advanced-table th, .advanced-table td {
13    padding: 12px 15px;
14    text-align: left;
15    border-bottom: 1px solid #e1e1e1;
16  }
17  
18  .advanced-table th {
19    background-color: #4CAF50;
20    color: white;
21    font-weight: 600;
22    text-transform: uppercase;
23    letter-spacing: 0.5px;
24  }
25  
26  .advanced-table tr:last-child td {
27    border-bottom: none;
28  }
29  
30  .advanced-table tr:nth-child(even) {
31    background-color: #f8f8f8;
32  }
33  
34  .advanced-table tr:hover {
35    background-color: #f1f1f1;
36    transition: background-color 0.2s ease;
37  }
38  
39  .price {
40    text-align: right;
41    font-family: monospace;
42    font-weight: bold;
43    color: #2c3e50;
44  }
45  
46  .in-stock {
47    color: #27ae60;
48    font-weight: bold;
49  }
50  
51  .out-of-stock {
52    color: #e74c3c;
53    font-weight: bold;
54  }
55</style>
56
57<table class="advanced-table">
58  <thead>
59    <tr>
60      <th>Product</th>
61      <th>Description</th>
62      <th>Price</th>
63      <th>Availability</th>
64    </tr>
65  </thead>
66  <tbody>
67    <tr>
68      <td>Widget A</td>
69      <td>Basic model with standard features</td>
70      <td class="price">$12.99</td>
71      <td class="in-stock">In Stock</td>
72    </tr>
73    <tr>
74      <td>Widget B</td>
75      <td>Premium model with advanced capabilities</td>
76      <td class="price">$24.99</td>
77      <td class="out-of-stock">Backordered</td>
78    </tr>
79  </tbody>
80</table>

Advanced Techniques

  • box-shadow - Adds shadow effects around the table
  • border-radius - Rounds the corners of the table
  • text-transform - Controls the capitalization of text
  • transition - Creates smooth animations for property changes

Responsive Tables for Mobile Devices

Making tables work well on mobile devices requires special techniques. Here's a responsive approach.

html
1<style>
2  .responsive-table {
3    width: 100%;
4    border-collapse: collapse;
5  }
6  
7  .responsive-table th, 
8  .responsive-table td {
9    padding: 12px 15px;
10    border: 1px solid #ddd;
11  }
12  
13  .responsive-table th {
14    background-color: #f2f2f2;
15    font-weight: bold;
16  }
17  
18  .table-container {
19    overflow-x: auto;
20    max-width: 100%;
21    margin: 1em 0;
22    box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);
23  }
24  
25  @media screen and (max-width: 600px) {
26    .responsive-table {
27      display: block;
28      width: 100%;
29    }
30    
31    .responsive-table thead {
32      display: none;
33    }
34    
35    .responsive-table tbody,
36    .responsive-table tr,
37    .responsive-table td {
38      display: block;
39      width: 100%;
40    }
41    
42    .responsive-table tr {
43      margin-bottom: 1em;
44      border: 1px solid #ddd;
45    }
46    
47    .responsive-table td {
48      text-align: right;
49      padding-left: 50%;
50      position: relative;
51      border: none;
52      border-bottom: 1px solid #eee;
53    }
54    
55    .responsive-table td::before {
56      content: attr(data-label);
57      position: absolute;
58      left: 15px;
59      width: 45%;
60      padding-right: 10px;
61      white-space: nowrap;
62      text-align: left;
63      font-weight: bold;
64    }
65  }
66</style>
67
68<div class="table-container">
69  <table class="responsive-table">
70    <thead>
71      <tr>
72        <th>Product</th>
73        <th>Description</th>
74        <th>Price</th>
75        <th>Status</th>
76      </tr>
77    </thead>
78    <tbody>
79      <tr>
80        <td data-label="Product">Widget A</td>
81        <td data-label="Description">Basic model with standard features</td>
82        <td data-label="Price">$12.99</td>
83        <td data-label="Status" class="in-stock">In Stock</td>
84      </tr>
85      <tr>
86        <td data-label="Product">Widget B</td>
87        <td data-label="Description">Premium model with advanced capabilities</td>
88        <td data-label="Price">$24.99</td>
89        <td data-label="Status" class="out-of-stock">Backordered</td>
90      </tr>
91    </tbody>
92  </table>
93</div>

Responsive Techniques

  • overflow-x - Enables horizontal scrolling for wide tables
  • Media Queries - Applies different styles based on screen size
  • Stacking - Converts table rows to stacked blocks on small screens
  • data-label - Provides labels for stacked table cells

Zebra Striping and Hover Effects

Zebra striping (alternating row colors) improves readability, and hover effects enhance user interaction.

html
1<style>
2  .zebra-table {
3    width: 100%;
4    border-collapse: collapse;
5    font-family: Arial, sans-serif;
6  }
7  
8  .zebra-table th, .zebra-table td {
9    padding: 12px 15px;
10    text-align: left;
11    border-bottom: 1px solid #ddd;
12  }
13  
14  .zebra-table th {
15    background-color: #4CAF50;
16    color: white;
17  }
18  
19  .zebra-table tbody tr:nth-child(odd) {
20    background-color: #f9f9f9;
21  }
22  
23  .zebra-table tbody tr:nth-child(even) {
24    background-color: #f2f2f2;
25  }
26  
27  .zebra-table tbody tr:hover {
28    background-color: #e6f7ff;
29    transition: background-color 0.2s ease;
30  }
31  
32  .highlight {
33    background-color: #fffacd !important;
34    font-weight: bold;
35  }
36</style>
37
38<table class="zebra-table">
39  <thead>
40    <tr>
41      <th>Product</th>
42      <th>Q1 Sales</th>
43      <th>Q2 Sales</th>
44      <th>Growth</th>
45    </tr>
46  </thead>
47  <tbody>
48    <tr>
49      <td>Widget A</td>
50      <td>$5,000</td>
51      <td>$6,200</td>
52      <td class="highlight">+24%</td>
53    </tr>
54    <tr>
55      <td>Widget B</td>
56      <td>$3,500</td>
57      <td>$4,100</td>
58      <td class="highlight">+17%</td>
59    </tr>
60    <tr>
61      <td>Widget C</td>
62      <td>$2,800</td>
63      <td>$2,500</td>
64      <td>-11%</td>
65    </tr>
66  </tbody>
67</table>

Visual Enhancement Techniques

  • nth-child - Selects elements based on their position for alternating row colors
  • :hover - Applies styles when the user hovers over table rows
  • transition - Creates smooth color transitions for hover effects
  • !important - Overrides other CSS rules (use sparingly)

Best Practices for Table Styling

Follow these guidelines to create effective and accessible table designs.

text
1/* Best practices include:
21. Use CSS instead of HTML attributes
32. Ensure readability with proper padding and contrast
43. Add visual hierarchy with colors and hover effects
54. Make tables responsive for all devices
65. Maintain accessibility with proper contrast and structure
76. Use consistent styling across all tables */

Key Guidelines

  • CSS over HTML - Use CSS for all styling in modern web development
  • Readability - Ensure text is legible with proper spacing and contrast
  • Visual Hierarchy - Use colors and styles to guide the user's attention
  • Responsiveness - Ensure tables work well on all screen sizes
  • Accessibility - Consider screen readers and users with visual impairments

Conclusion

While HTML attributes provide a quick way to add basic styling to tables, CSS offers far more powerful and flexible options for creating professional, responsive, and accessible tables. By combining semantic HTML structure with well-crafted CSS, you can transform basic data grids into visually appealing components that enhance the user experience.

html
1<!-- Example of a well-styled table -->
2<style>
3  .styled-table {
4    width: 100%;
5    border-collapse: collapse;
6    box-shadow: 0 2px 3px rgba(0,0,0,0.1);
7    border-radius: 4px;
8  }
9  
10  .styled-table th {
11    background-color: #4CAF50;
12    color: white;
13    padding: 12px 15px;
14    text-align: left;
15  }
16  
17  .styled-table td {
18    padding: 10px 15px;
19    border-bottom: 1px solid #ddd;
20  }
21  
22  .styled-table tr:hover {
23    background-color: #f5f5f5;
24  }
25</style>
26
27<table class="styled-table">
28  <tr>
29    <th>Product</th>
30    <th>Price</th>
31    <th>Availability</th>
32  </tr>
33  <tr>
34    <td>Widget A</td>
35    <td>$12.99</td>
36    <td>In Stock</td>
37  </tr>
38</table>

Frequently Asked Questions

Are HTML table styling attributes completely obsolete?

While deprecated in HTML5, these attributes still work in all browsers. However, for modern web development, you should use CSS instead as it provides better control, maintainability, and responsiveness.

How can I add rounded corners to my table?

Use the CSS border-radius property on the table element. Note that you may need to set border-collapse: separate; for it to work properly.

What's the best way to make tables responsive?

There are several approaches: 1) Horizontal scrolling (wrap table in a container with overflow-x: auto;), 2) Stackable layout (convert to block elements on small screens), 3) Hide less important columns on small screens, 4) Provide a summary or alternative data representation.

How can I style specific rows or cells differently?

Use CSS classes or structural pseudo-classes like: tr:nth-child(odd) or tr:nth-child(even) for zebra striping, tr:hover for hover effects, and custom classes for specific styling needs.

Can I use CSS frameworks like Bootstrap for table styling?

Yes, CSS frameworks like Bootstrap include pre-designed table styles that are responsive and well-tested. This can save development time while ensuring good design and compatibility.