Is there a way to set an image as the background for several components in ReactJS?

I am trying to add an image as a background where various components like video, login, and bottom component will be placed on top of it. Can anyone help me with the correct code structure for achieving this in React? I am quite new to React development. All the different components will go within this section. The code snippet is provided below:

import React from 'react'
import Video from './Video.js'
import './Home.css'
import logo from './images/background.png'

    function Home() {
        return (
            <div className="home">
               <img src={ logo } />
            </div>
        ) 
    }
    
    export default Home

Answer №1

To achieve an image overlay effect on a child component, it is recommended to create a parent component that wraps around the child component. This way, the parent component will appear as an overlay above the child component.

import React from 'react'
import Video from './Video.js'
import './Home.css'
import logo from './images/background.png'

function ImageOverlay() {
    return (
        <div className="home" style={{background: url(logo)}}>
           {...props.children}
        </div>
    ) 
}

export default ImageOverlay

To utilize this parent component:

<ImageOverlay>
    <Video src={videourl} >
</ImageOverlay>

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 techniques can I use to format my blockquotes in this particular style?

I'm struggling to figure out how to incorporate yellow blockquotes into my quotes without the formatting causing unwanted indents and line heights. My goal is to achieve something like this: https://i.sstatic.net/4O1NR.png This is what I've at ...

Incorporating external CSS stylesheets into a custom LESS framework

Is there a reliable method for integrating a third-party CSS file obtained from an external source into my own LESS compile structure without making many changes to it? I anticipate that the external CSS file may be updated periodically, so I would prefer ...

Create a new webpage following the slug

Currently in the process of developing a NextJS application, I am utilizing getStaticPaths and getStaticProps to generate static pages and handle necessary requests for them. The goal is to create all pages following the URL structure: challenge/[slug]/ w ...

Aligning the Navigation icons on my webpage

<nav> <ul> <li><a href="index.php">Home</a></li> <li><a href="index.php?about">About</a></li> <li><a href="index.php?products">Products</ ...

During the loading process, CSS display inline items may momentarily appear as list-items on the page

I am experiencing an issue with my Carousel (jcarousel) displaying items inline. It seems that when the carousel loads, the items first appear vertically in a list format before switching to their normal inline position. This is happening on multiple webs ...

Selenium WebDriver in Java is able to detect hidden elements by checking if they are displayed or not

This snippet of HTML code contains a Form: <div id="bodyLeftBlock" class=""> <form id="signUpForm" class="" method="post" action="/en/signup/post/" novalidate="novalidate" style="display: none;"> <input class="" type="hidden" va ...

What is the best way to create CSS that will only work on browsers other than IE and Edge?

I need assistance with writing CSS code that will only be applied to 'desktop' screen sizes when the page is viewed on browsers other than IE and Edge. Here's what I have so far: @media only screen and (min-width: 981px) /* and XXX */ The ...

Utilizing React hooks to import an array of objects in JSON format

I spent an entire day trying to incorporate categories from a JSON file into my React project using hooks, but I haven't had any success yet. I'm really hoping someone can assist me with this task. Below is a snippet of the JSON structure: { &quo ...

Assistance with HTML and CSS to position an image in the corner

I need assistance with positioning a corner image on top of another image. I want the span, which has a small background image, to be in the top left corner of the main image. Below is my code: <div id="wrapkon"> <span style="width: 4px; height: ...

Tips for concealing text adjustments during window resizing in an HTML document

I'm currently working with a website code that involves two sidenavs, one on the right and another on the left. When one sidebar opens, the other collapses. However, I've noticed that the text in between readjusts when the sidebars are toggled op ...

Troubleshooting: Bootstrap 4 CSS causing Chrome's print layout to vanish

My goal is to give users of my website the ability to print pages in either portrait or landscape mode. While attempting to print from Google Chrome with Bootstrap 3.3.7, I noticed that the Chrome print settings included a Layout option. However, after up ...

React: Utilizing a shared data store across multiple components

As I develop a React Component within a library that enables multiple instances to share state, update state, and track unmounting of all instances, I face the challenge of not being able to utilize the Context provider pattern due to the inability to wrap ...

The JavaScript code in Three.js is experiencing issues when running in a secure HTTPS environment

After transitioning my website from http to https, I encountered an issue with one of my projects. The background using the Three.js library in this random generator does not show up when the URL is https. However, when the URL is http, the generator wor ...

I keep checking my input file, convinced that it's empty even though I've already selected a file

I am currently facing an issue with the upload system using Codeigniter 4 and Bootstrap 4.6x. Everything seems to be working fine except for one specific problem. My intention is to have users upload a screenshot for my leaderboard web app. However, whenev ...

No display of Tailwind updates on local server in Safari browser using NextJS and a Mac M1

For my personal project with NextJS, I am using Tailwind CSS, but I am experiencing frequent issues with updating styles. It seems like there is a 50/50 chance that Tailwind will apply the styles to the component properly, and sometimes I have to go throug ...

What causes the appearance of a slight space between a child element positioned absolutely and its parent element with borders? All the margins, padding, and positions have been assigned a value of

I have encountered a peculiar issue that I managed to recreate in a simplified version. In my scenario, the parent element has the CSS property position: relative, while the child element with the ::after pseudo-element has position: absolute and all direc ...

Access the ID within the onClick function in Next.js that is already accessible from getServerSideProps

I am currently facing an issue where I need to access id1 from getServerSideProps within an onclick function. I attempted to use useState inside getServerSideProps and tried to access it within the onclick function. However, I am unable to console.log(id ...

Tips for Repurposing a React Table

I am in the process of developing my own react component library to be used across my entire application. At the moment, I have started with a table component which is currently undergoing testing. However, I am facing the challenge of calling the componen ...

The 'async' keyword is not permitted in this context within a TypeScript React application

An issue has arisen with the aync, indicating that the async modifier is not permitted in this context: private async getValue= (acc: Access): void => { await this.service.getForm(''); } It appears that there might be an ...

Determine the attribute value based on the source

Is there a method to retrieve the value of the swap-section attribute based on a specific image source? <div class="ig_gallery_right_img" style="overflow-y: scroll"> <img src="/corp/homeowner/images/promo/gallery_traditional_3_sm.jpg" ...