how do I use the CSS hover on only SOME cells in a table?

早过忘川 提交于 2021-02-08 23:38:37

问题


How do I use the CSS hover on only SOME cells in a table?
Can I turn it off on those that I don't want it applied to? I'm using this:

td:hover {
    border-style:dotted;
    border-color:#F60;
    border-width:medium;
    border-left-style:dotted;
    border-right-style:dotted;

}

But I need it to only apply to certain cells


回答1:


Instead of specifying your style for all cells you can create a class and only apply that to the cells you want the style on. Update your css to this:

.myclass:hover {
    border-style:dotted;
    border-color:#F60;
    border-width:medium;
    border-left-style:dotted;
    border-right-style:dotted;

}

Then you do something like this in the HTML code:

<table>
  <tr>
    <td class="myclass">Cell 1 with special hoover</td>
    <td>Cell 2</td>     
    <td>Cell 3</td>
  </tr>
</table>



回答2:


the <td> tag supports Global Attributes in HTML so you can simply add a class to your code for each of the table cells you want to have the hover on.

...
<td class="cell-hover">Table Cell Data</td>
...

The modify your CSS Selector to include the selection of only td cells that have the "cell-hover" class.

td:hover {
   border-style:dotted;
   border-color:#F60;
   border-width:medium;
   border-left-style:dotted;
   border-right-style:dotted;
}

By using a . within CSS you can select only the elements that have the proceeding class name



来源:https://stackoverflow.com/questions/35585179/how-do-i-use-the-css-hover-on-only-some-cells-in-a-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!