css - SCSS : parent hover selector

标签 css sass

scss 中我们可以使用父选择器类:

<div class="parent is-active">
  <div class="children"></div>
</div>

.parent {
  width: 200px;
  height: 200px;
}

.children {
  height: 100px;
  width: 100px;

  .is-active & {
    background: red;
  }
}

example

您将如何编写父级 hover 选择器?

最佳答案

如果我正确理解你的问题:https://jsfiddle.net/wx9L9jzj/3/

SCSS:

.parent {
  width: 200px;
  height: 200px;
}

.children {
  height: 100px;
  width: 100px;
  
  .is-active & {
    background: red;
  }
  
  :hover > & {
    background: blue;
  }
}

请注意 :hover > & SCSS 选择器,它主要查看父节点以确定子节点是否在 :hover 伪类中。这不是标准 CSS,需要将 SASS 语法编译为标准 CSS,SASS 通过重写选择器来实现。

根据 SASS documentation on the parent (&) selector :

The parent selector, &, is a special selector invented by Sass that’s used in nested selectors to refer to the outer selector. It makes it possible to re-use the outer selector in more complex ways, like adding a pseudo-class or adding a selector before the parent.

When a parent selector is used in an inner selector, it’s replaced with the corresponding outer selector. This happens instead of the normal nesting behavior.

关于css - SCSS : parent hover selector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37149015/

相关文章:

html - 如何在 SCSS 中设置数据占位符属性的样式?

html - 我应该如何设计这个 img 元素来达到我想要的效果

angular - 使用 webpack 和 SCSS 将 Materialize 集成到 Angular2 项目中

css - Bootstrap 的 SCSS 无法编译 - 总是得到 "` unit($number )` must be a number"错误

css - sass中类名的动态变量

css - 如何在scss中编写带参数的类?

CSS 菜单链接保持突出显示

html - Bootstrap 4 具有列宽的响应表

javascript - 我的全日历背景颜色在 listDay 或 listWeek 上不起作用,但在 Month 上起作用

javascript - 添加事件类的 slider