css - 使用 & 在类中嵌入伪元素

标签 css pseudo-element

我正在使用 CSS/JS 构建一个旋钮,我在一个示例中偶然发现了一个 CSS 语法,我想在我的代码中重现它,但它似乎不起作用。我也尝试强制使用不同的 z-index 但没有成功。

这是有效的:

.knob 
{
  position: relative;
  width: 100px; height: 100px;
  border-radius: 50%;
  background: gray;
  transform: rotate(0deg);
}

.knob:before
{
  content: '';
  position: absolute;
  width: 12px; height: 12px;
  left: 44px; top: 12px;
  border-radius: 50%;
  background: white;
}

这就是我想要做的:

.knob 
{
  position: relative;
  width: 100px; height: 100px;
  border-radius: 50%;
  background: gray;
  transform: rotate(0deg);

  &:before
  {
    content: '';
    position: absolute;
    width: 12px; height: 12px;
    left: 44px; top: 12px;
    border-radius: 50%;
    background: white;
  }
}

我相信它应该有相同的结果,但作为一个诚实的 CSS 新手,我不明白这是怎么回事。 Here's a CodePen with the full code.

最佳答案

该语法是SCSS 语法。你不能在你的 css 文件中写这样的代码。您可以设置从 SCSSCSS 的编译,并将您的代码写入您的 SCSS 文件。

关于css - 使用 & 在类中嵌入伪元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54913888/

相关文章:

jQuery:使用 closest()、find() 和 children() 获取特殊父项的 sibling

html -::selection 伪选择器忽略::before 伪元素

CSS :before superscript "6"?

html - 在元素之间显示伪元素

html - CSS正确定位伪元素

html - 占据所有剩余空间的 div 中的绝对定位 div

javascript - Typeahead.js 干扰 Bootstrap 输入组

python - 用python或selenium获取css伪元素内容

jquery - 滚动时将标题粘贴并替换到窗口顶部

css - SCSS 符号使用错误