How to reference global Sass variables in a Sass module

My preferred method involves utilizing a global .scss file that houses all the foundational styles, such as primary color selections. Following this, I create separate modules for each React Component to maintain organization and reduce the need for importing excessive .css files.

Despite finding success with this strategy, an unexpected error has arisen.

Within my styles/global.scss file, I have defined the following:

$primary-color: #6dd3d6;

body {
    margin: 0;
    padding: 0;
}

In addition, I possess another file named NavBar.module.scss, featuring the subsequent content:

@import "styles/global";

.nav {
    display: flex;
    padding: 20px;
    background: $primary-color;
}

Examining my _app.js document reveals the following structure:

import React from 'react';
import '../styles/global.scss'

export default function App({ Component, pageProps }) {
  return <Component {...pageProps} />
}

Moreover, within my NavBar.js file, the ensuing code is evident:

import React from 'react'; 
import styles from './NavBar.module.scss';

export default function NavBar() {
    return(
    <nav className={styles.nav}>
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
            <li>Item 3</li>
        </ul>
    </nav>
    );

However, an issue surfaces with the appearance of the following error message:

./components/NavBar.module.scss:3:1
Syntax error: Selector "body" is not pure (pure selectors must contain at least one local class or id)

  1 | $primary-color: #6dd3d6;
  2 | 
> 3 | body {
    |                ^
  4 |     margin: 0;
  5 |     padding: 0;

Answer №1

The error message clearly states that using the body selector in a module is not allowed because it is not considered pure. To resolve this issue, create a separate Sass file dedicated to variables and then import it into your module or global Sass files.

In addition, it is incorrect to import the global.scss into any of the .module.scss classes as this will cause errors. The global style should be imported only once and does not need to be reimported.

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

HTML - Retain placeholder text while user inputs

My input is structured like this: <input value="My text" placeholder="Placeholder"> Typing in the input causes the placeholder text to disappear, which is expected. However, I am looking to keep the placeholder text visible as a background behind ...

What is the process for defining the root of a project in ESLint?

I've been working on a project using Next.js and Typescript. My imports look like this: import Component from "/components/Component/Component";, with the root directory being specified as /src. This setup works fine in Next.js, but ESLint k ...

Looking to set the default checked option for a radio button in antd?

Creating a Radio Button Group that displays data dynamically from an array. Check out the code below : <Radio.Group options={uniqueRadioElements} onChange={onCategorySelect} value={selectedCategory} optionType="button" buttonStyle="s ...

Achieve a scrolling effect for just a specific section of an HTML page

Hey there! I'm currently working on adding scrolling text along the side of a webpage for a specific section. My goal is to have three different sections, each with text that stops scrolling when you reach it and then transitions to the next section o ...

The Safari browser appears to be disregarding the positioning and size of input checkboxes, and also slightly shifting them after they are

After searching everywhere online, I still can't find a solution to this particular issue. I have a parent div that centers all elements, but for some reason the checkbox won't center in Safari. I've tried adjusting it using various methods ...

Render function in Next.js did not yield anything

Recently, I came across the next.js technology and encountered an error. Can anyone help me solve this issue? What could be causing it?View image here import React from 'react' import Button from "../components/button" function HomePa ...

Struggling with Bootsrap Flex and Justify-Content functionalities

I recently designed a footer with 4 columns, and the last column contains two rows of divs. I wanted the last column to be left-aligned, so I used justify-content-start for the first row, and it worked perfectly. However, when I tried applying the same sty ...

The mobile display does not support scrolling in Material-UI Drawer

I recently implemented the React Material UI library and integrated the Drawer component as a side-bar menu in my project. However, I encountered an issue where the Drawer component does not support scrolling on mobile devices. You can check out the websi ...

The next.js code is functioning properly when run in development mode, but encounters issues when attempting

When using the useAddress() function in run dev, it is returning undefined undefined and then the address when console logged. However, in the run build/start, it only returns undefined. What steps should I take to resolve this issue? import { useAddres ...

The HTML footer designed with bootstrap is not behaving as expected. It is not staying fixed at the bottom of the page and is not displaying at full width despite my coding efforts

Working on a small Sinatra app with bootstrap for layout, I'm encountering some issues. The layout is not sticking to the bottom and not displaying at 100% width as intended. Additionally, the footer text is not centering even after applying the text ...

Issue with vertical cell alignment in MUI x-data-grid persists following the latest update to version 7.2.0

After updating my MUI app to the latest npm packages version, specifically upgrading x-data-grid from 5.17.9 to 7.2.0, I encountered an issue. In my application, I utilize a grid where certain columns are populated using the renderCell property: const cel ...

Adjust the width of a DIV based on the content of other DIVs, and float and center them all

Initially, what seemed like a simple task has turned out to be more complicated than I anticipated. Here is my code: <html> <head> <style> div { border-top: 1px solid black; margin: 10px; } div#all { border: 0; } </style> <body ...

Static list elements are utilized in the CSS drop-down menu

It may appear that my coding skills are lacking or incorrect. When I attempt to create a drop-down menu in CSS, the new list elements end up on the other side of the page instead of within the container box. How can this be fixed? Below is the code snippe ...

The presence of concealed cells within an HTML table is leading to an increase in the

I am currently working on an HTML Table that serves as a summary table for displaying a list of items. The table has 4 visible columns out of approximately 20, with the remaining columns set to display: none;. However, the hidden cells are causing the rows ...

What is the reason behind H1 tags not requiring a class or ID in CSS?

As I was reviewing my CSS code, I noticed that the h1 tag is defined like this: h1 { .... } Unlike everything else in my code, which either has an id "#" or a "." class preceding it. This made me wonder why header tags don't require this. Could it b ...

What is the functionality behind a free hosting website?

Is anyone familiar with websites like Hostinghood, where users can create a subdomain and upload HTML, CSS, etc.? I'm curious about how they operate and how I can create a similar site. This is my first question here, so please respond instead of disl ...

How can I modify the value of a CSS animation rule in AngularJS?

I am looking to dynamically change the value assigned to stroke-dashoffset based on a custom input. @-webkit-keyframes donut-chart-1 { to { stroke-dashoffset: 100; } } @keyframes donut-chart-1 { to { stroke-d ...

The system encountered an error while trying to access the property "slug

I am currently working on a project that involves using next.js and wordpress. I have set up my api.js file, but I encountered an issue when trying to access the [slug] property from another component. The error message displayed is "Typerror: Cannot read ...

Tips for perfectly centering a SPAN element within a DIV both vertically and horizontally

Here is an example of my HTML code: <div id="slideClick"> <div style="padding: 0; margin: 0 auto; width: 100%; height: 100%; text-align: center; border: 1px solid blue;"> <span class="sideMsg">MESSAGES</span> </d ...

Utilizing multiple classes in HTML for a button element

  I've recently come across the concept that elements can have multiple classes. It's interesting, isn't it? .rfrsh-btn { background-image:url(../../upload/rfrsh_nb_grey.png); ... } .submit { font-size: 0.85em; paddi ...