5

Please have a look at Stackblitz or the code below
Having a problem where variables from :root are not set to elements. But when I rename :root => .component. It works as expected. The box is shown.
Why :root variables are not set? Are not they are global or It is specificity problem?
Thanks in advance.

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>

compiled to css output :

: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 package is used to compile scss to css.

6
  • Cannot reproduce based on given code. Could you reproduce this in a Stack Snippet? Commented May 2, 2018 at 11:31
  • hi @Paulie_D. I have uploaded the file to stackblitz. Could you pls check there. It is ready to edit. Commented May 2, 2018 at 12:08
  • @Paulie_D . I see but stack snippet does not support scss which this questions related to. Commented May 2, 2018 at 12:15
  • If it actually problem relates to SCSS..but you don't know that it does....do you? Does it work in the compiled CSS? Commented May 2, 2018 at 12:16
  • it is actually either node-sass or scss related issue. Commented May 2, 2018 at 12:21

1 Answer 1

1

SCSS and CSS4 variables behave different.

SCSS variables format:

$color: red;


.classRed{
  color: $color;
}

SCSS will compile to CSS this way:

.classRed{
  color: red;
}

CSS4 variables format:

:root{
  --color: red;
}

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

CSS won't compile like SCSS since it is already CSS (it's using the variables CSS4 feature).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.