3

I'm trying to use CSS Custom properties in my Angular 7 app but am running into issues where building down to css gets rid of the property that is set to a CSS variable.

SCSS

:root {
  --my-test-var: red;
}

.testclass123 {
  height: 150px;
  background: var(--my-test-var);
}

Builds down to:

:root {
  --my-test-var: red;
}

.testclass123 {
  height: 150px;
}

If I use a fallback options like var(--my-test-var, purple); then .testclass123 will also have property background: purple;

My Angular version is 7.2.7 and Angular Material 7.3.7

8
  • Are you doing it in global styles and not component-level? Commented Oct 31, 2019 at 20:11
  • Ideally I'd be able to use them globally. Currently I've tried both the global stylesheet and component styles Commented Oct 31, 2019 at 20:13
  • Can you reproduce it on StackBlitz? Also, I suppose that you may run some sort of pet project and if it so it would be better to switch onto 8th version (why use obsolete)? Anyway, to answer this question we need either a StackBlitz example or GitHub project to investigate Commented Oct 31, 2019 at 20:17
  • Also, just for try - try to change red onto some hex code Commented Oct 31, 2019 at 20:17
  • 1
    It works with latest version. stackblitz.com/edit/angular-p7h5vv Maybe it's worth deleting node_modules and running npm i to reinstall sass compiler Commented Oct 31, 2019 at 20:30

1 Answer 1

0

If you use SCSS as global style you can define like this the color. This is the best approach.

$product: red;

And then only you call when you need like this.

background-color: $product;
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.