SassError: Variable not defined

I have a file containing variables with different themes that I need to override:

_vars.scss

body {
  $bg-color: #fff
}
body.theme-dark {
  $bg-color: #333
}

In my Angular button component, I am referencing these variables:

button.scss

@import '_vars.scss';

.button {
  background-color: $bg-color;
}

However, I encountered a compiling error:

SassError: Undefined variable.

background-color: $bg-color;

What is the correct way to overwrite variables based on the body theme class? Thank you!

Answer №1

When deciding on the value of $bg-color for your theme, remember to also consider the importance of setting $font-size-18.

Just a suggestion, you might want to explore using CSS Custom Properties instead of SASS variables in your coding journey. You can find an informative article on this topic at Codyhouse, which delves into color themes as well.

To delve further into customizing colors and themes, I recommend checking out this article.

Answer №2

Variables are only valid within the scope they are defined in, not universally. To address your question, consider the following:

_vars.scss

Instead of creating variables within the body scope, define them in the global scope.

$bg-color: #fff;

body {
  background-color: $bg-color;
}

Then, import it into your button.scss without the underscore "_":

@use "vars" as *;

button {
  background-color: $bg-color;
}

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

Is there a solution to address the issue I am encountering with my API, specifically the 404 error that

I am currently developing an application that retrieves codes from a mongoDB database and displays them on a web page. However, I am encountering an issue in the console: GET http://localhost:4200/api/code/codes/ 404 (Not Found) zone.js:2863 This is a n ...

Is there a way in Angular 1.5 to compile the HTML of a parent component from within a child component?

I have created two angular components called app-menuitem and app-menu. The app-menu component contains a list of app-menuitems as its children, without using transclude. App-menuitem Component: angular.module('app') .component('appMen ...

How can I resolve a route error after converting typescript to es5 in an Angular2 example?

I'm currently diving into the world of Angular2 and exploring how to convert TypeScript to ES5. You can find detailed instructions in this documentation: https://angular.io/docs/ts/latest/tutorial/toh-pt5.html Upon running the ES5 code, I encountered ...

Creating a dynamic Bootstrap carousel with a fixed hero header text inside a designated container - here's how!

I am currently working on a project to develop a responsive Bootstrap slider with a fixed hero header that remains consistent while the slider images change. The hero header needs to be aligned to the left within a responsive Bootstrap container and center ...

Unable to update markers on agm-map for dynamic display

In my Angular 5 application, I am utilizing Angular Google Maps (https://angular-maps.com/) along with socket.io for real-time data updates of latitude and longitude from the server. Although I am successfully pushing the updated data to an array in the co ...

Adjust the dimensions of the icon

My current project involves creating a sidebar with icons and associated text to represent each icon. However, I encountered an issue while trying to adjust the size of the icons within the sidebar using the "useStyles()" method. For some reason, the size ...

The content inside the inner div does not stretch to match the height of its parent element

I want to address a specific issue I am facing with my div structure. Although it may seem similar to other questions on StackOverflow, none of the existing solutions have worked for me. The problem is that I have two child divs inside a parent div. The h ...

Issues have been identified with the performance of Ionic Material List when used in conjunction with ng

In my project involving the Ionic floating menu button, everything is working fine. However, I have encountered an issue where when I click on the + button, I do not want to be able to click on the click me button while the menu is open (Req1.png)https://i ...

Triggering an event within a component to execute a specific function in another component

I am working on a component that includes multiple routes such as home, app, and navbar. My goal is to have the encrementcalc() function execute when the navbar button is pressed. I attempted to use the event emitter but was unsuccessful. Can someone prov ...

What are the steps to converting one grid to another solely with the use of Bootstrap utilities?

https://i.sstatic.net/SuGiM.png I am looking to convert the grid layout shown above into the grid layout displayed below. To achieve the above layout, I have used the following grid structure: <div className="row"> <div className=&q ...

Testing Angular Service Calls API in constructor using Jasmine Test

My service is designed as a singleton, and its constructor initiates an API call function. This simplifies the process during service initialization, reducing the complexity and dependencies on various components like AppComponent to import and execute API ...

Flex wrap is causing additional space within the layout

When using flex-wrap, if there are more textBoxes than the available width in the container, they shift to the next line. This is fine, but it leaves too much space between the button and the textBox. This spacing issue can make the layout look odd. I hav ...

Tips on connecting a font directory from FontAwesome

I am currently attempting to incorporate the fonts provided by FontAwesome for their icons, and they specify that a root folder named Fonts is needed. My attempt to link it was using: <link type="text/css" href="/myPath/font" /> ...

The incorrect sequence of Angular/Protractor functions is causing issues within the tests

Trying to extract the values from a column in a grid and compare them before and after sorting can be tricky. I have two functions set up for this task - one to retrieve the initial column values, and another to check the order post-sorting. However, there ...

Animating CSS when closing a modal box

I followed the instructions from a tutorial on W3Schools here to create this code. The "open model" button triggers the modal to open with a smooth CSS animation, which looks great. However, when I click the close button, the modal abruptly closes without ...

What is the optimal HTML tag for showcasing chat text conversations?

To create a text window with fixed width and length that automatically wraps long texts without a horizontal scroll bar but with a vertical one, you can use HTML and CSS. The preview window on SO's ask question page is a good example of this. It is si ...

Having trouble capturing emitted events from a child component in Angular 2+?

I have a question as a beginner. I'm trying to send a message from the child component to the parent component but for some reason, it's not working. app.component.html <app-cart-component> [items]="rootItems" (outputItems)=&quo ...

Error: No routes found for 'documents' in Angular 2 RC5

I'm currently in the process of upgrading my application to RC5 and have encountered some challenges. Within my app.routing.ts file, I've included the following: import { Routes, RouterModule } from '@angular/router'; export const ap ...

Difficulty in transitioning observable information between inherited components

Having a base component and two inherited components, each component retrieves data from its own service which is stored in the state. When attempting to use the following approach, the data does not switch correctly: get codeData$(): Observable<Codes.C ...

the webpage is not displaying the style sheet properly

I have set up a local instance of nodejs and am currently running a web service that references a local index.html file upon startup. This web service is operating on my desktop. After experimenting with CSS, I've encountered an issue where the style ...