What is the proper way to apply a backgroundImage to just one component?

Is there a way to set the backgroundImage on just one component in a React project? When I attach it to the body element, it shows as the backgroundImage for all components. However, if I assign it to the .wrapperContainer element of the current component, it displays differently.

My goal is to achieve this desired output without the background image showing up on all other components:

CSS:

body {
  background: url("../../services/images/hehe.jpg") no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover; 

  animation: increase 60s linear 10ms infinite;
  -webkit-transition: all 5.2s ease-in-out;
  -moz-transition: all 5.2s ease-in-out;
  -ms-transition: all 5.2s ease-in-out;
  -o-transition: all 5.2s ease-in-out;
  transition: all 5.2s ease-in-out;
  z-index: -2;

  background-color: #f6f8fa;
}

React:

return (
  <div className="wrapperContainer"> /*This is the element i replace .body with*/
    <Card className="wrapperCard">
      xxxxxxx
      xxxxx
      xxx
    </Card>
  </div>
)

Answer №1

Give this a try and see if it works for you

return (
  <div className="mainContainer" style={ { backgroundImage: 'url("../../services/images/hehe.jpg")', backgroundRepeat: 'no-repeat', backgroundPosition: 'center center', backgroundAttachment: 'fixed' } }> /*I've replaced .body with this element*/
    <Card className="cardWrapper">
       xxxxxxx
       xxxxx
       xxx
    </Card>
  </div>
)

Answer №2

Welcome to STACK Arasto. Feel free to customize it with the component name you prefer. For instance, if you only want the background to show up in the Card component.

Let's say the render function of Card is as follows:

<div className="bg-apply">

</div>

You can assign a class to the parent div in the Card component and style that class just like you did in your query. This will ensure that the style/background image is exclusively applied to this specific div/component.

.bg-apply {
background: url("../../services/images/hehe.jpg") no-repeat center center fixed; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover; 
animation: increase 60s linear 10ms infinite;
-webkit-transition: all 5.2s ease-in-out;
-moz-transition: all 5.2s ease-in-out;
-ms-transition: all 5.2s ease-in-out;
-o-transition: all 5.2s ease-in-out;
transition: all 5.2s ease-in-out;
z-index: -2;
background-color: #f6f8fa;
}

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

"Comparing 'npm start' and 'npm run start: prod'

Whenever I run npm start, I encounter this page: https://i.sstatic.net/OMJAd.png On the other hand, when I run npm run start: prod, I see this page instead: https://i.sstatic.net/1ATAW.png Interestingly, for all other components, both npm start and npm ...

Creating dynamic dropdown lists in React Js depending on the selection in the initial dropdown

I have been struggling to render a dropdown list dynamically based on the selected option in the first dropdown. Despite watching related videos and reading articles, I still can't figure out where I am going wrong. There are two dropdowns, and I want ...

hide input type with minimal display

Can someone help me find the less equivalent to this css code snippet? I'm not familiar with less and this code is part of a larger global css file that includes generated code from all less files. input[type="checkbox"] { display: none; } Any a ...

Is Preact's bundle size bigger compared to create-react-app?

Contemplating a shift to Preact for my project in order to reduce the bundle size. However, upon comparing the bundles, the difference was not as significant as I had anticipated. Preact, 39kb: https://i.sstatic.net/PRdEd.jpg React 45kb: https://i.sstati ...

CurrentStyle in IE is not accurately reading the inherited font size from the computed style, leading to incorrect results

Here is a little test case I've compiled for you: http://jsfiddle.net/D4sLk/2/ The font sizes set in this case are as follows: * (everything): 12px container: 20px test element: inherit The hierarchy of the DOM is: container > test element. W ...

What are alternative options to replace the <br> tag?

My form has a simple structure like this: <form method="post" action="/registration"> <label for="alias">Alias:</label> <input type="text" name="alias" id="alias"& ...

NextJS experiencing a glitch with the header design

Trying to create a custom header in NextJS, but encountering an issue: https://i.sstatic.net/Y9aUE.png Attempted to set width and height to 100%, but it's not working as expected. Here's the code snippet I'm using: import type { NextPage ...

Position the Material UI Box element to the bottom of its parent container

Hello there, I've come across the following layout: <Box width="100%" height="100%" p={1}> <Box my={1} display="flex" justifyContent="center"> </ ...

Creating an app using Ionic 1 that features scrollable tabs with the ability to handle overflow

My current code snippet is displayed below. On smaller mobile screens, all 7 tabs are not visible at once and instead appear in two rows which looks messy. I want to modify the display so that only 3 tabs are visible at a time and can be scrolled through. ...

Encountering an issue with a cyclic object value error when trying to access the state after modifying it

Hey there, I'm currently immersed in a react-native project where I've set up a form to update the state as inputs are filled out. However, when I try to access my state later on, I encounter an error stating cyclic object value. Upon delving i ...

React Select streamlines dropdown options for multi-selection by abbreviating names

Is there a way to shorten dropdown names when selected similar to the example shown in the image below https://i.sstatic.net/qUFP6.png This is the snippet of my code : multiValue: [ { value: "BUF", label: "BUF" }, ...

ReactJS error: Unrecognized property <problem>

Issue: I am facing a problem where the 'completed' property set to a boolean in my to-do list is not getting checked when using toDoData.map (item =>). Within my ToDoData.js file, I have defined a property called "completed" with true or fals ...

Struggling to align text to the far left within a text area using Bootstrap

When I accidentally place my cursor earlier in the text area, it starts writing from that point, leaving some unwanted spaces at the beginning. I prefer it to start from the extreme left, similar to how it behaves in input boxes. Below is the code snippet ...

Navigate Formik Fields on a Map

Material UI text-fields are being used and validated with Formik. I am looking for a way to map items to avoid repetitive typing, but encountering difficulties in doing so. return ( <div> <Formik initialValues={{ email: '&a ...

What is the method for configuring the App bar component in the absence of an iconElementLeft

Is it possible to utilize the app bar component without displaying the left icon? I attempted to disregard the iconElementLeft property with no success, as the icon continues to appear. import React from 'react'; import AppBar from 'materi ...

Having trouble with the onLoadingComplete props in the Next.js Image component?

Is there a way to properly retrieve the naturalWidth and naturalHeight using the onLoadingComplete props? I tried following the documentation on https://nextjs.org/docs/api-reference/next/image#onloadingcomplete but it doesn't seem to be working. Am I ...

Encountering a problem when trying to utilize material-ui icons in a React application

After installing the Material-UI core and icons packages using npm install @material-ui/core and npm install @material-ui/icons in my React app, I tried to use the FileUploadIcon. Here's how I imported it: import { FileUploadIcon } from '@materia ...

Tips on efficiently updating state for an object containing two arrays of objects

Is there a way to dynamically add new objects to the data array in this code snippet? I want to use setState() to modify the existing state or completely replace the data property. const [tableState, setTableState] = useState<TableState>({ co ...

Attempting to incorporate Font-Awesome Icons into the navigation bar tabs

As a newcomer to React, I've been attempting to incorporate Font Awesome icons into my secondary navigation bar. Despite using switch-case statements to iterate through each element, all the icons ended up looking the same, indicating that only the de ...

The process of generating an efficient build gets stuck and never finishes

I am currently facing an issue while attempting to build my React app. Whenever I execute "npm run build," the terminal gets stuck at "Creating and optimized production build..." and doesn't complete the process. I have tried running this on a system ...