I am unable to utilize folder paths in CSS within my React application

Having trouble setting a background image in CSS within my React app. When styling and setting the image from a React component, it works just fine. Can anyone provide assistance?

This code snippet will work:

const style={
    width:'300px',
    height:'200px',
    backgroundImage: 'url(pictures/backgroundPics/1.jpg)'
}

const Test =()=>(  
    <div style={style}/>
)

However, this won't:

import './test.css'

const Test =()=>(  
    <div className='test'/>
)

CSS:

.test{
    background-color: gray;
    width: 300px;
    height: 200px;
    background-image: url(../../public/pictures/randomPics/1.jpg)
}

I've attempted various ways to set my CSS path, including relative to the CSS file or HTML file, but I keep encountering errors like "module not found" or parse errors. My Test component's path is src/test/test.js and my CSS file is in the same test folder. I am using css-loader v0.28.4.

It does work if I use a web address as the URL:

background-image: url(http://www.pvhc.net/img226/gzypyldoqaxjhvtyyhei.png)

https://i.stack.imgur.com/nGe3K.png

Answer №1

You do not have to specify the relative path of the public folder in your CSS file. Have you experimented with this instead:

background-image: url(/images/randomImages/1.jpg)

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

"Linking a Next.js application with Azure's Application Insights for advanced insights

Trying to include my Next.js (TypeScript) app logs in Azure Application Insights has been a bit challenging. The documentation provided poor guidance, so I decided to follow this solution: https://medium.com/@nirbhayluthra/integrating-azure-application-ins ...

Setting up Authorization for FETCH requests in NEXT.js using .env.local

`export default function reservations() { let [reservationStock, setReservationStock] = useState([]); useEffect(() => { fetch(url, { headers: new Headers({ Authorization: "MyBasic Credentials", " ...

Create a hover effect on HTML map area using CSS

Is it possible to change the background color of an image map area on hover and click without using any third-party plugins? I attempted the following code: $(document).on('click', '.states', function(){ $(this).css("backgro ...

The modifications I apply to my Next.js code are not reflecting on the screen as expected

After setting up a new project using npx create-next-app@latest, I encountered an issue while editing the files layout.js and page.jsx within the /app directory. Despite making changes to these files while the web server was running (using npm run dev), th ...

When Typescript Encounters Compilation Errors, Treat Them as Warnings Instead

After using npx create-react-app my-app --typescript to create my app, I want to set it up in a way that allows me to compile it even when there are typescript errors, so I can address them later. I couldn't find any compilerOptions for this. Is ther ...

The ternary operator's logic seems to be malfunctioning when dealing with boolean values, consistently returning false even when the condition is true

My task involves creating a series of footer links for various projects, with each project name followed by a "|" except for the last one. Below is the code snippet that I am working with. const items = [ { lastItem: false, itemName: "Kuma&q ...

Manipulating state in React within a map function

I am struggling to understand the issue described below. The problem is related to the asynchronous setState aspect. I typically use callbacks, but they don't seem suitable in this case. My objective is to create a sortable state by iterating over d ...

Background image that covers the entire page

I am using background-size:cover to ensure that the entire background positioning area is covered by the background image. However, I've noticed that setting it to "cover" sometimes cuts off parts of my background image. Is there another way to achiev ...

What might be preventing me from running Webpack with React?

I've been struggling to set up webpack with React for a few days now. Even after following multiple tutorials, I can't seem to get it installed properly. Most recently, I followed this tutorial, but when I try to run webpack, I encounter an error ...

Why won't my Nivo Slider images print? How can I make my Nivo Slider images appear on printouts?

I am having trouble printing the nivo slider (default-theme): Despite trying for a few days, I still can't figure out why it's not working. I have attempted solutions like #slider { visibility: visible; width: 950px; height: 35 ...

Image not found in PDF created from HTML to PDF conversion

I am currently utilizing the HTML-pdf library to convert an HTML page into a PDF format. While the dynamic content is being displayed in the generated PDF, there seems to be an issue with the image not appearing as expected. Despite trying various methods, ...

Click on the "Get Value" button within the material-ui/core/Slider component

Project - using React framework. Objective: Create a line with dots representing various percentages between 0 and 100. The data comes from an external source (for this example, I have used a static array). The quantity and values of the points are dyn ...

Formatting rapidAPI data correctly on a React frontend: Best practices for optimal data presentation

I am currently working on a project utilizing Node, Express, and React where I need to integrate an API from RapidAPI. At the moment, the data is only being rendered in JSON format. How can I retrieve the data from the server coded with Node/Express and di ...

Is there a way to invert the order of elements in a scrollable container in Google Chrome?

The code snippet provided creates the desired effect, but unfortunately, it only seems to function properly in Firefox and Edge. In Chrome, while the elements are stacked correctly, there is no horizontal scroll bar, making the rightmost items hidden and i ...

I cannot seem to get the image to display properly, it keeps getting cut off and

I am trying to add an image to my website: https://i.sstatic.net/giAdT.jpg However, when I try to do this, the full image is not showing: https://i.sstatic.net/VRBlB.png This is the code I am using: <!DOCTYPE html> <html lang="en"> ...

The hintText property in Material UI's Chip-input component seems to be malfunctioning

I am using a ChipInput component from here. I need to display a hint text when the input is empty. <div className={classes.headline}> <ChipInput hintText='Type anything' onChange={this.handleTagChange} /&g ...

implementing a sidebar menu using AngularJS

I am currently working with bootstrap and AngularJS to create a sidebar menu. Here is the code I have for it: <ul class="nav navbar-nav"> <li><a href="#"><span class="glyphicon glyphicon-send"></span> Link</a& ...

Retrieving data in [slug].js using Reactjs

I am currently working on a project in Reactjs utilizing the "nextjs" framework. I have successfully managed to retrieve data (specific blog details) based on the slug([slug.js]). However, I now need to display data from all other blogs within the same c ...

Material-UI: Highlighting the selected item in SelectField

I am integrating a SelectField with approximately 30 options into a mobile application. While everything is functioning well, the challenge lies in the fact that it is used for rapid data input. Due to the constraints of the mobile screen size, only the fi ...

Hover over two different divs with JQuery

I have a situation where I have two HTML table rows. When I hover over the first row, I want to display the second row. However, once the mouse leaves both rows, the second row should be hidden. Is there a way to achieve this using JQuery? <tr class=" ...