CSS background image not displaying in ReactJS with SCSS

Currently, I am working on a React project and I am trying to incorporate a local image from a public folder as the background for a div container with transparent color. However, I am facing an issue where the image is not being displayed.

I have successfully implemented a similar method using an image from the internet (starting with 'https'). I have also attempted to use CSS to style the image, but unfortunately, it is still not showing up.

Here's the code snippet for setting the background with a local image (which is not visible):

background-image: url("/public/images/background.jpg");

I have also tried this approach:

background: #eaeaea urlurl("/public/images/background.jpg");

Meanwhile, the following code correctly displays the background using an image from the internet:


background: #eaeaea url("https://mdbootstrap.com/img/Photos/Horizontal/Nature/full page/img(20).jpg");

After some consideration, I believe the issue lies in the URL format ('url('/path/path/img.jpe)' - it should be corrected accordingly.


// Import Bootstrap and its default variables
@import '~bootstrap/scss/bootstrap.scss';

.btn{
    color: red;
}

.background {
    background: #eaeaea url("/public/images/background.jpg");
        background-size: cover;
    background-position: 50% 50%;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    }

Answer №1

Here is a suggestion for you to try:

background: url('/./images/background.jpg');

Hopefully this solution will solve the issue.

Answer №2

To set up your project correctly, start by generating an .env file within the project folder. Add the following line of code: SASS_PATH=node_modules:src. Don't forget to transfer all desired images into the src folder.

background-image: url("/background.jpg");

Answer №3

To enhance your background, consider incorporating an image into the background tag like so:

background-image: url("/assets/images/bg-pic.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

What is causing the discrepancy in functionality between these two HTML/CSS files with identical code?

In this codepen, you'll find the first example: Subpar Pen: https://codepen.io/anon/pen/jGpxrp Additionally, here is the code for the second example: Excellent Pen: https://codepen.io/anon/pen/QqBmWK?editors=1100 I'm puzzled why the buttons l ...

Dynamic row height in MUI DataGrid

I'm currently utilizing the MUI DataGrid component, and I have a specific behavior in mind that I'd like to achieve: When there is a small number of rows present, I want the table to only occupy the necessary space for those rows. On the other h ...

Stop SVG Text from Rotating using CSS

I designed an SVG circle containing text and now I would like to apply a CSS transform to rotate the SVG, but without rotating the text element. .progress { webkit-transform: rotate(-90deg); transform: rotate(-90deg); } .progress__value { stroke-d ...

Verify the existence of the email address, and if it is valid, redirect the user to the dashboard page

Here is the code snippet from my dashboard's page.jsx 'use client' import { useSession } from 'next-auth/react' import { redirect } from 'next/navigation' import { getUserByEmail } from '@/utils/user' export d ...

Achieving the desired results through the utilization of CSS3 rather than relying on images

I am currently exploring the use of CSS3 to create a menu instead of relying on images over at . You can view my progress and code (including images and styles) here: Although I attempted to use CSS3 for the border shadow and matching the background of ...

Design adaptable HTML backgrounds

Hello, I am working on a webpage that uses two background images. Here is the CSS I'm using: body{ background: url('<?=base_url();?>/assets/header_bg.png') no-repeat, url('& ...

Ways to display the data within a BLOB object

On this page, the user is showcasing a table with three columns - tipo_esame (string), data_esame (string), and uri (BLOB). const archiveItems = this.state.archive.map((archive, i) => { return ( <tr key={archive.hash_referral}> <td ...

Sequential execution of multiple useState updates when an update triggers a re-render

Can you explain why the function setPeople is being executed after setFirstName, setEmail, and after the event handler has been exited? const [firstName, setFirstName] = useState(''); const [email, setEmail] = useState(''); const [peopl ...

What is the reasoning behind placing the CSS file in the header section and the JS file at the bottom

Hey there, I need some help. Why is the CSS file included in the header section but the JS file at the end of the page? Can I also include the CSS file at the bottom of the page? <!DOCTYPE html> <html> <head> <link type= ...

React Nextjs implementation of a fixed navigation bar to stick at the top

Hello, I am experiencing some issues setting up a sticky navbar in Next.js. The current navbar doesn't seem to be functioning as expected. Is there anyone who can assist me with the code provided below? import React, { useEffect } from 'react&apo ...

The latest pathway fails to refresh in NextJs

I've implemented a search bar at the top of my app which directs to /search/[searchId].js page: <Link href={`/search/${search}`}> <button type='submit' className='btn-cta btn-3'>SEARCH</button> < ...

Data can be retrieved in a React/Next.js application when a button is clicked, even if the button is located in a separate

Whenever the button is clicked, my function fetches weather data for the current location. I am trying to figure out how to transfer this data from the Location component to the pages/index.tsx. This is where another component will display the data. This ...

What is the process for deleting a token from local storage and displaying the logout page in Next JS?

I developed a Next.js web application and am looking to display a logout page when the token expires, while also removing the expired token from local storage. How can I ensure that this functionality works no matter which page the user visits within the a ...

"Utilizing setState for updating state in React with functional updates within useCallback

Is it acceptable to use functional updates in useCallback without including the state or setState functions in the dependency array? If so, what is the reasoning behind this? For instance: const example = () => { const [number, setNumber] = useState( ...

Navigating with Next.js router.push() changes the URL but fails to actually display the page

My Next.js app is running on localhost:3000 and it redirects to a login page if there is no user data. However, when it redirects to the login page, it doesn't render and I encounter this error: Error: Objects are not valid as a React child (found: [o ...

The scroll animation feature was not functioning properly in Next.js, however, it was working flawlessly in create react app

I recently transitioned a small project from Create React App (CRA) to Next.js. Everything is working as expected except for the scroll animations in Next.js, which are not functioning properly. There are no errors thrown; the animations simply do not occ ...

Output the following by using the given format: *a* -> *a1**aabbbaa* -> *a2b3a2*

I am a beginner in JavaScript. Could you please explain how to achieve the following output? * "a" -> "a1" * "aabbbaa" -> "a2b3a2" I attempted using a hash map, but my test cases are failing. Below is the code I have writt ...

The error detected in the W3Schools validator pertains to CSS for audio elements that do not contain

Could somebody kindly explain why this code is being flagged as an error on w3schools? audio:not([controls]) { display: none; height: 0; } ...

Caution: There is a conflict in DefinePlugin with conflicting values for 'process.env.NODE_ENV'

Whenever I attempt to run development mode, I keep encountering the warning mentioned in the title. Although this script functioned properly for a previous website, it now consistently triggers this warning. Below is my package.json configuration: { &qu ...

What are the steps to fix the "Invariant Violation" issue that is linked to the redux store?

During my DOM testing to verify if a dialog box would open upon clicking a button, I encountered an error message: Invariant Violation: Could not find "store" in either the context or props of >"Connect(Photos)". Either wrap the root component in a , or ...