"Learn the steps of incorporating a video as your webpage's background

Initially in this file, I implemented a logic where the background images change on each reload. However, now I have a new requirement. I want to display a video in the background, and the video is stored locally in my Public folder. The project is built on React. Please provide assistance with the code implementation as well as guidance on the CSS aspect. The existing code snippet is included below.

import './App.css';
import Footer from './Components/Footer';
import Header from './Components/Header';
function App() {
  // let imageArr = ["back.jpg", "front.jpg", "right.jpg"];
  // let randomNum = Math.floor(Math.random() * imageArr.length);
  // let randomImage = imageArr[randomNum];
  return (
    // <div className="App" style={{backgroundImage: `url(${randomImage})`}}>


      <div className='container'>
        <div className='container'>
          <Header />
          <Footer />
        </div>
      </div>

    // </div>
  );
}

export default App;

Answer №1

Give this a try, it's effective!

Html:
<iframe
        src="https://www.youtube.com/embed/E7wJTI-1dvQ"
        frameborder="0"
        allow="autoplay; encrypted-media"
        allowfullscreen
        title="video"
      />

Css:
iframe {
 width: 100vw;
 height: 56.25vw; /* For a 16:9 aspect ratio, use 9/16*100 = 56.25 */
 min-height: 100vh;
 min-width: 177.77vh; /* For a 16:9 aspect ratio, use 16/9*100 = 177.77 */
 position: absolute;
 top: 50%;
 left: 50%;
 transform: translate(-50%, -50%);
}

View the code in action here: https://codesandbox.io/s/nostalgic-swartz-9196lu?file=/src/App.js

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

Browser crashes due to JavaScript for loop

I am facing an issue where a particular loop is crashing the browser when executed. This loop is part of a set of three functions designed to factorize quadratic equations. After conducting tests, I am confident that the problem lies within the loop itse ...

Fixing Bugs in Checkbox Functionality using useState in Reactjs when implementing Material UI

I am working on a numeric select functionality where selecting a number renders the component multiple times. Inside the component, there are checkboxes that should not all activate at once when one is selected. https://i.sstatic.net/Q5Csn.png You can vi ...

Tips for effectively utilizing axios without triggering the 400 (Bad Request) message on the console

In my project, I've integrated an API using axios with Ruby on Rails. The code for the apiCaller.js file is shown below: import axios from 'axios'; import { API_URL } from './../constants/config'; export default function callApi( ...

What is the best way to integrate JavaScript with my HTML command box?

I'm having some trouble with an HTML command box that is supposed to execute commands like changing stylesheets and other tasks. I thought I had everything set up correctly, but it doesn't seem to be working at all. Below is the HTML code with th ...

DOMPDF - Comparing background URL rendering between Linux and Windows

When converting HTML to PDF, my image doesn't show on a Linux server. On localhost, the image displays properly. Here is my CSS code: background: red url("<?php echo __DIR__ ?/my_img.png"); However, on the Linux server, the image does not appea ...

Troubleshooting Material UI TableCell's onclick functionality not functioning as expected

I am currently working on a functionality where clicking a cell in a table will log "hi" to the console. Below is the code snippet for the table cell: <TableCell onClick = {console.log("hi")} id = 'tc' padding = "checkbox" ...

Error: Unable to access the 'prototype' property of an undefined object (inherits_browser.js)

After updating our app to a newer version of create-react-app, we started encountering the following error: https://i.sstatic.net/ILdsl.png This error seems to be related to inherits_browser.js, which is likely from an npm module that we are unable to id ...

Adding a custom source to the script tag in Angular 7

I am currently using angular cli to develop my web application. The app is being built in the dist folder as of now. This is the index.html file: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Adm ...

Arrange, Explore (Refine), Segment HTML Chart

Is there a way to efficiently sort, paginate, and search (based on a specific column) data in an HTML table? I've explored several JQuery plugins such as DataTable, TableSorter, Picnet Table Filter, but none seem to offer all three functionalities (se ...

Adjustable Video Size and Parent Container Vertical Space

Here is the code snippet I am working with: .jumbotron { position: relative; z-index: -101; overflow: hidden; height: 500px; background: red; } .jumbotron video { position: absolute; top: 0; left: 0; right: 0; bottom: 0 ...

Adjust the font size in CSS based on a specified range of values

When the screen size is small (mobile), the font size is fixed at 14px. On really large screens, the font size is set to 18px, ensuring it never goes below 14px or above 18px>. This is achieved using media queries.</p> <p>However, for the i ...

The seamless way to eliminate error messages in React using asynchronous methods

A query fetches an array of error messages. One states that "Username is already in use", while the other indicates "Password should be at least 5 characters long". These errors are displayed in React as follows: {this.props.auth.errors ? ( this.prop ...

Only collapsing the uppermost level in the CSS hierarchy

Within my template, I have utilized the same tree structure code that was referenced in a previous question found at this link. The solution provided in that question initializes the page with the entire tree collapsed (view jsfiddle). My goal is to modif ...

Is there a way to dynamically retrieve a list of sheets from a spreadsheet using the Google Sheets API in a React application?

How can I retrieve a list of all sheets in the Google Sheet below? Take note of the parameter ranges=${sheet}. The current code is set up to fetch data from a single sheet. However, my goal is to create a dynamic call by obtaining a list of all available ...

Implementing personalized font styles in HTML on a WordPress platform

I recently followed a tutorial on how to upload custom fonts to my WordPress website, which you can find at the link below. I completed all the necessary steps, such as uploading to the ftp, modifying the header.php file to include the font stylesheet, and ...

Why bother using useCallback in a component if React.memo is not utilized alongside it?

Here's an example to consider: const Child = () => { console.log("I did re-render!"); return null; }; const App = () => { const [_, setValue] = useState(); const fn = useCallback(() => { // do something }, []); ret ...

Using div tags may cause rendering issues

I am trying to use multiple div tags with webkit borders, but for some reason only the one called 'Wrapper' is displaying properly. Here is my code: .wrapper { margin: 20px auto 20px auto; width: 800px; background: url('images/background_1. ...

What is the proper way to mention JavaScript packages when including them as parameters in Angular elements within HTML?

I was looking to enhance the command to be more authoritative. <div (click)="lastCall(999)">click me</div> My attempt to utilize Number.MAX_SAFE_INTEGER resulted in an error from my computer stating that it wasn't recognized. As a result ...

React Forms: Dynamically Updating the User Interface using WithFormik() and Axios GET Response

I've been using withFormik() to create a form for my Gatsby application. Currently, I am implementing a GET request using axios within the handleSubmit() function of withFormik(). Once I receive the response, I want to update it on the UI immediately ...

The process of updating UseContext global state in React Native and ensuring that the change is reflected across all screens

Struggling with updating global state values using React useContext on different screens? Attempting to change theme color in the App, but changes only appear on the current screen and not carried over to others? Looking for assistance in resolving this ...