css - 如何让css不重复同一个类?

标签 css sass

.class-action:hover, .class-action.activeOne {
  color: black;
  font-weight: bold;
}

.class-action.activeOne {  
  margin: 3px 0px; 
  border-bottom: 2px solid black;
}

有没有办法堆叠这些CSS代码?我不想再重复同一个类。我想要“.class-action.activeOne”的所有四种样式,并且在“.class-action:hover”时只需要以上 2 种样式

其他选项如下,但它重复了 css

.class-action:hover {
  color: black;
  font-weight: bold;
}

.class-action.activeOne {
  color: black;
  font-weight: bold; 
  margin: 3px 0px; 
  border-bottom: 2px solid black;
}

最佳答案

简单地嵌套你的选择器:

.class-action {
    &:hover,
    &.activeOne {
        color: black;
        font-weight: bold;
    }

    &.activeOne {  
        margin: 3px 0px; 
        border-bottom: 2px solid black;
    }
}

关于css - 如何让css不重复同一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74866072/

相关文章:

html - 下拉菜单被截断

html - 如果一个元素的祖先元素的高度 < 100%,有没有办法在不使用 JS 的情况下仍然使它成为浏览器窗口的高度?

css - 编辑 sass-rails 配置选项

html - 如果标签内部有跨度,则复选框会自动取消选中

javascript - 悬停和 javascript 事件问题

css - 变量中存在多个类名

html - 最大宽度下的 Tab UI 溢出切割元素

javascript - 垂直导航中的子类别被隐藏且显示不正确

javascript - 是否有机会通过 css 将 div 类插入 javascript

css - Symfony2 Assetic cssrewrite 制作错误链接