Unable to import global CSS in React; however, local components are working fine

My React application is having trouble loading global CSS styles. While local components are able to access their own styled-components, the global CSS styles are not being applied across all components. I have tried various import paths and different files, including using require(path), but nothing seems to work.

src
--modules
----component1
----component2
----asset
---------global.css
--index.js

global.css

.test-class{
    font-size: 100px;
    color:red;
}

In index.js

import './modules/assets/global.css'
render() {
return (<div className="test-class">Hello World</div>)}

However, when I use styled-components, it works fine.

Below is my webpack configuration:

module.exports = {
entry: ['babel-polyfill', './src/vendor.js', './src/index.js'],
  plugins: [
    new HTMLWebpackPlugin({
      template: 'src/index.html'
    }),
    new MiniCssExtractPlugin({
      filename: isDev ? '[name].css' : '[name].[hash].css',
      chunkFilename: isDev ? '[id].css' : '[id].[hash].css'
    })
  ],
module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ['babel-loader', 'eslint-loader']
      },
      {
        test: /\.ts[x]?$/,
        exclude: /node_modules/,
        use: ['babel-loader', 'awesome-typescript-loader']
      },
      {
        test: /\.s?[ac]ss$/,
        use: [
          'style-loader', // : MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              importLoaders: 1,
              modules: false
            }
          },
          {
            loader: 'postcss-loader',
            options: {
              path: '/postcss.config.js',
              sourceMap: true
            }
          }
        ]
      }
    ]
  },
}

Answer №1

If you're looking to access the styles from global.css, make sure that you are importing the correct file in your index.js. Ensure that you import the right file and everything should work fine.

Your import statement should look like this:

import './modules/assets/global.css';

It appears that webpack is configured correctly, but for added assurance, consider creating a rule specifically for CSS files since there doesn't seem to be any SCSS or similar being used:

{test: /\.css$/, loader: 'style-loader'},
    {
        test: /\.css$/,
        loader: 'css-loader',
        options: {
          importLoaders: 1,
          modules: false
        }
    },

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

Having trouble getting the useState hook to work properly in a Next.js application when using a

Having trouble with the useState hook. I've tried to find a solution by searching through various forums like https://github.com/vercel/next.js/issues/7626, https://github.com/vercel/next.js/issues/17592, and also came across discussions on Stack Over ...

Tips for aligning column components in a vertical position

My task is to align vertically the elements within multiple columns that include a headline, a description, and a button. By default, each element expands based on its content size https://i.stack.imgur.com/RJJP4.png However, I need all elements to be al ...

Angular Material Sidenav fails to cover the entire screen while scrolling

https://i.stack.imgur.com/32kfE.png When scrolling, the Sidenav is not expanding to take up 100% of the screen and it continues to scroll along with the page content. <div layout="column"> <section layout="row" flex> <!-- siden ...

Create a reusable React component in Typescript that can handle and display different types of data within the same

I have a requirement to display four different charts with varying data types. For instance, interface dataA{ name: string, amount: number } interface dataB{ title: string, amount: number } interface dataC{ author: string, amount: ...

Tips on customizing the appearance of mat-card-title within a mat-card

Is there a way to truncate the title of a mat card when it overflows? I tried using the following CSS: overflow:hidden text-overflow:ellipsis white-space: nowrap However, the style is being overridden by the default mat-card style. I attempted to use mat ...

contenteditable -- Utilizing AngularJS to create a block element for the title only

When I click on an input field that is editable, I want the background color to change to white within the box. Can someone please assist me with this? Below is my code: HTML <div id="section{{section.index}}"> <h2 class="title" contentedit ...

Encountering the "Maximum Update Depth Exceeded" error in React Typescript with hooks

I encountered this error: Uncaught Error: Maximum update depth exceeded. It seems to be related to calling setState multiple times within componentWillUpdate or componentDidUpdate. React limits nested updates to prevent infinite loops. I am unsure of what ...

Error in Next.js: ReferenceError: document is not defined

Having trouble setting up a payment form for users to make payments, encountering the error message below: document is not defined Currently working with Next.js. Here is the code snippet I'm using: import React from "react"; import {Elem ...

Modifying radio inputs to update when a state variable is altered in react.js

In my HTML, I have a div that includes 4 radio inputs, and another div with an onClick event that increments a counter. Every time I click on the div, I want the radio inputs to reload so that no input is selected. Below is the code snippet: 'use clie ...

Issues with implementing drag functionality in a React and Next image carousel

Attempting to replicate a JS draggable slider in React/Next, but struggling to make it slide with mouse drag. Upon the first click, nothing happens, but when the mouse is released, the slider starts sliding and won't stop unless the page is reloaded. ...

Creating various containers in React JS for different components

I am faced with the task of rendering multiple DOM elements from my JavaScript code. Imagine I have div elements like this: <div id="div1"><div> //Some Html tags <div id="div2"><div> //Some Html tags <div id="div3" ...

Having trouble getting a React app integrated with WordPress on an NGINX server to function properly over HTTPS?

I am currently in the process of developing a React application that integrates with a WordPress site. The setup I am aiming for is as follows: When users visit example.com, they will be directed to my React app. If they navigate to example.com/blog, they ...

Has there been a recent issue with FireFox 3 where it fails to clear a float and does not recognize negative margins?

I've been searching online, but haven't come across a definitive answer yet. I'm interested in knowing if there is an issue with Firefox 3 when applying a negative margin to a div that is either clearing a float or containing another floated ...

The retrieval process takes place only one time or when I access the developer tools

I am currently developing a website where I need to retrieve reports from mongoDB and display markers on a map based on the locations of these reports. However, I am facing an issue where this functionality only works once when I initially open the website ...

Emphasize specific letters in a word by making them bold, according to the user

In my app, there is a search feature that filters data based on user input and displays a list of matching results. I am trying to make the text that was searched by the user appear bold in the filtered data. For example, if the user searches 'Jo&apos ...

Error encountered in typescript when trying to implement the Material UI theme.palette.type

Just starting out with Material UI and TypeScript, any tips for a newcomer like me? P.S. I'm sorry if my question formatting isn't quite right, this is my first time on Stack Overflow. https://i.stack.imgur.com/CIOEl.jpg https://i.stack.imgur.co ...

React Semantic UI - adjusting spacing on mobile screens with marginRight

I have implemented a web app using React Semantic UI and the Heroku React create app buildpack. However, I am facing an issue where there is always a margin on the right side of the fluid container. I checked Chrome's inspector but could not locate t ...

`How to perfectly place multiple text blocks and images using CSS`

Check out this screenshot to see the issue I'm facing: Here is the HTML code: <div class="parent_process"> <div class="first_process"><img src="images/exprimer-besoin-telephonie.png"/><span style="width:18%">You fi ...

Turn off Appbar padding at the top and bottom

I am looking to create a customized box within the Appbar that spans the full height, as illustrated in the image below: https://i.stack.imgur.com/CFMo0.jpg However, the default Appbar provides padding from all sides to its internal elements, leading to ...

Z-index behaving abnormally

I've been working on developing a website and one of the key features I'm trying to implement is a slideout menu that slides horizontally when the mouse hovers over it. To make development easier, I initially set the menu to be fully extended by ...