Is there a method to incorporate scss into a project without requiring a separate stylesheet?

I am currently working on a Gatsby application that utilizes SCSS.

My code snippet looks like this:

import React from "react";
import styles from "./mosaic.module.scss";
import Tile from '../tile/tile';

const Mosaic = (): JSX.Element => (
    <section className="wrapper style1">
        <div className="inner">
            <h2 className="major">Smaller Projects</h2>
            <p>Here under follow minor projects, often unfinished; I show them anyway because they helped me widen my experience. :)</p>
            <section className={`${styles["features"]}`}>
                {[0,1,2,3,4,5,6].map(e => <Tile key={e}></Tile>)}
            </section>
        </div>
    </section>
)

export default Mosaic;

If I don't import the class from the module with className=${styles["features"]}, it won't work as expected.

However, you may notice that some classes like className="inner" still function properly.

This is because in gatsby-browser.js, I have imported some global CSS using:

import "./src/styles/global.css"

The issue here is the confusion between when to use the styled class and when to stick to the conventional method.

Is there a workaround to consistently utilize either the default styling or the styled approach for all elements?

Answer №1

To use standard global CSS, simply change the name of your file to mosaic.scss, import the file, and apply your classNames in the usual way.

import "./mosaic.scss";
...
<section className='features'>...</section>

If you opt for the *.module.scss extension, you are indicating to gatsby-plugin-sass that you intend to utilize CSS Modules.

To use CSS Modules, follow these steps:

import styles from "./mosaic.css";
...
<section className={styles.features}>...</section>

or

import {features} from "./mosaic.css";
...
<section className={features}>...</section>

Edit: Based on your clarification in the comments, it seems like gatsby-plugin-sass or CSS Modules may not be suitable for your needs. You might want to explore alternative tools for managing your css (e.g., stylable.io)

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

Best practices for managing useStates and useEffects in a Reactjs trivia game application experiencing double rendering

In my React quiz app, a random Korean hangul character is displayed from a set of 40 items in Data.json. Users are presented with 5 buttons containing different answers also sourced from Data.json. Just to clarify, all components in my application are sib ...

``Why is the default value not appearing in the Autocomplete set?

I'm having trouble setting a default value in my autocomplete field. I am fetching select options from the database with this code: let films = await db.collection("films").find().toArray(); This is the data that is returned: [ 0: { title ...

Error Alert: React Hook ReferenceError - Attempting to access 'variable name' prior to initialization - Issue detected in [React Hook]

Recently, I've started diving into the world of React. My current challenge involves building a table that holds an array of data. To populate this table, I'm utilizing the set method to extract specific values from a multi-dimensional array. For ...

The justify-content property is failing to properly align content along the main axis in Grid layout

I've implemented tailwind for my CSS styling. Within a simple div, I have 3 child elements. My goal is to create space between them along the main axis. Here's the code snippet in question: <div className="grid grid-cols-12 text-white ju ...

Is there a way to customize the color of the HR element in a Material-UI Select Field?

https://i.stack.imgur.com/DYeX7.png https://i.stack.imgur.com/CN0T6.png Hi there, I am currently working on a website and using a Select Field component from Material-UI. I am faced with the challenge of customizing the style to change the default light ...

Eliminate list items with a keyboard stroke

I am currently developing a straightforward todo list application using JavaScript. The main functionality I am trying to implement is the ability to add new items from an input field to a list, as well as the option to remove items from the list. While ...

Angular: Compilation of SCSS/SASS results in unexpected whitespace issues

I am encountering an issue with whitespace being added to the CSS after compiling a *.scss file in my Angular 7 project. This unwanted whitespace is causing incorrect results in the UI. To replicate the problem, you can visit... ...and copy and paste the ...

Avoid using dynamic classes when rendering in React.js

I'm facing an issue where applying a class conditionally does not work, but it works fine when I apply a static class name. const styles = theme => ({ labelsuccess: { "background-color": "#5cb85c" }, labelprogress: { "background-colo ...

Tips for refreshing the <img> tag using a user image

I am currently designing a bootstrap webpage and trying to implement a feature where the user can input an image, which will then be displayed in a preview modal along with some text provided by the user. The modal is functioning correctly. Here is the co ...

I'm looking for a way to securely transfer data, such as an authentication token, from a web application to an electron-based thin client. My current approach involves utilizing custom URL protocols to

Have you ever wondered how applications like Teams and Zoom produce a pop-up when clicking on a meeting link in the browser, giving you the option to continue in the browser or open in the app? When choosing to open in the app, it launches on the desktop a ...

Pictures glide within containers

Hey, I'm having an issue with my images. They're not floating like they should be, even though they are centered in the div. I really want them to float and be centered. Here's the code snippet I've been working with. HTML <div cla ...

Utilize the React.createContext() method integrated within the _app.js file

I need to retrieve the emailLogin function from my context in a different component: userContext.js import React, { useEffect, useState } from 'react'; import cookie from 'js-cookie'; import {firebase} from './firebase-client&apos ...

Using Javascript to deactivate a clickable image upon clicking another image

I have two buttons with alert functions assigned to each. I want the function on the first button to only work when that button is clicked, and not work if the second button has been clicked. Below is the code I've tried, but it's not functioning ...

Preventing the automatic selection of the initial item in a PrimeNG dropdown menu

While utilizing the p-menu component from PrimeNG in popup mode ([popup]="true"), I encountered an unexpected issue where the first item in the menu is automatically selected and turns gray. Here is the code snippet that I am using: <p-menu #menu [popu ...

Tips for incorporating in/out animations with a container for the Slide feature:

I need help creating a component that will slide to the right when mounted, and to the left when unmounted. The Slide component should be contained in a div with the class "profile-slide-container", but I'm unsure how to achieve this. Below is the co ...

What exactly does the use of type assertion as any in Typescript entail?

I'm attempting to dissect a portion of code and figure out its functionality. While I've encountered type assertion before, this particular example is proving to be quite baffling for me. (this.whatever as any).something([]); Here's the la ...

Class Component equivalent of React's useForm is to use the "useState

I recently transformed a functional component to a class component and am now unable to find the equivalent of useForm when utilizing a class structure. Previously, I had the following code: var commentField = form.getState()["active"].replace(new RegExp( ...

Safari not rendering SVG wave at full width reached

My SVG isn't scaling to 100% width in Safari like it is in IE, Chrome, and Firefox. Despite numerous attempts, I haven't been able to make my SVG stretch across the full width like it does in other browsers. The project is built using React. . ...

having difficulty carrying out the commands following the post request, without encountering any error messages

I am having trouble executing some instructions after making a post request using an async function that gets called on a button click. async function createPost() { try { await axios.post("http://localhost:3001/post", { content ...

Unlock the Power of Rendering MUI Components in ReactJS Using a For Loop

Hey there! Hope everything is going well. I've been working on a fun project creating an Advent/Chocolate Box Calendar using ReactJS. One challenge I'm facing is figuring out how to iterate over a for loop for each day in December and render it ...