:root CSS变量未设置为元素样式

5
请查看Stackblitz或以下代码
出现了一个问题,:root变量未设置为元素。但是当我将:root重命名为.component时,它按预期工作。框被显示。
为什么:root变量没有被设置?它们不是全局的吗或者这是一个特殊性问题?
谢谢。

SCSS:

:host {
  display: block;
}
:root {
  --profile-picture-size-w: 9rem;
  --profile-picture-size-h: 9rem;
  --profile-picture-border: 1px solid #1e96a9;
  --profile-box-backgound-size: cover;
  --profile-box-p: 20px 0;
  --article-dateInfo: 3px;
}

.component{
  height: 500px;
  background: lightgray;
  #main_content {
    .scrollbarContent{
      article#profile {
        .profileBox {
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          padding: var(--profile-box-p);

          .profilePicture {
            position: relative;
            width: var(--profile-picture-size-w);
            height: var(--profile-picture-size-h);
            background: cyan;
          }
        }
 }
    }

  }

}

@media all and (min-width: 768px){
  :root {
    --profile-picture-size-w: 5.5rem;
    --profile-picture-size-h: 5.5rem;
    --profile-picture-border: 5px solid #1e96a9;
    --profile-box-backgound-size: 100% 38%;
    --profile-box-p: 20px 0 0 0;
    --article-date-info: 0;
  }
}

HTML:

<div class="component">
  <perfect-scrollbar id="main_content">
    <div class="scrollbarContent">
      <article id="profile">
        <div class="profileBox">
          <div class="profilePicture">
          </div>
        </div>
      </article>
    </div>
  </perfect-scrollbar>

</div>

编译成 CSS 输出:

:host {
  display: block;
  height: auto;
  margin-left: auto;
  margin-top: 6px; }

:root {
  --profile-picture-size-w: 9rem;
  --profile-picture-size-h: 9rem;
  --profile-picture-border: 1px solid #1e96a9;
  --profile-box-backgound-size: cover;
  --profile-box-p: 20px 0;
  --article-dateInfo: 3px; }

.component {
  height: calc(100%); }
  .component #main_content .scrollbarContent article#profile .profileBox {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--profile-box-p); }
    .component #main_content .scrollbarContent article#profile .profileBox .profilePicture {
      position: relative;
      width: var(--profile-picture-size-w);
      height: var(--profile-picture-size-h);
      margin-top: 14px;
      background: cyan; }

@media all and (min-width: 768px) {
  :root {
    --profile-picture-size-w: 5.5rem;
    --profile-picture-size-h: 5.5rem;
    --profile-picture-border: 5px solid #1e96a9;
    --profile-box-backgound-size: 100% 38%;
    --profile-box-p: 20px 0 0 0;
    --article-date-info: 0; }
  :host {
    height: 77vh; } }

Node-sass包用于将scss编译成css。


无法根据给定的代码进行复现。您能否在 Stack Snippet 中复现此问题? - Paulie_D
嗨@Paulie_D。我已经上传文件到Stackblitz了。你能不能去那边看看呢?它已经可以编辑了。 - Vugar Abdullayev
@Paulie_D。我知道,但是Stack Snippet不支持与此问题相关的SCSS。 - Vugar Abdullayev
如果问题实际上与SCSS有关...但你不知道它是这样的...是吗?在编译后的CSS中是否有效? - Paulie_D
这实际上是涉及到node-sass或scss的问题。 - Vugar Abdullayev
当然。同时,我在问题中添加了输出。Output: 输出: - Vugar Abdullayev
1个回答

1

SCSS和CSS4变量的行为不同。

SCSS变量格式:

$color: red;


.classRed{
  color: $color;
}

SCSS 会以这种方式编译为 CSS:

.classRed{
  color: red;
}

CSS4变量格式:

:root{
  --color: red;
}

.classRed{
  color: var(--color);
}

CSS不会像SCSS那样编译,因为它已经是CSS了(它正在使用variables CSS4 feature)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接