简体   繁体   English

更改文本和背景色CSS / HTML

[英]Changing text and background colour CSS / HTML

I am using this class to change the background and text colors which works great. 我正在使用此类更改背景和文本颜色,效果很好。

#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 works fine but when I change the text to a hyperlink I cannot get the text to change color. auto-style21可以正常工作,但是当我将文本更改为超链接时,我无法使文本更改颜色。

I know this is something to do with the style but I cannot figure out how to control the text of the hyperlink when the cell is moused over. 我知道这与样式有关,但是当单元格悬停在单元格上时,我无法弄清楚如何控制超链接的文本。 Even changing the color of the text when mousing over the text would do. 将鼠标悬停在文本上时,甚至可以更改文本的颜色。

I would much prefer the text to change as the mouse enters the cell. 我更希望文本随着鼠标进入单元格而改变。

You need to directly or indirectly target the A, rather than the hover of the cell, to make the style apply to the link. 您需要直接或间接定位A,而不是单元格的悬停,以使样式适用于链接。

If you want the link to change based on the hover of the cell itself, you can use the following: 如果要根据单元格本身的悬停更改链接,可以使用以下命令:

Change this section of code: 更改这段代码:

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

to this: 对此:

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

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

You need to style the <a> element. 您需要设置<a>元素的样式。 Your CSS targets the <td> only. 您的CSS仅定位<td>

a {
  color: red;
}

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

See snippet for example. 参见示例。

 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> 

You need to target the a tag instead of the td tag for the hover if you would like to change the color of that element. 如果要更改该元素的颜色,则需要将a标签而不是td标签作为目标。 Try the following: 请尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM