Guide to setting a universal background image in ReactJS for all pages

I attempted to add an image as a background to all pages, but encountered some challenges along the way. Initially, I tried setting the background image in the style of the body tag within index.html:

<body style="background-image: url(%PUBLIC_URL%/bp.jpg)">

However, this did not produce the desired result. Next, I moved the picture from the public folder to /src and added it to the body tag in index.css:

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background-image: url("./bg.jpg");
}

Unfortunately, this method also failed to achieve the desired outcome. As a final attempt, I modified the App.css file which controls routing in my project:

In App.js:

import './App.css';
import { BrowserRouter as Router, Route } from "react-router-dom";
import CharacterFormView from "./views/CharacterFormView";
import SkillTreeView from "./views/SkillTreeView";

function App() {
  return (
    <div className="App">
      <Router>

        <Route exact path="/" component={CharacterFormView}/>
        <Route path="/character-form" component={CharacterFormView}/>
        <Route path="/skill-trees" component={SkillTreeView}/>

      </Router>
    </div>
  );
}

export default App;

In App.css:

.App {
  text-align: center;
  background: url("./bg.jpg") no-repeat;
  background-size: cover;
}

While this last attempt successfully set the background image for the first two routes, "/" and "/character-form", it only displayed up to half the page and did not extend to fullscreen. The third route, "/skill-trees", had no background image at all.

I am seeking a more efficient solution to apply the same fullscreen background image to all pages without needing to manually specify it in each .css file. Any guidance or suggestions would be greatly appreciated.

Thank you in advance.

Answer №1

If you want to add a background image in your App, make sure to do it properly by following these instructions:

 .App {
      text-align: center;
      background-image: url("./bg.jpg"); // ensure the path is correct
      background-size: cover;
    }

By implementing the background image in this way, it will be displayed in your main component, such as App.js, and all of its child components.

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

Struggling to grasp the concept of DOM Event Listeners

Hello, I have a question regarding some JavaScript code that I am struggling with. function login() { var lgin = document.getElementById("logIn"); lgin.style.display = "block"; lgin.style.position = "fixed"; lgin.style.width = "100%"; ...

Guide to making a dynamic image map with interactive links using HTML and CSS

Seeking advice on how to create clickable areas within an image that lead to different pages on my website. Image maps seem like a good solution, but the issue is that they require specific coordinates which may not work well for responsiveness. I was hop ...

What is the best way to increase the row spacing in an Ant Design Table?

I have a table with expandable rows in antd, and I am looking to add some vertical space between the rows. I've tried using the rowClassName property provided by antd, but it did not work as expected. I also attempted to use a custom component, but th ...

Safari fails to refresh CSS when it comes to dynamic attributes

Take a look at this simple demonstration: http://jsfiddle.net/fE8ry/ I am attempting to modify the attribute of a div, and based on the attribute's value, apply the corresponding CSS. When the page loads, the attribute selector functions correctly a ...

The final thumbnail fails to appear in the visible display (react-responsive-carousel)

I am currently facing an issue with displaying a series of images using react-responsive-carousel. When the images exceed a certain size causing the thumbnail section to become scrollable, the last thumbnail is always out of view. Although I have impleme ...

Using the curl command, incorporate JSON data into an already established array

I've been working with PHP to use curl and send an HTML post request. My goal is to include additional data to an existing JSON array located at a particular URL. When I send the request, I'm unsure if the data will be replaced or added to the ex ...

I desire to alter the description of an item within a separate div, specifically in a bootstrap

I used the map method to render all the images. However, I need a way to track the current image index so that I can change the item description located in another div. Alternatively, if you have any other solutions, please let me know. App.js : import Ca ...

I don't understand why my BeautifulSoup code is throwing an attribute error even though the variable it's referring to is assigned a value

Currently, I am working with Python 3.9.1 along with selenium and BeautifulSoup to develop my first web scraper targeting Tesco's website. This is essentially a mini project that serves the purpose of helping me learn. However, upon running the code p ...

showing text from chosen selection with an additional element included in the output

I could use some assistance with this code snippet. My goal is to showcase the options in the format: "Fried Rice = 10.000" as the outcome. However, the issue I am facing is that the select option box also contains the price. What I actually need is for th ...

Is there a way to monitor images that are integrated into HTML code?

Is there a way to track the views and impressions of images on web pages while still allowing them to be embedded in HTML through the "img src="http://mysite.com/upload/myimage.jpg"/" element? I'm familiar with setting up a handler for ".jpg" files i ...

Having trouble applying CSS to multiple classes used in an AJAX call

I'm trying to utilize richfaces for some fast AJAX widgets, but I am encountering difficulties in setting CSS parameters for them. After inspecting the generated code, I found that the elements have the classes "rf-ds" and "rpds". Despite applying st ...

Formik's handleSubmit function seems to be being overlooked and not executed as

I've encountered an issue while trying to validate a form before submission using formik and yup validation. The form is divided into two parts, where the first part needs to be validated before moving on to the second part. I set a state handleShow(t ...

Building an Angular 4 app featuring a customized default landing page and URL parameters functionality

I am in the process of developing a web portal that will be embedded within an iFrame. Below is the code snippet I am using to set up the portal: Routes Configuration const routes: Routes = [ { path: '', redirectTo: '/dash ...

downsides of scrolling text functionality

Sure, it may seem evil. But what makes it so? Is it because all browsers support it? Which aspx asp.net controls are restricted in this tag? What are the reasons for avoiding this tag? ...

"Enhancing Code Readability with VS Code: Adding Space before Curly Br

Whenever I utilize the autoformat feature in my VS Code editor, it adds spaces before curly brackets in my code: Change this: <Button onClick={this.callMyFunc.bind(this, screenSet.index)}>Add</Button> To this: <Button onClick={this.callM ...

Add transparency to a component using CSS

I am currently facing an issue with a div element called signup-box. I have applied an opacity value of 0.65 to it, but unfortunately, this is affecting everything within the div as well. This means that my white text is no longer visible as white and th ...

Experimenting with altering the heights of two Views using GestureHandler in React Native

I am currently working on a project where I need to implement height adjustable Views using React Native. One particular aspect has been causing me some trouble. I'm trying to create two stacked Views with a draggable line in between them so that the ...

Displaying NodeJS error messages directly in the main HTML file

After creating a form using NodeJs and implementing input validations that display errors when users enter incorrect values, I encountered an issue where the errors appear on a new blank page instead of within the main HTML file itself with stylish formatt ...

Using the shortcut (Ctrl + /) in vscode to create a comment in jsx can sometimes lead to

When using vscode with react class components, pressing crl + / will generate the proper comment syntax for JSX. {/* comment */} However, when working with react functional components in vscode, pressing crl + / will produce an incorrect comment syntax f ...

Identifying and recording duplicate values within an array using object transformation in JavaScript

I currently have an array structured like this: uniqueCount = [a,a,b,c,d,a,a]; I'm trying to figure out how many occurrences of each element (a, b, c) are in the array. I'd like to display the results in the form of an array of objects: [{key ...