Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change background color on rows, but not row headers

Tags:

css

html-table

I'm trying to change the background color of a table's rows using CSS, but I do not want to change a table heading's background. However, by default, a TH is wrapped in a TR, so the TR rule is pushed to my TH.

Here's a jsfiddle to test with, if it helps: http://jsfiddle.net/jomanlk/Bcayc/

like image 881
Jeff Lamb Avatar asked Mar 05 '12 19:03

Jeff Lamb


People also ask

How do I change the background color of a row in a table?

The HTML <tr> bgcolor Attribute is used to specify the background color of a table row. It is not supported by HTML 5. Attribute Values: color_name: It sets the background color by using the color name.

How do you add a background color to a row?

Changing the Background Color of a Cell To change the color of a row, you can add the “style” property into the <td> brackets and define the color of the cell from there.

How do I change the background color of a specific row in HTML?

Cell background colors are set by applying the bgcolor attribute to a <tr> tag (to color the row) or to a <td> tag (to color the cell).


1 Answers

You can add <thead> and <tbody> to differenciate your header rows from the data rows. This way you can target only the desired ones:

table tbody td:hover{
    background: #f00;
}

table tbody tr:hover{
    background: #00f;
}​

DEMO

Along with the following markup:

<table>
    <thead>
        <tr>
            <th>Col 1</th>
            <th>Col 2</th>
            <th>Col 3</th>
            <th>Col 4</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Col 1</td>
            <td>Col 2</td>
            <td>Col 3</td>
            <td>Col 4</td>
        </tr>
    </tbody>
</table>​
like image 80
Didier Ghys Avatar answered Nov 07 '22 08:11

Didier Ghys