简体   繁体   English

CSS Hover属性在子元素而不是整个元素上

[英]CSS Hover attribute on sub-element rather than entire element

Hopefully this makes sense - my html is as follows 希望这是有道理的 - 我的HTML如下

html HTML

<a href="#" class="someclass">An <span class="sub">Interesting</span> Link</a>

where the css is simply color information. 其中css只是颜色信息。 What I'd like to do is to make it so when you mouseover the link as a whole only the "sub" information is affected by the hover attribute. 我想要做的就是这样做,当你将鼠标悬停在整个链接上时,只有“子”信息受到悬停属性的影响。 While this would be easy enough to do in javascript I was curious as to whether or not css allowed for this behavior. 虽然这在javascript中很容易做到,但我很好奇css是否允许这种行为。 If I were to try to write this it would probably look like 如果我试着写这个可能看起来像

a:hover{
    .sub{color:newcolor};
}

however I've never seen anything like that and obviously it doesn't work 但是我从来没有见过这样的东西,显然它不起作用

a:hover .sub { color: newcolor }

Like this: 像这样:

a:hover .sub { color: #f00; }

If you need to apply it only to that specific class: 如果您只需要将它应用于该特定类:

a.someclass:hover .sub { color: #f00; }

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

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