1. Technology
Send to a Friend via Email

How do you change the background color of a table?


Mature man working on computer at home
StA-gur Karlsson/E+/Getty Images
Question: How do you change the background color of a table?

The attribute bgcolor will change the background color of the table, the current table row, or the current table cell. But the bgcolor attribute is deprecated in favor of style sheets. The better way to change the background color is to add the style property background-color to the table, row, or cell tag.

<table style="background-color: #ff0000;">
<tr style="background-color: yellow;">
<td style="background-color: #000;">

Or you can set the styles in a style sheet in the head of your document or an external style sheet.

table { background-color: #ff0000; }
tr { background-color: yellow; }
td { background-color: #000; }

The best way to set the background color on a column is to create a style class and then assign that class to the cells in that column.

The CSS:

td.blueCol { background-color: blue; }


<tr><td class="blueCol">cell 1</td><td>cell 2</td></tr>
<tr><td class="blueCol">cell 1</td><td>cell 2</td></tr>


©2014 About.com. All rights reserved.