简体   繁体   中英

Why do I get "Undefined variable" errors with pseudo-selectors in SASS?

The following is perfectly valid CSS:

#someID::before {
   --var: 1px;
}

.someClass #someID::before {
   # use --var in here
}

But when I try to do the same thing in SASS, I get an undefined variable error:

#someID::before {
   $var: 1px;
}

.someClass #someID::before {
   # use $var in here
}

Am I misunderstanding how scoping works in SASS?

CSS variables ( CSS custom properties (variables) ) and SCSS variables are not the same. SCSS variables are for values. For example, let's say I have a brand guideline and I use the colours like this:

$black: #000;
$white: #fff;

And now I can use this way:

header {
  .topbar {
    background: $white;
  }
}

But CSS variables are similar. And also, you are declaring the variable in the global scope there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM