简体   繁体   English

将:hover分配给类而不是元素

[英]Assigning :hover to a class rather than an element

I'm working on a decent-sized project in which there are a bunch of elements I want to fade slightly when the user hovers on them. 我正在做一个体面的项目,其中有很多元素要在用户悬停时稍微淡化。 This is my current code: 这是我当前的代码:

.brand-images img,
.horList li a,
.producto__descripcion a,
.slider-btn,
.social-media__banner a,
.marca-productos__foto,
.marca-productos__nuevos-productos a,
.producto-zona__image a,
.campanas a {
    opacity: 1;
}

.brand-images img:hover,
.horList li a:hover,
.producto__descripcion a:hover,
.slider-btn:hover,
.social-media__banner a:hover,
.marca-productos__foto:hover,
.marca-productos__nuevos-productos a:hover,
.producto-zona__image a:hover,
.campanas a:hover {
    opacity: .5;
}

This is obviously not very concise. 这显然不是很简洁。 My question is: is it possible to assign a :hover pseudo class to a normal CSS class? 我的问题是:是否可以将:hover伪类分配给普通的CSS类? Something like this is what I had in mind: 我想到的是这样的事情:

.hover-fade { opacity: 1; }
.hover-fade:hover { opacity: .5 }

That way I'd be able to just apply that class to the elements I want. 这样,我将能够将该类应用于所需的元素。 If it's not possible, any other suggestions of how to cut down the CSS are welcome. 如果不可能的话,欢迎其他有关如何减少CSS的建议。

You can apply :hover to any valid css selector that has :hover pseudo element. 您可以将:hover应用于任何具有:hover伪元素的有效CSS选择器。

 div.wrapper a[name="link"]:hover, a:hover, aa:hover, [name="link"]:hover, div a:hover, .link, .wrapper .link { color: red; } 
 <div class="wrapper"><a name="link" class="a">Link</a> </div> 

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

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