css - 表格中的文本和背景颜色

标签 css html-table colors

我有一个表格,我希望当鼠标悬停在其上时,单元格的内容及其背景会改变颜色。所以我尝试了以下 CSS

td {
  background-color: white;
  color: black;
}

td:hover {
  background-color: black;
  color: white;
}
<table width="150px">
  <tr>
    <td><a href="www.testing.com">testing</a></td>
  </tr>
</table>

看似简单,其实行不通。背景会改变颜色,而文本不会。

我错在哪里?我该如何纠正?

最佳答案

好的,链接通常应用了自己的悬停样式,因此您需要定位表格中的链接以更改其颜色:

td {
  background-color: white;
  color: black;
}

td:hover {
  background-color: black;
  color: white;
}

td:hover a {     /* only target links in a td cell that is hovered */
  color: white;
}
<table width="150px">
  <tr>
    <td><a href="www.testing.com">testing</a></td>
  </tr>
</table>

关于css - 表格中的文本和背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53430991/

相关文章:

android - 隐藏标题栏时删除 Android 应用程序上可见的底部填充

css - 使用 Div 时保留 Div 父背景图像 :hover

html - HTML 中的表格应该是坏的吗?为什么有这么多关于无表 CSS 的讨论?

rstudio 颜色外观,包名称的特定颜色,front::

javascript - 如何让这个链接在这个元素上工作?

html - 简单的 DIV float /堆栈问题(右侧固定,左侧灵活,媒体规则)

c# - 更改事件标签页上的字体颜色

iOS - 按下时色调颜色会发生变化

html - 修复了对齐的表头问题

MySQL + JSON : Retrieve all data where id = term