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

The div element should stretch to occupy the entire height of the screen

I have structured my webpage with three different divs. The left one is defined with col-md-2, the middle one with col-md-8, and the remaining right div with col-md-2. I want the right div to take up 100% of the screen height, even when no controls are pre ...

Retrieving CSS style values with Node.js

I am looking to design a new CSS style that includes the lowest and highest values. table[my-type="myStyle"] { width: 255.388px !important; } This code snippet is included in numerous CSS files within my style directory. ...

Tips for Personalizing Bootstrap 4.3 Through BootstrapCDN

I'm having trouble customizing the Bootstrap 4.3 BootstrapCDN by linking an external CSS file to override it. Even though I have placed the custom CSS file below the Bootstrap one, it doesn't seem to be taking effect. Can anyone explain why? Th ...

ReactJS: Unable to navigate to a new page when moving from a page with automatic refresh

Every 5 seconds, I automatically refresh a page (Page1) using setTimeout and a callback function. However, every time I navigate to a new page (Page2), it quickly redirects back to Page1 after a few moments. I have tried using window beforeunload event l ...

Which CORS strategy is most effective for making requests to external APIs?

I've set up Axios as my HTTP client to interact with 3rd party APIs, along with Express for the server and the cors package to handle CORS issues by adjusting the HTTP headers. However, I keep encountering the error message Failed to load https://api. ...

The error message "connectDragSource is not a function" is being displayed in ReactDn

I'm currently working on a top-level component that comprises several different components. InventoryBox - marks the area where an inventory is located InventoryList - displays the list of items within the inventory Item - represents a single item w ...

Converting images to greyscale or transparency within a ReactJS component

I am exploring ways to transform an image into grayscale or make it transparent before using it as a background in ReactJS. Is there a way to achieve this in ReactJS? My current code snippet is shown below: <GridListTile> <img style={{ -we ...

Issue: The data type 'void' cannot be assigned to the data type 'ReactNode'

I am encountering an issue while calling the function, this is just for practice so I have kept everything inside App.tsx. The structure of my class is as follows: enum Actor { None = '', } const initializeCard = () => { //some logic here ...

Vertical Alignment of Navigation Bar in Bootstrap 4

I am trying to align the center of my navigation bar using the "Cover" Bootstrap 4 template. Please find the current code provided below. <div class="cover-container d-flex h-100 p-3 mx-auto flex-column"> <header class="masthead mb-auto"> ...

Setting dynamic routes in Next.js when the basePath is not the root can be done by carefully configuring the routing system

After following a tutorial on setting up my Next.js project to be located in a subfolder (/app), everything was running smoothly. However, I encountered issues when trying to use dynamic routes for certain pages. I attempted three different approaches: ...

The color of the React Loader Spinner remains unchanged

I am currently working on a Next.js project where I want to implement React-loader-spinner with animation. However, I am facing an issue with the color of the animation not changing despite trying inline styling and custom classes. How can I resolve this? ...

Converting React-related node packages in the npmjs.com repository to ES5: A step-by-step guide

As a beginner, I am delving into the intricacies of React packages within the npmjs.com repository. I have noticed a curious pattern where some modules I install work seamlessly with my application (like react-motion), while others refuse to cooperate and ...

Focusing on the alignment of an article tag

I am currently facing a challenge in centering the content within an HTML article element. As of now, the content is aligned to the left margin of the page. Below is the snippet of HTML I am working with: <article> <h1>Sign In</h1> ...

The components for my children are not being displayed within the Drawer component in Material UI and React

Why are the Material UI components and other project components not displayed when I use my DrawerForm component? List of dependencies: react: 18.2.0 react-dom: 18.2.0 @amcharts/amcharts5: 5.3.6 @mui/icons-material: 5.11.11 @mui/material: 5.11.12 Code s ...

When utilizing Angular, the mat-datepicker is displayed underneath the modal, even after attempting to modify the z-index

I am encountering a problem with a mat-datepicker displaying below a modal in my Angular application. Here are the key details: Html: <div class="col-12"> <mat-form-field appearance="fill"> <mat-label>Start Date ...

Utilizing a JSDoc comment from an external interface attribute

Currently, I am in the process of developing a React application. It is common to want the props of a child component to be directly connected to the state of a parent component. To achieve this, I have detailed the following instructions in the interface ...

Decoupling the Top Navigation Bar from the Side Navigation Bar

I currently have a top navigation bar with some menus and a side navigation bar with additional menus. I have applied styling to the vertical navigation bar, but it seems to be affecting all the navigation bars, resulting in an output as shown in the image ...

The `margin:auto;` property does not function properly with inline-block elements

I recently adjusted my "container" div by changing it to an inline-block. Previously, I had applied margin:auto; to center it, which worked well with a specified width. Original Code (functional) #container { border: 1px solid black; height: 2 ...

Tips for Effectively Declaring a Variable with React's useState

How should I correctly specify variable types in useState? In the code below, the value for alert must be either "success","warning", "error", or "info" const [alertValue, setAlertValue] = useState("error" ...

ES6 component rendering is malfunctioning on CodePen

class PlayfulPaws extends React.Component { constructor(props) { super(props); }; render() { return ( <div> <DIV /> <JSX /> <aClassDiv /> </div> ); }; }; ReactDOM.render(&l ...