如何使用rowspan创建突出显示的表

时间:2016-03-03 08:56:36

标签: css html-table highlight

我有一个包含rowspan的表,这是我的HTML代码:

<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>

这是我的CSS代码:

.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;
}

我希望我的表格中的每一行都突出显示,但它不适用于我上面的CSS代码。我怀疑因为我的桌子上有一个行盘,所以它不能很好地运行。我该怎么做才能解决我的问题?如何使用rowspan制作突出显示的表格?

1 个答案:

答案 0 :(得分:0)

您可以按照示例向“Premium”单元格添加一个特殊的钩子类。然后将它们定位在主.row:hover状态。

此外,您不需要td[rowspan]部分。

请考虑以下代码:

.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>

我认为可能会创建一个更好的标记结构,因为这不是易于维护的代码,但正如您在上面看到的那样它肯定可以工作。