27

How can I change the background column of an html table column when the mouse is over it?

Preferably with css only.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
flybywire
  • 261,858
  • 191
  • 397
  • 503

8 Answers8

54

This can be done using CSS with no Javascript.

I used the ::after pseudo-element to do the highlighting. z-index keeps the highlighting below the <tds> in case you need to handle click events. Using a massive height allows it to cover the whole column. overflow: hidden on the <table> hides the highlight overflow.

Demo: http://jsfiddle.net/ThinkingStiff/2XeYe/

Output:

enter image description here

CSS:

table {
    border-spacing: 0;
    border-collapse: collapse;
    overflow: hidden;
    z-index: 1;
}

td, th {
    cursor: pointer;
    padding: 10px;
    position: relative;
}

td:hover::after { 
    background-color: #ffa;
    content: '\00a0';  
    height: 10000px;    
    left: 0;
    position: absolute;  
    top: -5000px;
    width: 100%;
    z-index: -1;        
}

HTML:

<table>
    <tr>
        <th></th><th>50kg</th><th>55kg</th><th>60kg</th><th>65kg</th><th>70kg</th>
    </tr>
    <tr>
        <th>160cm</th><td>20</td><td>21</td><td>23</td><td>25</td><td>27</td>
    </tr>
    <tr>
        <th>165cm</th><td>18</td><td>20</td><td>22</td><td>24</td><td>26</td>
    </tr>
    <tr>
        <th>170cm</th><td>17</td><td>19</td><td>21</td><td>23</td><td>25</td>
    </tr>
    <tr>
        <th>175cm</th><td>16</td><td>18</td><td>20</td><td>22</td><td>24</td>
    </tr>
</table>
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
18

I have a more simple solution (Live example: http://jsfiddle.net/q3HHt/1/)

HTML:

<table>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
  </tr>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
  </tr>
</table>

CSS:

table, td {
    border: 1px solid black;
}

td {
    width: 40px;  
    height: 40px;
}

.highlighted {
    background-color: #348A75;
}

jQuery:

$('td').hover(function() {
    var t = parseInt($(this).index()) + 1;
    $('td:nth-child(' + t + ')').addClass('highlighted');
},
function() {
    var t = parseInt($(this).index()) + 1;
    $('td:nth-child(' + t + ')').removeClass('highlighted');
});

Live example: http://jsfiddle.net/q3HHt/1/

CoderPi
  • 12,985
  • 4
  • 34
  • 62
M. Ahmad Zafar
  • 4,881
  • 4
  • 32
  • 44
  • 1
    Use `$('td:nth-child(' + t + ')', $(this).closest('table')).addClass('highlighted');` to prevent all the tables on the page from being highlighted if you have multiple. – sushain97 Aug 25 '13 at 00:58
  • Yes correct but a better way would be to use `id` attribute – M. Ahmad Zafar Aug 26 '13 at 05:19
  • Wouldn't that require individual listeners for each table? I just went for the simple route without editing my HTML and thought it may be useful for anyone finding this answer via Google (as I did). – sushain97 Aug 26 '13 at 21:04
  • 1
    $('td').hover(function() { $('table td:nth-child('+ ($(this).index()+1) +')').toggleClass('highlighted'); }); – arsh Jun 25 '16 at 10:16
  • combine with this syntax for dynamic content hover: https://stackoverflow.com/a/14056759/5924640 – Exceptional NullPointer Aug 02 '19 at 14:03
8

Only works for cells or rows, sorry. e.g.

td {
  background-color: blue;
}

td:hover {
  background-color: red;
}

There are JavaScript solutions available but nothing in CSS right now will do what you want because of the limitations of selectors.

td  /* all cells */
{ 
  background-color: blue;
}

tr /* all rows */
{
  background-color: pink;
}

/* nothing for all columns */
CoderPi
  • 12,985
  • 4
  • 34
  • 62
Jonathan Fingland
  • 56,385
  • 11
  • 85
  • 79
  • 1
    Actually you can style columns using either the sibling selector (gets a little messy) or CSS3's `nth-child` (no IE support). You still can't do it on hover though, because you're only ever hovering on a cell or row, not a column. – DisgruntledGoat Oct 12 '09 at 16:10
6

Just to extends Muhammads answer (https://stackoverflow.com/a/11828637/1316280), if you want to highlight the cols only in the actual table, change the jquery-code-part to: this jsfiddle is specific for only the actual table

jQuery

$('td').hover(function() {
    var t = parseInt($(this).index()) + 1;
    $(this).parents('table').find('td:nth-child(' + t + ')').addClass('highlighted');
},
function() {
    var t = parseInt($(this).index()) + 1;
    $(this).parents('table').find('td:nth-child(' + t + ')').removeClass('highlighted');
});

jsFiddle: http://jsfiddle.net/q3HHt/123/

Community
  • 1
  • 1
Robert Kubiak
  • 156
  • 1
  • 5
2

I do not think there is a clean HTML + CSS way to do this. Javascript is an alternative, for example the jQuery tableHover plugin

wiifm
  • 3,787
  • 1
  • 21
  • 23
2

I had a similar problem where I had too many columns to display on screen. VIA PHP, I turned each row into a 1 x column table. So, n rows = n tables. I then nested each table within a master table. Doing so allowed me to call td:hover from my stylesheet. Since each td held a table, it has the same effect of highlighting the a column when I mouse over it.

-2

You can try experimenting with <col> tag and col:hover { background: red; } style, but I doubt that it will work. Anyway, this definitely won't work in older versions of MSIE, so you will need javascript in order to do this.

n1313
  • 20,555
  • 7
  • 31
  • 46
-4

You can highlight the whole row with pure CSS using:

tr td {background-color: red;}
tr:hover td {background-color: blue;}

Achieving this effect for a column is impossible with this approach, as cell (td) is a child of a row (tr), not a column.

To make it work in IE7+, make sure to add doctype declaration (what you should always do anyway:)).

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
Johnny
  • 11