Next.js experiencing issues with applying styles from .modules.scss files

Recently, I initiated a new Next.js 13 application,

Within this project, there exists a file named main.modules.scss

.heading {
    font-size: 4rem;
    color: #000;
}

.dark .heading {
    color: #fff;
}

My intention is to utilize this file for styling a specific component as demonstrated below

import styles from "@/styles/main.module.scss";

export default function Home() {
    return (
        <>
            <Header />
            <h1 className={styles["heading"]}>Hi! I was styled by scss</h1>
        </>
    );
}

Upon implementation, it has come to my notice that the styles defined within the .heading class are being correctly applied. However, the properties specified under .dark .heading seem to be ineffective.

It should be noted that my theme provider automatically assigns a .dark class to the HTML element.

An alternative approach involved utilizing a regular .scss file and assigning a class like so

    <h1 className="heading">Hi! I was styled by scss</h1>

Remarkably, this method yielded the desired outcome without any issues

Answer №1

If you want to indicate that you are using a global class, you can use the :global selector.

For example, your main.modules.scss file would look like this:

.heading {
    font-size: 4rem;
    color: #000;
}

:global(.dark) .heading {
    color: #fff;
}

How does this method function?

Please note: I do not claim to be a CSS expert

CSS modules are confined to the components they are assigned to.

This implies that by default, the .dark selector is transpiled and does not pertain to the globally defined .dark class.

  • Take a look at this post for more information on CSS modules
  • Refer to this blog post for insights into using the :global selector

Answer №2

If you want to experiment, give styles.heading a shot.

<h1 className={styles.heading}>Hey there! I've been styled using SCSS</h1>

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

insert a gap between two elements in the identical line

Is there a way to create spacing between two text fields in the same row? I have tried using margins, paddings, and display flex in the css file but haven't been successful. import "./styles.css"; import TextField from "@material-ui/cor ...

Understanding the React Login Flow with Google and Facebook Integration

I'm currently working on incorporating Google and Facebook login features into my React application. As of now, I have added two buttons (one for Google and the other for Facebook) along with their respective code snippets: <GoogleLogin classN ...

What is the best way to efficiently update the state of a nested object in React using UseReducer without losing any of the state values?

I am facing an issue in React with a "+" button that is supposed to increment the value of an object property called "Strength" by one. Instead of increasing the value, when I click the button, all state values disappear on my browser view of the app. I&ap ...

Top authentication approach for Ruby on Rails API-only server, paired with ActiveAdmin

I'm currently working on a project that involves developing the frontend in Next.js using the fetch API for requests, and the backend in RoR. The challenge I'm facing is setting up user authentication. After some research, I've come across f ...

Encountered a problem while setting up React app using npm

I've encountered an issue while trying to install a react app using npm. Even after downgrading my npm version to 3, I am still facing errors. My internet connection is strong as well. Thank you in advance for your assistance! https://i.stack.imgur ...

Unique solution: "Using CSS to ensure the overflow document always scrolls to the bottom

Is there a way in CSS to make a div with overflow: scroll always automatically scroll to the bottom along the Y-axis, regardless of its content? For example, like a chat thread that consistently displays the latest message at the bottom. I've encount ...

Hover functionality in autocomplete feature malfunctioning

My Autocomplete component relies on the selected value of the parent Autocomplete, and it's been working perfectly. Typing or hovering to select both work as expected. const [hoverd, setHoverd] = useState(false); const onMouseOver = () => setHoverd ...

Eliminate empty space in the table cell

I am working with a table that looks like this: <table> ... <td> <div class="btn-group btn-group-sm"> <button class="btn btn-info mini"><span class="glyphicon glyphicon-duplicate"></span></button&g ...

Tips on how to change an image tag into a CSS selector

I need to change this tag to a WebElemet using CSS Selector instead of Xpath. I attempted with Xpath as shown below: panelSectionTitle.find( By.cssSelector( "img.aw-layout-right[style='']" ) ) <img style="" class="aw-layout-right" height="2 ...

Code not functioning properly in Internet Explorer

In one of my JavaScript functions, I have the following CSS line which works well in all browsers except for IE (Internet Explorer). When the page loads, the height of the element is only about 4px. element.setAttribute('style', "height: 15px;") ...

What is the best way to position this grid container directly beneath the search box using absolute positioning?

<!DOCTYPE html> <html lang="tr"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Unique Starter</title> ...

The default theme has not been specified in Tailwind CSS

During my recent project, I utilized React, Next.js in combination with Tailwind CSS. In this project, I delved into styling customization by implementing react-slick for a specialized slider. To achieve the desired aesthetic, I made modifications to the r ...

Tips for transferring properties from one React component to another React component

I need to figure out how to activate a button in a modal when text is entered into an input field. The form is part of a different class and is utilized within a parent class. How can I pass an onChange method to my form component? Check out the code for ...

How to open a new tab in ReactJS

When attempting to open a component in a new tab using the Link in React router, it results in a 404 error page being displayed instead of the desired react component. The main React entry point file (js), import React from 'react'; import { re ...

Is there a way to instruct the code to select between two variables and align their values?

I have encountered a challenge while working on a unit test. The website I am testing is dynamic, causing the CSS Selector and xpath to frequently change. As a solution, I came up with the idea of using both CSS and xpath to retrieve the same text. Curren ...

What is the process for setting up redux in _app.tsx?

Each time I compile my application, I encounter the following error message: /!\ You are using legacy implementation. Please update your code: use createWrapper() and wrapper.useWrappedStore(). Although my application functions correctly, I am unsure ...

Does react-tap-event-plugin serve a purpose in the current year of 2018?

Does React still require the use of zilverline's tap event plugin or has it been integrated directly into React's source code, eliminating the need for a tap delay? ...

I am in need of help with coding so that my entire webpage is displayed properly even when I resize it to the smallest width. Additionally, I need assistance with making

Having some issues with making my personal website work correctly. I'm planning to share images of how it looks at different widths - largest width and smallest width. Also, here are the links to my website here and to my GitHub code here Seeking gui ...

``The presence of symlink leading to the existence of two different versions of React

Currently, I am working on a project that involves various sub custom npm modules loaded in. We usually work within these submodules, then publish them to a private npm repository and finally pull them into the main platform of the project for use. In orde ...

Error: The property 'insertAfter' of the variable is not defined and cannot be read

While attempting to launch the frontend of my project (built in next.js) using 'yarn dev', I encountered this error message: error - ./node_modules/next/dist/compiled/css-loader/cjs.js??ruleSet[1].rules[2].oneOf[6].use[1]!./node_modules/next/dist ...