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

Ways to hide a div element when the size of the window is decreased

I need help with keeping my logo fixed on the header of my website. You can view an example of the issue here: Currently, when the window is resized to a certain height, the logo (The sun) changes position. How can I ensure that it remains fixed in place? ...

What is the best way to zoom in on an image and make it move off the screen as I scroll through my webpage?

I am working on a website design where I would like to implement an image zoom effect as the user scrolls down. Additionally, I want the image to move to either the left or right side of the screen as it goes out of view when scrolling to the bottom. While ...

Revamp Your Service Naming and Nickname with Swagger Codegen IO

Is it possible to customize the Swagger IO CodeGen naming conventions for generating Angular API Service Proxies? Check out Swagger Editor here The current convention combines API, Controller Name, Controller Method, and HTTP Action. public apiProductGet ...

Creating an Http interceptor in Ionic 3 and Angular 4 to display a loading indicator for every API request

One of my current challenges involves creating a custom HTTP interceptor to manage loading and other additional functions efficiently. Manually handling loading for each request has led to a considerable increase in code. The issue at hand: The loader is ...

Manipulating array objects by replacing values in Typescript

Attempted two different methods to obtain a partial summary within each array object, but unfortunately, both were unsuccessful. var arr = [ { "value": 10, "newBalance": 0 }, { "value": -10, "newBalance": 0 }, ...

Tips to avoid multiple HTTP requests being sent simultaneously

I have a collection of objects that requires triggering asynchronous requests for each object. However, I want to limit the number of simultaneous requests running at once. Additionally, it would be beneficial to have a single point of synchronization afte ...

What is the process for including a version number in npm pack?

I've been working on deploying TeamCity with NPM pack, and I've noticed that it's packaging everything up as filename-0.0.0.tgz. So, I decided to switch to the command line to troubleshoot before running it through TC. When I run npm pack i ...

Crafting a captivating stacked image using just two segments

Recently, I designed my own personal website. To make it visually appealing, I decided to use a background picture as the header and placed my portrait as the avatar. My inspiration for the design was drawn from the sleek interface of Linkedin profiles. Ho ...

Clicking on tabs causes their content to mysteriously vanish

I am working on creating a dynamic menu using jQuery that switches content based on tabs. However, I am facing an issue where clicking on a different <li> item causes the content to disappear. Each list item should correspond to different content sec ...

Angular 2 router generates incorrect URLpaths

When navigating through my routes, I encountered an issue with the following routing setup: const routes: Routes = [ { path: '', component : HomeComponent, children: [] }, { path: 'login', ...

Mobile page sliding mechanism

My website contains a div that is mostly off the page, but on hover it translates onto the main page. You can check out my website. However, this method doesn't work well on mobile devices. Hovering is not effective and I often have to click multipl ...

Select the small image to reveal the larger overlay image

I have a grid of images and I want each image to expand into fullscreen when clicked. However, the JavaScript I am using is causing only the last image in the grid to overlay. How can I resolve this issue? Here is the code I am currently using: http://js ...

Information about the HTML detail element in Angular 2 is provided

Hi, I'm curious if there's a way to know if the details section is open or closed. When using <details (click)="changeWrap($event)">, I can't find any information in $event about the status of the details being open. I am currently wor ...

Incorporate pug and sass into your React project for a more

Are pug/jade and sass compatible with react? I enjoy organizing my code by separating files, and the functionality that sass and pug provide is great for this purpose. Is there a way to integrate pug and sass into a react project? Pug example: doctype ...

Reorganizing the layout of grid items

Currently, I am working on a task involving GRID CSS. The main objective of the task is to utilize GRID specifically, rather than other methods like flex. I have successfully set up two columns in my GRID, each with its own divs. However, what I am struggl ...

Unlimited requests sent to the server while subscribing to an observable on HTTP

I am currently enhancing an Angular2 website with a feature to display the name of the user who is logged in. While I have been successful in retrieving the necessary information from the back-end service, I am encountering an issue where requests are sen ...

Do we need to include href in the anchor tag?

Why am I unable to display the icon within the <anchor> element without using the href attribute? The icon only appears when I set the href attribute to "". Even if I manage to show the icon by adding href="", adjusting the size with width and height ...

How can HostBinding be used to target a custom directive in order to deliver either a success or error message and show it on

I am incorporating a custom directive to display specific server messages/errors following an http request. For example, in the response or error section, I want to target the custom directive and present the emphasized message. The directive is already e ...

Concealing the path of a file input in CSS

Similar Question: How to Conceal Input File Path in HTML File Upload Hello there! I was wondering if it's feasible to conceal the input file path and only display the browse button? Thanks a lot! Lucas ...

Creating a visual that when clicked, reveals an enlarged version at a different location

Is there a way to make an image appear in a different location on the page when it's hovered over? I've searched online but couldn't find a solution using just HTML and CSS. Does anyone know how to achieve this effect? Image not hovered: ht ...