为了清晰起见,有没有办法对 CSS 选择器进行分组?
如果我有十几个 CSS 选择器,并且想要为所有这些选择器分配 :hover
属性,我习惯这样做:
selector, selector2, someOtherSelector, someSelector div {
//some properties
}
selector:hover, selector2:hover, someOtherSelector:hover, someSelector div:hover {
//some properties
}
输入 :hover
四次似乎是多余的。有没有办法像这样对选择器进行分组
(selector, selector2, someOtherSelector, someSelector div):hover {
//some properties
}
?
If I have a dozen CSS selectors, and want to assign :hover
properties to all of them, I'm used to doing this:
selector, selector2, someOtherSelector, someSelector div {
//some properties
}
selector:hover, selector2:hover, someOtherSelector:hover, someSelector div:hover {
//some properties
}
Typing :hover
four times seems redundant. Is there a way to group the selectors like
(selector, selector2, someOtherSelector, someSelector div):hover {
//some properties
}
instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不是 CSS 原生的。通过使用诸如 SCSS 之类的东西,您可以编写:
Not natively in CSS. By using something like SCSS, you can write:
如果它们都共享相同的悬停属性,您可以创建一个为所有定义您的 :hover 的人共享的类,
因此您会得到:
可重用的类使代码更干净、更少。
If they all share the same hover properties you could create a class that is shared for all that defines your :hover
So you'd get:
Re-usable classes makes for cleaner and less code.
遗憾的是,实际上没有任何更简单的方法来做你想做的事情。除非你想将样式移至 jQuery 或其他东西(但这不是一个好的解决方案)。
Sadly, there's not really any easier way of doing what you're trying to do, unfortunately. Unless you want to move the styles to jQuery or something (but that's not a good solution).