Stop SCSS from processing CSS variables in Angular-CLI

I am currently using Angular5 with sass v1.3.2.

My goal is to dynamically change a color that is widely used in the scss files of my single page app without having to recompile new files.

This color is globally defined in my _variables.css as:

$brand: #123123;

For instance, it is utilized like this:

h1 {
    color: $brand;
}

Upon researching, I discovered that I can alter the color if CSS variables are used, for example:

# CSS
:root {
    --brand: #123123
}

#JS
document.documentElement.style.setProperty('--brand', '#456456');
# OR
document.querySelector(':root').style.setProperty('--brand', '#456456');

But to achieve this using SCSS, I had to utilize the css-vars mixin as follows:

$brand: #123123;

:root {
  @include css-vars((
    --brand: #{$brand},
  ));
}

And then utilize it like so:

h1 {
    color: var(--brand);
}

However, two issues arose:

  • Even after these steps, --brand wasn't visible at root.
  • In addition, the CSS generated in <script type="text/css"> by angular-cli did not contain --brand anywhere; instead, it compiled the CSS variable into #123123, resulting in:

    h1 { color: #123123; }

Do you have any suggestions on how to successfully modify a global color at runtime? Or perhaps how to place my CSS within :root and prevent SASS from compiling it?

UPDATE

As demonstrated by @JonUleis, using css-var is unnecessary. Now, the variable --brand appears in the DOM under :root.

However, the line color: var(--brand); still does not show up in the CSS, causing h1 to lack a color style entirely.

After updating node-sass to version 4.9.0 from 4.8.3, everything worked perfectly.

Answer №1

It seems that your node-sass version may be outdated and not compatible with the CSS custom properties syntax.

I have successfully compiled your example code using Sassmeister without utilizing the css-vars mixin:

https://i.stack.imgur.com/baIrO.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Karma Test Error: Disconnected due to lack of communication within a 60000 millisecond timeframe

Executing the "ng test" command for Unit Testing in an Angular project is resulting in some errors. Chrome Headless 120.0.6099.130 (Windows 10) ERROR Disconnected , because no message in 60000 ms. Chrome Headless 120.0.6099.130 (Windows 10): Executed 404 ...

What is the best way to use a single schema in MongoDB to create an object with an array?

Currently, I am working on creating a single Schema for Author with their respective quote. Here's what my model looks like at the moment: const mongoose = require("mongoose"); const AuthorSchema = new mongoose.Schema({ name: String, quote: ...

What is the recommended module to choose when importing MAT_DIALOG_DATA?

When working on my Angular project, I noticed that there are multiple options available for importing MAT_DIALOG_DATA. I have tried importing both @angular/material and @angular/material/dialog, and found that both options work successfully. However, I a ...

The issue arises when trying to use ::v-deep in conjunction with v-dialog's content-class when using scoped scss

I've been working on styling the content of the Vuetify dialog component and have been using the content-class prop along with scoped styles to achieve this. Can someone explain the difference between the styles provided below? And also, any tips on h ...

As you hover over the div, the content inside subtly shifts upwards and downwards

I am facing an issue with my movie list. When hovering over a movie, a border appears around it as expected. However, I also notice that at the same time, it seems like extra padding is being added to the 'top' and 'bottom', causing the ...

The link text does not appear in black color

Can anyone assist in modifying this code snippet? I'm trying to make the text appear black, but it's showing up as light grey on the Dreamweaver preview screen even though the CSS says it should be black. View Fiddle Below is my HTML code: < ...

Angular2 with Electron is a stellar demonstration of a successful integration

I'm interested in exploring Electron with Angular2 and I'm on the lookout for a reliable example on GitHub that combines these technologies. Ideally, I would like to find a repository that I can easily download as a zip file, run npm install, npm ...

Inquiring about how to make the pieces move on the checker boards I created

I'm having trouble getting my SVG pieces to move on the checkerboard. Can someone please help me figure out how to make them move, even if it's not a valid move? I just want to see them in motion! The important thing is that the pieces stay withi ...

Have you made a selection in both bi-directional bound select dropdowns?

I have implemented two-way binding on select dropdowns using ngFor, and they are functioning correctly. However, upon page load, no option is selected by default. I attempted to use [selected] but it did not work... Any ideas on how to resolve this? Code: ...

Dealing with a 500 (Internal Server Error) error when using the HttpClient library

My implementation using HttpClient is as follows: file: app.component.ts this.appService.fetchObject(param) .subscribe((response: ObjectDTO) => { // Perform certain actions }, error => { var ...

Passing parameters in a jQuery function through a button click

I am trying to figure out how to pass the @item.Id value as a parameter in the click function for a button. Here is the code snippet: <button id="buttonEdit" style="color:darkgreen" data-toggle="modal" data-target="#Ed ...

Resizing background images to their original size using jQuery

Can anyone help me figure out the actual size in pixels of a background image that is being displayed? <style> body { background-image: url(background.jpg); background-size: contain; } </style> I tried using jQuery to get the size of ...

Arrange two divs next to each other on the page: the div on the right can be scrolled through, while the div on the

I am attempting to create a layout similar to this https://i.sstatic.net/Y1FCp.png My goal is to: Have two main divs positioned side by side inside a wrapper div Ensure the wrapper expands to fill 100% of the browser size The left div (banner) should b ...

Adding a prefix to the imported CSS file

My React app, created with create-react-app, is designed to be integrated as a "widget" within other websites rather than functioning as a standalone application. To achieve this, I provide website owners with minified JS and CSS files that they can inser ...

Achieving a Photo Grid Layout with CSS

Looking for help with my CSS photogrid - it's close to what I want, but not quite there. Here's the layout I'm aiming for: 1 | 2 | 3 4 | 5 | 6 But currently, it displays like this: 1 | 3 | 5 2 | 4 | 6 I've tried tweaking the CSS but ...

I must remove the thumb from the input range control

I am looking for a way to remove the thumb from the progress bar in my music player. I have tried various methods without success, and I simply want the progress bar to move forward with color change as it advances based on the Progress variable. For refe ...

How does the browser interpret the CSS property transform: none?

Does the transform: none result in matrix(1,0,0,1,0,0) or a different outcome? I am looking to reset the transform; what is the best approach for this? <style> .box{ height: 100px; width: 100px; background-color: red; transition: 0.5s; transform: ...

Angular 10 library devoid of ivy technology

After successfully building an Angular 10 library and using it via a direct import (file:dist/my-lib), I decided to publish the library to a private npm repository. To accomplish this, I made changes to my tsconfig.lib.json file and disabled ivy: "ang ...

Difficulty linking CSS to HTML in a Flask web application

My HTML/CSS file is pretty straightforward: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta charset="utf-8"> <meta name=&quo ...

Is it feasible to capture a screenshot of a URL by using html2canvas?

Is it possible to take a screenshot of a specific URL using html2canvas? For example, if I have the following URLs: mydomain.com/home mydomain.com/home?id=2 mydomain.com/home/2 How can I capture and display the screenshot image on another page? window ...