CSS Properties

HTML Table Background Color

Sets the background color for an element. Commonly applied to headers, rows and footers.

Colors

Definition

The background-color property is used to set the background color of an element. It specifies a background color for the content, padding, and border areas of an element.

Possible Values

  • color: Specifies the color of the background. It can be a keyword (e.g., red), a hexadecimal value (e.g., #ff0000), an RGB value (e.g., rgb(255, 0, 0)), or any valid color representation.

Examples

HTML Table with Background Color

In this example, we can apply a hex color to the entire table.

CSS
table { background-color: #eceff1; /* Light gray background for table header cells */ }
Column 1 Column 2 Column 3
Row 1, Cell 1 Row 1, Cell 2 Row 1, Cell 3
Row 2, Cell 1 Row 2, Cell 2 Row 2, Cell 3
Row 3, Cell 1 Row 3, Cell 2 Row 3, Cell 3
Row 4, Cell 1 Row 4, Cell 2 Row 4, Cell 3
Note: Adding background-color to the entire table may not work as expected if you're also using background-color with other table elements, such as <td> or <th>. You may notice that the background color for these elements take precedence where applicable.

HTML Table Header Background Color

More often than not, you'll want to make the header of your table stand out. This can be achieved by applying background-color the <th> elements.

CSS
th { background-color: #eceff1; /* Light gray background for table header cells */ }
Column 1 Column 2 Column 3
Row 1, Cell 1 Row 1, Cell 2 Row 1, Cell 3
Row 2, Cell 1 Row 2, Cell 2 Row 2, Cell 3
Row 3, Cell 1 Row 3, Cell 2 Row 3, Cell 3
Row 4, Cell 1 Row 4, Cell 2 Row 4, Cell 3

HTML Table Cell Background Color

The background-color property can be applied to individual table cells (<td> or <th>) to highlight specific data points. This is particularly useful for emphasizing key information or creating a visual hierarchy within the table.

CSS
td { background-color: #eceff1; /* Light gray background for table cells */ }
Column 1 Column 2 Column 3
Row 1, Cell 1 Row 1, Cell 2 Row 1, Cell 3
Row 2, Cell 1 Row 2, Cell 2 Row 2, Cell 3
Row 3, Cell 1 Row 3, Cell 2 Row 3, Cell 3
Row 4, Cell 1 Row 4, Cell 2 Row 4, Cell 3

HTML Table Row Background Color (Even / Odd)

To improve readability, alternating row colors are often employed in tables. The nth-child pseudo-class can be used in conjunction with the background-color property to achieve this effect.

CSS
tr:nth-child(even) { background-color: #f9f9f9; /* Light background color for even rows */ } tr:nth-child(odd) { background-color: #ffffff; /* White background color for odd rows */ }
Column 1 Column 2 Column 3
Row 1, Cell 1 Row 1, Cell 2 Row 1, Cell 3
Row 2, Cell 1 Row 2, Cell 2 Row 2, Cell 3
Row 3, Cell 1 Row 3, Cell 2 Row 3, Cell 3
Row 4, Cell 1 Row 4, Cell 2 Row 4, Cell 3

Hover Effects

Applying a different background color on hover can provide a visual indication to users that a particular row or cell is interactive.

CSS
tr:hover { background-color: #e6f7ff; /* Light blue background color on hover */ }
Column 1 Column 2 Column 3
Row 1, Cell 1 Row 1, Cell 2 Row 1, Cell 3
Row 2, Cell 1 Row 2, Cell 2 Row 2, Cell 3
Row 3, Cell 1 Row 3, Cell 2 Row 3, Cell 3
Row 4, Cell 1 Row 4, Cell 2 Row 4, Cell 3

Transparency and Opacity

The background-color property can also accept RGBA or HSLA values, allowing you to set a background color with varying levels of transparency. This can be useful for creating subtle overlays or effects while maintaining visibility of underlying content.

CSS
td { background-color: rgba(255, 0, 0, 0.3); /* Semi-transparent red background for table cells */ }
Column 1 Column 2 Column 3
Row 1, Cell 1 Row 1, Cell 2 Row 1, Cell 3
Row 2, Cell 1 Row 2, Cell 2 Row 2, Cell 3
Row 3, Cell 1 Row 3, Cell 2 Row 3, Cell 3
Row 4, Cell 1 Row 4, Cell 2 Row 4, Cell 3

Gradient Backgrounds

For more intricate designs, consider using gradient backgrounds. CSS gradients allow you to blend multiple colors smoothly, adding depth and visual interest to table elements.

CSS
td { background-image: linear-gradient(to right, #ffcc00, #ff6666); /* Gradient background for table cells */ }
Column 1 Column 2 Column 3
Row 1, Cell 1 Row 1, Cell 2 Row 1, Cell 3
Row 2, Cell 1 Row 2, Cell 2 Row 2, Cell 3
Row 3, Cell 1 Row 3, Cell 2 Row 3, Cell 3
Row 4, Cell 1 Row 4, Cell 2 Row 4, Cell 3

CSS Variables

Leverage CSS variables to maintain consistency and ease of maintenance in your stylesheets. Define background colors as variables, making it simpler to update the color scheme across your HTML tables.

CSS
:root { --primary-bg-color: #e0e0e0; } td { background-color: var(--primary-bg-color); /* Use the variable for background color */ }
Column 1 Column 2 Column 3
Row 1, Cell 1 Row 1, Cell 2 Row 1, Cell 3
Row 2, Cell 1 Row 2, Cell 2 Row 2, Cell 3
Row 3, Cell 1 Row 3, Cell 2 Row 3, Cell 3
Row 4, Cell 1 Row 4, Cell 2 Row 4, Cell 3

Using background-color with other CSS Properties

Experiment with combining the background-color property with other styling properties, such as border, box-shadow, or font, to create comprehensive and visually appealing table designs.

Accessibility Considerations

Contrast Ratio

Ensure that the chosen background color maintains an adequate contrast ratio with the text color for accessibility. This is essential for users with visual impairments.