css - 这个mixin是什么意思?

标签 css sass scss-mixins

在 scss 文件中,我看到了下面的代码片段:

@mixin myMixin() {
    :global(.rtl) & {
        @content;
    }
}

我了解关键字 @mixin 以及 @content 并尝试通过此链接了解 :global() : What does :global (colon global) do? .

但我不确定“&”在这里做什么或整个 mixin 做什么。

最佳答案

与号 (&) 是用于嵌套的组合符,在本例中它用于限定选择器。

举个一般的例子:

// This Sass...
.foo {
  .bar & {
    color: red;
  }
}

// ...would compile to this CSS
.bar .foo { color:red; }

在您的示例中,mixin 声明替换了 .foo,并且看起来像:

// If you called your mixin at .bar
.bar {
  @include myMixin {
    color: red;
  }
}

// It should compile to something like this
:global(.rtl) .bar { color: red; }

关于 ampersands and qualifying selectors in this CSS Tricks article 的更多详细信息.

关于css - 这个mixin是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54974250/

相关文章:

javascript - 单击时如何保留按钮周围的矩形边框

css - Sass for 循环中的变量范围

css - 从 flex 容器中排除子元素

twitter-bootstrap - 从我的 scss 文件中使用 Bootstrap 的实用程序 API

javascript - 有没有办法在 react 中使用 JS 动态更新 scss 变量?

javascript - 基本的 JQuery .animate() 不起作用,但可以使用 .css() 更改相同的 CSS!

css - 尝试更改动态创建面板的宽度,但不起作用

html - CSS 选择器测试器有效性

css - 我可以在 SASS mixin 中声明一个新变量吗?

css - 如何在 HTML 中包含 SCSS 文件