【问题标题】:Changing text and background colour CSS / HTML更改文本和背景颜色 CSS / HTML
【发布时间】:2019-02-06 16:46:03
【问题描述】:

我正在使用这个类来改变背景和文本颜色,效果很好。

#cellType1 {
  width: 10%;
  height: 83px;
  vertical-align: middle;
  background-image: url("Index.html")
}
#cellType1:hover {
color: white;
background-color: #6EBA37;
transition:0.6s;
}

...

<td id="cellType1" class="auto-style21" style="width: 10%">
      <a class="auto-style24" href="ContactUs.html">
      <span class="auto-style37">Contact Us </span></a></td>

auto-style21 工作正常,但是当我将文本更改为超链接时,我无法让文本更改颜色。

我知道这与样式有关,但我无法弄清楚当单元格悬停时如何控制超链接的文本。甚至可以在将鼠标悬停在文本上时更改文本的颜色。

我更希望在鼠标进入单元格时更改文本。

【问题讨论】:

标签: html colors hyperlink hover mouse


【解决方案1】:

您需要设置&lt;a&gt; 元素的样式。您的 CSS 仅针对 &lt;td&gt;

a {
  color: red;
}

a:hover {
  color: white;
  background-color: #6EBA37;
}

以 sn-p 为例。

a {
  color: red;
}

a:hover {
  color: white;
  background-color: #6EBA37;
}

#cellType1 {
  width: 10%;
  height: 83px;
  vertical-align: middle;
  background-image: url("Index.html")
}

#cellType1:hover {
  color: white;
  background-color: #6EBA37;
  transition: 0.6s;
}
<td id="cellType1" class="auto-style21" style="width: 10%">
  <a class="auto-style24" href="ContactUs.html">
    <span class="auto-style37">Contact Us </span></a>
</td>

【讨论】:

  • 感谢您的建议我尝试了您的解决方案,效果很好,但我更喜欢 Brisbe42 的解决方案
【解决方案2】:

您需要直接或间接定位A,而不是单元格的悬停,以使样式应用于链接。

如果您希望链接根据单元格本身的悬停而更改,您可以使用以下内容:

更改这部分代码:

#cellType1:hover {
color: white;
background-color: #6EBA37;
transition:0.6s;
}

到这里:

#cellType1:hover {
color: white;
background-color: #6EBA37;
transition:0.6s;
}

#cellType1:hover a {
color: white;
transition:0.6s;
}

【讨论】:

  • 谢谢 Brisbe42 这就是我要找的效果。
【解决方案3】:

如果您想更改该元素的颜色,则需要针对悬停定位 a 标记而不是 td 标记。请尝试以下操作:

#cellType1:hover a {
    color: #00ff00;
}

【讨论】:

    猜你喜欢
    • 2018-12-25
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多