Resolving CSS Background Color Not Spanning Entire Table Row

Dec 04, 2025 · Programming · 7 views · 7.8

Keywords: HTML | CSS | background-color

Abstract: This article explains how to use the CSS property border-collapse to ensure that background colors on table rows cover the entire row without white borders between cells, with code examples and analysis.

Problem Description

In web development, a common issue arises when attempting to apply a background color to a table row (<tr> tag) in HTML. Despite setting the background-color property, users often observe white borders or gaps around each cell (<td> or <th>), preventing the color from spanning the entire row. This problem stems from the default CSS table rendering model.

Cause Analysis

By default, CSS tables use the border-collapse: separate; property, which means that each cell has its own independent borders. This separation creates gaps between cells, where the background color of the row does not extend. Even if margins, borders, and paddings are set to zero, as in the provided CSS excerpt, the inherent table structure still leaves these spaces.

Solution

The most effective solution is to use the border-collapse: collapse; property on the table element. This CSS property merges the borders of adjacent cells, eliminating the gaps and allowing the row's background color to cover the entire area seamlessly.

Code Example

Here is a corrected CSS snippet based on the accepted answer:

table {
    border-collapse: collapse;
}
tr.rowhighlight {
    background-color: #f0f8ff;
}

With this change, the HTML table row will display the background color without interruptions.

Additional Considerations

While border-collapse: collapse; is the primary fix, other CSS properties like border-spacing can also affect table layout. However, for this specific issue, collapsing borders is sufficient. Ensure that other styles, such as margins and paddings, are appropriately set to avoid unintended spacing.

Conclusion

To make background colors span entire table rows in CSS, always set border-collapse: collapse; on the table element. This simple adjustment ensures consistent rendering across browsers and eliminates common visual artifacts like white borders between cells.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.