Adding CSS styles to a React JS application

I am encountering an issue with importing a .css stylesheet in a basic react app. I have the following components:

AvatarHeader.js:

import styles from './AvatarHeader.css';

export default function AvatarHeader({ style, children }) {
  return (
    <div>
      <img style={styles.image} src={Background} alt="background">
        { children }
      </img>

AvatarHeader.css:

.image {
   width: 400
}

Both AvatarHeader.js and AvatarHeader.css are located in the same folder.

package.json:

  {
    "name": "UtWeb",
    "version": "0.1.0",
    "private": true,
    "devDependencies": {
      "css-loader": "^0.25.0",
      "react-scripts": "0.9.0",
      "webpack": "^2.2.1",
      "webpack-dev-server": "^1.9.0"
    },
    "dependencies": {
      "react": "^15.4.2",
      "react-dom": "^15.4.2",
      "react-redux": "^4.3.0",
      "react-router": "^2.0.0",
      "redux": "^3.2.1",
      "react-router-redux": "^4.0.0"
    },
    "scripts": {
      "start": "react-scripts start",
      "build": "react-scripts build",
      "test": "react-scripts test --env=jsdom",
      "eject": "react-scripts eject"
    }
  }

The styles are not being applied to the component, but it works if I modify AvatarHeader.js as follows:

  import styles from './AvatarHeader.css';

  const image = {
    width: 400
  }

  export default function AvatarHeader({ style, children }) {
    return (
      <div>
        <img style={image} src={Background} alt="background">
          { children }
        </img>

I am unsure how to resolve this issue and make use of the initial method of importing the css file.

Answer №1

import './AvatarHeader.css';`
<img className='image' />` 

This is the accurate syntax to use.

Answer №2

To update the CSS file name, you must change it from AvatarHeader.css to AvatoarHeader.module.css. Next, adjust the import statement to:

import styles from './AvatarHeader.module.css'
. This should resolve the issue. Check out the documentation for more information.

Answer №3

If anyone happens to encounter this issue in the future, it is important that the stylesheet adheres to the specific module naming convention for the original syntax provided by the OP to function correctly.

By simply renaming the file to AvatarHeader.module.css, the React code below should now execute properly:

import styles from './AvatarHeader.module.css';

export default function AvatarHeader({ style, children }) {
  return (
    <div>
      <img style={styles.image} src={Background} alt="background">
        { children }
      </img>

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

A guide to playing a series of audio files in succession using the Ionic Media plugin

I have been attempting to create a playlist of multiple audio files using the Ionic media plugin from here. However, I am struggling to achieve this without resorting to using a timeout function. Here is my current approach: playOne(track: AudioFile): Pr ...

I'm looking to implement a feature in my notes application built with the MERN stack using Mongoose for MongoDB that allows users to add hidden notes. How

I am looking to implement a hidden notes functionality in my web application. When the "hide note" button is clicked, I want the note to be removed from its current location and added to a hidden notes section. Users should then have the ability to view an ...

The height of the iframe with the id 'iframe' is not functioning as expected

I am trying to set the iFrame's height to 80% and have an advertisement take up the remaining 20%. Code <!DOCTYPE html> <html lang="en"> <head> </head> <body> <iframe id="iframe" src="xxxxxx" style="border: ...

Modifying the maxHeight property of the angular-gantt component does not yield any noticeable changes

I am currently experiencing issues with dynamically changing the height using the angular-gantt library. Despite setting a new value for the maxHeight attribute in the controller, it does not reflect on the view as expected. I have seen this feature work i ...

Steps for placing an image within the boundary of a rectangular div

My div has a width of approximately 730px and a height of 50px. I am looking to place an image at the bottom of the div, exactly where its height ends - after 50px. I have been considering a query to append the image at the bottom. One solution could be ...

Tips for sending hidden variables created dynamically with Jquery

I recently developed a dynamic table for my project. You can check out the DEMO here. The dynamic table is nested within a div called 'box' in a form. <div id="box"> </div> To save the dynamically generated data, I am using Jquery ...

Identifying and Blocking Users from Accessing External Domains Outside of the Angular Application

I am working on an angular application and I need to implement a feature where I can detect when a user navigates outside of the app domain from a specific component. For instance, let's say the user is on the upload component processing important in ...

What is preventing me from assigning all padding values at once?

I attempted to modify all paddings of the html element using JavaScript, with the following code: const pd = 1 document.getElementsByClassName('board')[0].style.paddingRight = pd + 'px' document.getElementsByClassName('board') ...

Navigating to a specific page within a function using React-Router in React - A complete guide

In my react application, I have a form that utilizes Bootstrap and react-router. Upon submission of the form, I want to direct the user to another page if the credentials are correct, otherwise do nothing. <button class="btn btn-primary font-weig ...

Adjust the size of the textarea to accommodate the number of lines of text

<script type="text/javascript"> function AutoGrowTextArea(textField) { if (textField.clientHeight < textField.scrollHeight) { textField.style.height = textField.scrollHeight + "px"; if (textField.clientHeight ...

Page is missing images and product details being displayed

I have been diligently working on creating a product page for my wife’s jewelry site. Following a PHP tutorial, I implemented the code and attempted to run the page. Unfortunately, I encountered an issue where none of the images, including the header and ...

Pinia accessing data from one getter to another

I manage a shop in Pinia (using Vue 3) that frequently utilizes getters referring to other getters. I've been using state.getterName for this purpose, which has been functioning well, although occasionally it warns me that the getter does not exist. I ...

Achieving Center Alignment of Two Elements with Varying Widths in Relation to the Parent Container, all while maintaining Left Alignment and consistent indentation

I am currently working with Material UI, but maybe this is just a matter of understanding how to achieve it with Flexbox. Something similar to what is shown in the image below. I am struggling to make all three lines align in the same way on different scre ...

Struggling to configure Sass with @snowpack/app-template-react-typescript

I'm struggling to integrate Sass with the @snowpack/app-template-react-typescript template. I attempted to follow the steps outlined in this guide, but so far I haven't been successful. I even created a new project and tried adding it, but not ...

The process of converting an HTML Table into a secured PDF file using JavaScript

I am looking to export data from an HTML table to a PDF file using React JS or pure Javascript, with the requirement that the PDF file must be password protected. While researching, I found libraries such as jspdf, node-qpdf, and jsreport-pdf-password bu ...

The Google Charts Pie chart is shown as displaying "Other" at 100% instead of specific data

I've encountered an issue while using the Google Charts API to create a dynamic pie chart. Initially, everything was working smoothly as the chart displayed each segment accurately. However, upon adding more data, the pie chart started showing only on ...

What is the process of incorporating a video into a react.js project through the use of the HTML

I'm experiencing an issue where my video player loads, but the video itself does not. Can anyone shed light on why this might be happening? class App extends Component { render() { return ( <div className="App& ...

Checking forms for standard regulations with jQuery

Within my project, I have implemented multiple forms, where each form shares common fields like email, but also contains its own unique fields such as uniqueFieldA for Form A and uniqueFieldB for Form B. The challenge at hand is to develop a set of valida ...

Retrieve the image (via copy and paste) directly from the web browser

When it comes to copying images in a browser like chrome, there are two methods available: copying the image itself and copying the address of the image. If I copy the image address and paste it using my Paste Image button, I can successfully retrieve the ...

Receiving a null value while using Enzyme's shallow renderer

Setting up tests in my create-react-app using jest(preconfigured) and enzyme. I have included the enzyme-to-json serializer in package.json. "jest": { "snapshotSerializers": [ "enzyme-to-json/serializer" ] } H ...