简体   繁体   中英

Column and row highlight on hover in striped table in CSS

I'm using trick for CSS only highlight of table column on hover from https://css-tricks.com/simple-css-row-column-highlighting/

It's work perfect, but not for striped table via

tr:nth-of-type(odd) {
    background-color: #f0f0f0;
}

Highlighting not apply to upper and bottom cells in rows with background.

See example here: http://jsfiddle.net/615avo4v/

Please, help me fix this issue without JavaScript.

Thanks in advance!

Change the way you set the stripped background.

Use another pseudo element, this time on the first td of the even rows, and aligned horizontally. (and with a lower z-index).

 * { margin: 0; padding: 0; } table { width: 100%; border-spacing: 0; color: #212121; text-align: left; overflow: hidden; } table>tbody>tr>td { padding: 10px; font-size: 14px; position: relative; } table>tbody>tr:hover { padding: 20px; background-color: #ffa !important; } tr:nth-child(even) td:first-child::before { content: ""; position: absolute; background-color: lightgreen; top: 0; left: -5000px; width: 10000px; height: 100%; z-index: -10; } td:hover::after { content: ""; position: absolute; background-color: #ffa; left: 0; top: -5000px; height: 10000px; width: 100%; z-index: -1; } 
  <table> <tr> <td>col1</td> <td>col2</td> <td>col3</td> <td>col4</td> <td>col5</td> <td>col6</td> <td>col7</td> <td>col8</td> </tr> <tr> <td>col1</td> <td>col2</td> <td>col3</td> <td>col4</td> <td>col5</td> <td>col6</td> <td>col7</td> <td>col8</td> </tr> <tr> <td>col1</td> <td>col2</td> <td>col3</td> <td>col4</td> <td>col5</td> <td>col6</td> <td>col7</td> <td>col8</td> </tr> <tr> <td>col1</td> <td>col2</td> <td>col3</td> <td>col4</td> <td>col5</td> <td>col6</td> <td>col7</td> <td>col8</td> </tr> <tr> <td>col1</td> <td>col2</td> <td>col3</td> <td>col4</td> <td>col5</td> <td>col6</td> <td>col7</td> <td>col8</td> </tr> <tr> <td>col1</td> <td>col2</td> <td>col3</td> <td>col4</td> <td>col5</td> <td>col6</td> <td>col7</td> <td>col8</td> </tr> </table> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM