Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table tr background gradient on hover using CSS

My requirement:

On tr hover, trwill have the linear gradient background, not only a flat background color.

Sample td in tr:

A  |  B  |  C  |  B  |  A

Where A darker --> C lighter

JSFIDDLE:

https://jsfiddle.net/g7o84j43/

Code I've tried:

HTML:

<table border="1px" width="100%" cellpadding="0px" cellspacing="0px">
<tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
</tr>
<tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
</tr>
</table>

CSS:

tr:hover
{
    background: -webkit-linear-gradient(left, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Safari 5.1 to 6.0 */
    background: -o-linear-gradient(right, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(right, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Firefox 3.6 to 15 */
    background: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1)); /* Standard syntax (must be last) */
}

Any ideas how to achieve this?

like image 311
Nere Avatar asked Nov 03 '15 02:11

Nere


2 Answers

The simplest and most straight-forward solution to setting a gradient background for tr on hover is to add background-attachment: fixed also to it.

tr:hover {
  background: linear-gradient(to right, rgba(255, 0, 0, 1), rgba(255, 0, 0, 0), rgba(255, 0, 0, 1));
  background-attachment: fixed;
}
<!-- prefix library included only to avoid vendor prefixes -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<table border="1px" width="100%" cellpadding="0px" cellspacing="0px">
  <tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
  </tr>
  <tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
  </tr>
</table>

Here is a different approach for adding a gradient background to the row (tr) while hovering over it. In this approach, we add the gradient as a background to the table element itself and then override it for the unhovered state to a solid color (white). When the tr is hovered, the background of the tr is set to transparent and so the table background shows through revealing the gradient.

table {
  background: linear-gradient(to right, rgba(255, 0, 0, 0), rgba(255, 0, 0, 1));
}
tr {
  background: white;
}
tr:hover {
  background: transparent;
}
<!-- prefix library included only to avoid vendor prefixes -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<table border="1px" width="100%" cellpadding="0px" cellspacing="0px">
  <tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
  </tr>
  <tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
  </tr>
</table>

If the lighter color needs to be at the center then the gradient can be modified like in the below snippet:

table {
  background: linear-gradient(to right, rgba(255, 0, 0, 1), rgba(255, 0, 0, 0), rgba(255, 0, 0, 1));
}
tr {
  background: white;
}
tr:hover {
  background: transparent;
}
<!-- prefix library included only to avoid vendor prefixes -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<table border="1px" width="100%" cellpadding="0px" cellspacing="0px">
  <tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
  </tr>
  <tr style="text-align:center">
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>B</td>
    <td>A</td>
  </tr>
</table>
like image 163
Harry Avatar answered Nov 14 '22 23:11

Harry


Maybe not the best solution, but maybe good enough for you, and it's working in Chrome too. DEMO

HTML:

<div class="table-wrapper">
    <table>
        ...
    </table>
</div>

CSS:

/* To hide the overflow */
.table-wrapper{
    position: relative;
    overflow: hidden;
}

tr{
    position: relative;
}

/* Making the background with :before pseudo-element */

tr:before
{
    position: absolute;
    content: "";
    width: 100%;
    height: 20px;
    z-index: -1;
}

tr:hover:before
{
    background: -webkit-linear-gradient(left, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Safari 5.1 to 6.0 */
    background: -o-linear-gradient(right, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(right, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Firefox 3.6 to 15 */
    background: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1)); /* Standard syntax (must be last) */
}
like image 31
lmgonzalves Avatar answered Nov 15 '22 00:11

lmgonzalves