akuagelas akuagelas - 1 month ago 9
CSS Question

How to make highlighted table with rowspan

I have a table with rowspan and here is my HTML code:

<table width="800" cellpadding="5" border="1">
<tr class="head">
<th>NO.</th>
<th>FOOD NAME</th>
<th>TYPE</th>
<th>STATUS</th>
</tr>
<tr class='row'>
<td rowspan='2' align='center'>1.</td>
<td rowspan='2'>Mozarella Cheese</td>
<td>Regular</td>
<td rowspan='2' align='center'>Available</td>
</tr>
<tr class='row'>
<td>Premium</td>
</tr>
<tr class='row'>
<td rowspan='2' align='center'>2.</td>
<td rowspan='2'>Greentea Milk</td>
<td>Regular</td>
<td rowspan='2' align='center'>Available</td>
</tr>
<tr class='row'>
<td>Premium</td>
</tr>
</table>


And here is my CSS code:

.head {
background: rgb(206,220,231); /* Old browsers */
background: -moz-linear-gradient(top, rgba(206,220,231,1) 0%, rgba(89,106,114,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(206,220,231,1)), color-stop(100%,rgba(89,106,114,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(206,220,231,1) 0%,rgba(89,106,114,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(206,220,231,1) 0%,rgba(89,106,114,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, rgba(206,220,231,1) 0%,rgba(89,106,114,1) 100%); /* IE10+ */
background: linear-gradient(to bottom, rgba(206,220,231,1) 0%,rgba(89,106,114,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#cedce7', endColorstr='#596a72',GradientType=0 ); /* IE6-9 */}

.head th {
padding:10px;
color:#333;
text-shadow:1px 1px 0px #CCC;
font-size:14px;
}

.row {
background-color:#E0E0E0;
font-size:12px;
}

.row:hover td[rowspan] {
background: #00FF33;
font-weight:bold;
cursor:pointer;
}
.row:hover td[rowspan]:hover ~ tr {
background: #00FF33;
font-weight:bold;
cursor:pointer;
}


I want each row of my table is highlighted but it didn't works well with my CSS code above. I suspect because I have a rowspan in my table so, it didn't works well. What can I do to fix my problem? How to make highlighted table with rowspan?

Answer

You could add a special hook class to the "Premium" cells, following your example. And then target them in the main .row:hover state.

Also, you don't need the td[rowspan] part.

Consider the following code:

.head {
   background: rgb(206,220,231); /* Old browsers */
   background: -moz-linear-gradient(top,  rgba(206,220,231,1) 0%, rgba(89,106,114,1) 100%); /* FF3.6+ */
   background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(206,220,231,1)), color-stop(100%,rgba(89,106,114,1))); /* Chrome,Safari4+ */
   background: -webkit-linear-gradient(top,  rgba(206,220,231,1) 0%,rgba(89,106,114,1) 100%); /* Chrome10+,Safari5.1+ */
   background: -o-linear-gradient(top,  rgba(206,220,231,1) 0%,rgba(89,106,114,1) 100%); /* Opera 11.10+ */
   background: -ms-linear-gradient(top,  rgba(206,220,231,1) 0%,rgba(89,106,114,1) 100%); /* IE10+ */
   background: linear-gradient(to bottom,  rgba(206,220,231,1) 0%,rgba(89,106,114,1) 100%); /* W3C */
   filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#cedce7', endColorstr='#596a72',GradientType=0 ); /* IE6-9 */}

.head th {
    padding:10px;
    color:#333;
    text-shadow:1px 1px 0px #CCC;
    font-size:14px;
}

.row {
    background-color:#E0E0E0;
    font-size:12px;
 }

.row:hover td, .row:hover + .premium td {
    background: #00FF33;
    font-weight:bold;
    cursor:pointer;
}
.row:hover td:hover ~ tr {
    background: #00FF33;
    font-weight:bold;
    cursor:pointer;
}
<table width="800" cellpadding="5" border="1">
   <tr class="head">
       <th>NO.</th>
       <th>FOOD NAME</th>
       <th>TYPE</th>
       <th>STATUS</th>
   </tr>
   <tr class='row'>
        <td rowspan='2' align='center'>1.</td>
        <td rowspan='2'>Mozarella Cheese</td>
        <td>Regular</td>
        <td rowspan='2' align='center'>Available</td>
   </tr>
   <tr class='row premium'>
        <td>Premium</td>
   </tr>
   <tr class='row'>
        <td rowspan='2' align='center'>2.</td>
        <td rowspan='2'>Greentea Milk</td>
        <td>Regular</td>
        <td rowspan='2' align='center'>Available</td>
   </tr>
   <tr class='row premium'>
        <td>Premium</td>
   </tr>
</table>

I would consider maybe creating a better markup structure as this is not easily maintainable code, but as you can see above it can definitely work.

Comments