简体   繁体   English

CSS 在 hover 的子类中改变颜色?

[英]CSS changing color in a subclass on hover?

I'm currently working on a portfolio website, and for some of my links I have certain text that's bolded, whereas the rest of the sentence is a normal weight.我目前在一个投资组合网站上工作,对于我的一些链接,我有某些加粗的文本,而句子的 rest 是正常的重量。 I was wondering if there was a way to only change the color on the bolded portion when you hover over the sentence.我想知道是否有一种方法可以仅在您对句子进行 hover 时更改粗体部分的颜色。 I know I can use JavaScript to accomplish this, but I was wondering if there was a pure CSS solution.我知道我可以使用 JavaScript 来完成此操作,但我想知道是否有纯 CSS 解决方案。

Here's my HTML markup for a list item:这是我的列表项的 HTML 标记:

<li>
  <a href="about.html">
     <img src="images/info-icon.png" alt="about icon"> learn more <span class="bold">about 
     me</span>
  </a>
</li>

I'd like to only change the color of the text in the bold span when you hover over any part of the link.当您在链接的任何部分 hover 时,我只想更改粗体文本的颜色。 I put the link on everything within the li tag because I thought it was a bit misleading to put icons that weren't links.我将链接放在 li 标签内的所有内容上,因为我认为放置不是链接的图标有点误导。 Thank you!谢谢!

use hover selector for parent only in css as below仅在 css 中为父级使用 hover 选择器,如下所示

parent-class:hover child-class{
    /* your css here */
}

so for your code try like this所以对于你的代码试试这样

 a:hover span.bold { color: red }
 <li> <a href="about.html"> <img src="images/info-icon.png" alt="about icon"> learn more <span class="bold">about me</span> </a> </li>

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

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