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

using the useEffect hook to create a loop that runs infinitely

For my project, I am working on updating data in firebase. The issue that I'm facing is that the data seems to be constantly looping, causing potential crashes. Is there a way to stop this loop and prevent any crashing? import "./App.css"; ...

Vertical Orientation in HTML

Would appreciate some assistance on creating a vertical text with HTML similar to the example in the linked screenshot. It needs to be straightforward and vertically oriented. Check out the screenshot for reference ...

I do not have ESLint set up, so I can use JSX without needing 'React' to be in scope

In my research on the React documentation and Stack Overflow, I came across suggestions for fixing the error 'React' must be in scope when using JSX. Even after uninstalling ESLint from VSCode, I still encounter this issue. I developed a basic R ...

Creating if-else conditions in React can be achieved by utilizing the ternary

How can I make an if-else condition dynamic based on a button click? I have tried implementing it but it doesn't seem to work. I attempted to create conditions for different languages. For example, clicking the button labeled "Español" should print ...

Guide on developing a reusable field component within redux-form

I have a dilemma with reusing 2-3 fields in different forms within my application. I thought about creating these fields as components to make them reusable in other forms, but redux-form is giving me an error: Error: Field must be inside a component deco ...

Center align the division within the list item

Check out the fiddle provided here: https://jsfiddle.net/5jnnutg8/ I am looking for a way to center align and display inline the "something #" list items. While the title "Hi" can be centered using text-align: center in css, this doesn't seem to wor ...

Solve the issue of location dependency warning when using the React useEffect hook

I am facing an issue with my useEffect in a React component. I want it to only run the first time, but using location.pathname in the useEffect without dependencies triggers a warning: React Hook useEffect has a missing dependency: 'location.pathname& ...

The minmax() function is causing grid items to not wrap correctly

I have been working on creating a responsive web page where the "boxes" automatically adjust to a new row when the browser window is resized. To achieve this, I utilized the following CSS code: grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); ...

Having trouble integrating CanvasJS into a NextJS application

I have integrated CanvasJS into my NextJS app by placing the files canvasjs.react.js and canvasjs.min.js in the pages folder. I then imported them within a page like so: import React from 'react' import CanvasJSReact from './canvasjs.react& ...

Retrieving data from the database using getStaticProps in Next.js

As I was following a tutorial on Next.js, the instructor did something that deviated from what I had learned in school and left me pondering. Here is what he did: interface FaqProps { faq: FaqModel[]; } export default function Faq({ faq }: FaqProps) { ...

How to style text in CSS with a numbered underline beneath

Is there a way to apply underlining to text and include a small number underneath the underline in a similar style shown in this image, by utilizing css, html, or javascript? ...

Exploring the features of NextJS version 13 with the benefits

Starting from the 13th step, SSR is utilized by default and in order to opt for client side rendering you must specify it at the top like so: 'use client' Currently, my setup involves TypeScript and styled-component integration. Take a look at ...

Exploring the combination of Django templates and React in web development

Can Django templates be integrated with React? If yes, what is the best way to achieve that? ...

Tips for organizing a div into a dock panel

I am seeking guidance on how to align and resize the following divs based on my specific requirements outlined below. Area A Area A should remain at the top with a set height that will not change. Area B The height of Area B should vary between 0 and a m ...

Move the text from one text editor to another on a different page

Request: I am looking for a way to transfer content from TextEditor1 to TextEditor2, which are located on separate pages in a React Router Web App. The user inputs data into TextEditor1 on Page1 and upon submission, I want the content to be displayed in T ...

What is causing my HTML file path to function on my server, but not when I access the HTML file directly?

My file structure is organized as follows: The main file is located in the folder "HTML-Project/index.html". Within this folder, I have my style.css file and two other folders: one for pictures (HTML-Project/pictures) and another for categories (HTML-Proje ...

Tips for setting the tabindex on an element that has an opacity of 0

I have created a drag and drop image upload field with a hidden input element to style the upload button according to the design. However, I am concerned about accessibility and want the upload functionality to trigger when the user presses 'Enter&apo ...

Customizing Material-UI: A guide to overriding inner components

Looking to customize the styles of a Material UI Switch for the small variation? I have applied global styles but now want to override the styles of elements like track and thumb. Targeting the root element with the sizeSmall class is working, but unsure o ...

How to avoid an additional carriage return in Internet Explorer when editing a Textarea?

In Internet Explorer, we are facing an issue with a multiline textarea. After setting the content using JavaScript and checking it, everything appears correct without any additional carriage returns in the textarea: document.getElementById( 'text-ar ...

What could be the reason for the misalignment between md-menu and md-button when ng-href is included in the button?

Check out the demo by clicking here: http://codepen.io/audiodude/pen/vLyNvw The main distinction between the top and bottom sections lies in the fact that the top section utilizes an md-button with an ng-href attribute, which Angular Material compiles int ...