The CSS class name is not having any impact on the React.js components

Struggling with CSS integration in ReactJS? That's the issue I've been facing while working on a project involving Rails and ReactJS. Despite implementing styles in my JSX files, nothing seems to change visually. Take a look at what my App.jsx file contains:

import React from "react";
import styles from "./styles.css";

export default class Register extends React.Component {

  render() {
    return (
      <div className={styles.container}>
        <h1> this text should appear to the right </h1>
      </div>
   );
  }
}

In my accompanying styles.css file, the code looks like this:

.container {
    width:40%;
    text-align:right;
}  

Despite using webpack as well, it seems like my CSS isn't being applied correctly on the JSX components. It has been quite a struggle trying to find a solution, even after searching for answers online. Could anyone shed some light on why the CSS styling is not reflecting in my React components?

As an additional reference point, here is how my "config/webpack/development.js" file is structured:

process.env.NODE_ENV = process.env.NODE_ENV || 'development'

const environment = require('./environment')

module.exports = environment.toWebpackConfig()

Answer №1

The way in which the classNames are loaded depends on the settings of the webpack loader. If you are utilizing thecss-loader configured in react-scripts (specifically as of version 1.1.5), then the classNames will be loaded with the option{modules: false}, meaning they are global styles that can be referenced as strings in JSX code:

import "./styles.css";
... className="container" ...

Alternatively, you have the option to load local styles using the following CSS-file syntax:

:local .container {...

You can also adjust your webpack.config.js accordingly (refer tohttps://github.com/webpack-contrib/css-loader#scope for detailed documentation on the various available options).

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

How can I resize and horizontally align an image using html/css?

Is it possible to resize, crop and center an image using only HTML/CSS? (Using the img tag or CSS sprite) For instance, if I have a 500x500 pixel image, I would like to resize it to a 250x250 pixel image To make the actual visible image 100x100 pixe ...

"Attempting to dynamically include Components for SSR bundle in React can result in the error message 'Functions are invalid as a React child'. Be cautious of this

When working with my express route, I encountered an issue trying to pass a component for use in a render function that handles Server-Side Rendering (SSR). Express Route: import SettingsConnected from '../../../client/components/settings/settings-c ...

Why won't my Twitter Bootstrap navigation bar apply the styles?

I am currently working on a Rails app (4.0) and have incorporated Bootstrap 3 into my project. Both bootstrap.css and bootstrap-theme.css are stored in the stylesheets directory, while bootstrap.js is located in the javascripts directory. Most of the Boots ...

Item on the menu has been pushed lower in the list

There seems to be an issue with the layout of my menu as one item is displaying lower than the others without any clear reason. <html> <head> <title> My Website Homepage </title> </head> <body> ...

Update the information displayed on the navbar upon logging out and logging in with a different account

Objective : I am trying to display the first name and last name on my Navbar. To achieve this, I am making an axios request using the user ID. Update: Thanks to @Isaac, I no longer encounter an infinite loop when utilizing componentWillUpdate(). Challeng ...

How come I am able to send back several elements in react without the need to enclose them in a fragment

This piece of code is functioning properly in React, displaying all the names on the page. However, I am questioning why it is returning multiple elements as a React component. Shouldn't they be wrapped in a single fragment? function App() { const n ...

How to retrieve the first option selected in Material-UI React?

Hey there! I am currently working with React Material UI select. I have a question - how can I set the first option of items as selected without triggering the onChange method? When I change an option, it triggers the onChange method and adds an attribut ...

Adding link styles to the <a> tag in Bootstrap 4 Alpha 3: A simple guide

The following code is quite simple: <a>This is a link</a> In a demonstration of Bootstrap 4 Alpha 2 here, the link appears blue. When you hover over it, it turns dark blue. However, in a demonstration of Bootstrap 4 Alpha 3 here, it displays ...

When consecutive DOM elements are hidden, a message saying "Hiding N elements" will be displayed

Provided a set of elements (number unknown) where some elements should remain hidden: <div id="root"> <div> 1</div> <div class="hide"> 2</div> <div class="hide"> 3</div> <div class="hide"&g ...

Tips for implementing rtl layout using material-ui next in a react application

Attempting to implement an rtl layout in my react application using the next version of material-ui. I've utilized the code below to achieve this, however, some components are not responding as expected under the rtl layout. /** * App.js Layout Star ...

Adding a div element to a React component with the help of React hooks

I'm currently diving into the world of React and experimenting with creating a todo app to enhance my understanding of React concepts. Here's the scenario I'm trying to implement: The user triggers an event by clicking a button A prompt app ...

The Chrome browser allows interaction between two separate divs, where a button located in one div can impact

One issue I encountered involves a button located within a div that has unintended consequences for another div. Here is the basic structure of the elements: <div> <div> <div> <div> <butto ...

The Bootstrap div is struggling to maintain its width on smaller screens

My attempt to create 3 inputs with the following code: <div class="col-sm-5 col-md-6"> <form> <div class="form-row"> <div class="col-0.5 search-label"> <label class="control-label">Search :</lab ...

Urgent issue: a dependency request is an expression in Next.js

I am facing an issue with my multi-language site that uses i18next. Whenever I try to transition between pages, it takes too long and sometimes even refreshes the entire page. The console shows this warning: warn - ./node_modules/next-i18next/dist/common ...

Utilize CSS to position an image in four various locations on a web page

Expressing this may be a challenge, but I'll give it a shot. Within a div, there is text that can vary in length based on user input. My goal is to have the ability to insert an image in one of several positions, determined by the selection made in a ...

Ways to reset a JQuery/JavaScript filter

I am currently working on a list of div items that are being filtered and sorted using JavaScript. Each item animates as it enters the screen, scaling up to its final size. However, when I apply filters using buttons, items that are already displayed do n ...

A guide to accessing the innerHTML of a div using React

My current setup involves creating an editable-content field as shown below const Input = () => { const Enter = () => { ... } const Editable = () => ( <div className={"editable"} contentEditable={"true"}> This i ...

Automatically change a text based on its location

Currently leveraging the amazing flexible-nav to showcase the current subtopic within my post. I am also considering the possibility of extracting this current-string and showcasing it at the top of the page in my main navigation bar. This way, the text w ...

Modifying button appearance based on state change in AngularJS

I have a button with a grey color and I want to create two different states for it: one that is selected (blue) and another that is in an idle state (grey). Currently, I am working on Angularjs but I am fairly new to this platform. Could you kindly provid ...

Stopping an endless loop when retrieving data from localStorage in next.js

While working with fetching data from localStorage in the useEffect Hook, I encountered an interesting behavior. When I set the dependency array to [] (blank), the data is fetched successfully but new data is not retrieved until the page is refreshed. Howe ...