html - 扩展 & :hover of any class to any other class's hover in SASS or SCSS

标签 html css sass hover

有没有办法将任何类的 &:hover 扩展到任何其他类的悬停?

我的代码是这样的,它停止了两个类的悬停:

HTML

<a href="#" class="button1">Button 1</a>
<a href="#" class="button2">Button 2</a>

SCSS

.button1 {
  background: black;
  padding: 5px 10px;
  text-decoration: none;
  color: white;
  &:hover {
    background: red;
  }
}

.button2 {
  background: blue;
  padding: 5px 10px;
  text-decoration: none;
  color: white;
  &:hover {
    @extend .button1:hover
  }
}

最佳答案

您可以使用占位符。

%hover {
  background: red;
}

.button1 {
  background: black;
  padding: 5px 10px;
  text-decoration: none;
  color: white;
  &:hover {
    @extend %hover;
  }
}

.button2 {
  background: blue;
  padding: 5px 10px;
  text-decoration: none;
  color: white;
  &:hover {
    @extend %hover;
  }
}

或者,也许更好的解决方案是为您的按钮使用更通用的类:

.btn {
  padding: 5px 10px;
  text-decoration: none;
  color: white;

  &:hover {
    background: red;
  }
}

.btn--1 {
  background: black;
}

.btn--2 {
  background: blue;
}

关于html - 扩展 & :hover of any class to any other class's hover in SASS or SCSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34350513/

相关文章:

javascript - 如何在使用 jquery 时获取标签内的所有 html?

html - 背景图片向下重复

css - 将两个元素并排放置, float 会使 img 消失

css - 编写 Sass 函数的正确方法是什么?

html - Bootstrap 模板问题(移动菜单)

javascript - 为什么某些字体的元素的 scrollHeight 和 clientHeight 不一样?

html - CSS内联Div不可见间距?

html - 使用 css3 的响应式设计

css - 有没有办法让 Prepros 不缩小 CSS 输出?

css - Slippry 示例缩略图(图形轮播)