"Step-by-step guide on positioning a video in the center over another

I am designing a landing page and I want to incorporate a background video with a centrally aligned logo on top of the video. My goal is for the center of the logo to always align with the center of the background image, regardless of how the page responds to different screen sizes.

Here is a sample image I created in Photoshop to illustrate my idea: (https://i.sstatic.net/50Iwk.jpg) Essentially, as the page adjusts in size, I want the central logo and buttons to remain centered over the background image.

I have been exploring different parent-child approaches to achieve this effect. A forum discussion I found was helpful in guiding me towards the right direction: How to center one image over another

While the forum discussed images, I attempted to apply similar concepts to videos. However, I am encountering difficulties with overlaying the videos as intended. Here is a representation of the current issue: (https://i.sstatic.net/6nHtF.jpg)

The code snippet I started with is clearly incorrect, but I am unsure of where the mistake lies:

<!DOCTYPE html>
<html>
    <head>
        <title>Overlaying and centering a video under another video</title>
        <style>

            .frame{
                display: flex;
                align-items: center;
                justify-content: center;
            }

            #background-video {
                width: 1800px;
                z-index: -1;
            }
  
            #foreground-video {
                width: 500px;
                z-index: 1;
            }
        </style>
    </head>
    <body>
        <div class="frame">
            <video id = "background-video" autoplay loop muted>
                <source src= "screwvideo.mp4" type = "video/mp4">
            </video>
            
            <video id = "foreground-video" autoplay loop muted>
                <source src= "YEEEE.mp4" type = "video/mp4">
            </video>
        </div>
    </body>
</html>

Any advice or guidance on solving this issue would be highly appreciated, as I have struggled to find specific resources addressing this unique problem.

Answer №1

It's important to note that creating a video with a truly transparent background is not possible; there will always be some kind of background present. To achieve the illusion of transparency, consider utilizing the .gif format for your video.

To further enhance this effect, incorporate the following styles into your code:

.frame{
  position: relative;
}
  
#foreground-video {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1;
}

Answer №2

To enhance the HTML structure, I propose the inclusion of a new division labeled as "foreground".

<div class="frame">
    <video id = "background-video" autoplay loop muted>
      <source src= "screwvideo.mp4" type = "video/mp4">
    </video>
    
    <div class="foreground">
      <video id = "foreground-video" autoplay loop muted>
        <source src= "YEEEE.mp4" type = "video/mp4">
      </video>
    </div>
</div>

Subsequently, the focus will be on styling the "frame" and "foreground" classes instead of directly modifying the video elements. Furthermore, within the "foreground" div, incorporating buttons for home and about sections is recommended.

.frame{
  position: relative;
  width: 1800px;
  z-index: -1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.foreground {
  position: absolute;
  width: 500px;
  z-index: 1;
}

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

Traverse an SVG element using JavaScript

I have a beautiful star SVG that I created, and I want to use it instead of styling a span to create floating bubbles in my div. However, I'm facing some challenges in adapting my Javascript loop to incorporate the SVG for creating the bubbles, as opp ...

What is the best way to align div elements side by side while using a flex direction of column?

I need help aligning the text input next to the h1 tag, inline, and then displaying it as a flex-direction of column. Currently, all inputs are being set in a line with the h1 on top which is not the intended layout. Here is an illustration: https://i.sst ...

Experiencing issues with creating HTML using JavaScript?

I'm a JavaScript novice and struggling to figure out what's wrong with my code. Here is the snippet: var postCount = 0; function generatePost(title, time, text) { var div = document.createElement("div"); div.className = "content"; d ...

The simplest way to increase the size of a child element in order to generate a scrollable area

When working with HTML, it's important to consider how the size of a child div affects the parent div. If the child div is larger than its parent, scrollbars will appear on the parent div if the appropriate style rules are set. However, I'm inte ...

Adjusting height dynamically with CSS based on font size

Below is a link followed by a span element <a href="#" style="display:inline-block;font-size:900%">12</a> <span display="block">ACTIVE</span> The issue arises when the 'a' element does not have a specific height, causing ...

Do the last block in the table occupy the remaining space in the row?

Trying to create an HTML email calendar and struggling with getting the last td to fill the remaining space without affecting the width of other rows. Is there a way to increase the width of just the 4th block on the last row to fill the empty space? Here ...

Matching Figures to their Descriptions

I am currently working on a website and looking to include images with captions that display the price of each item underneath. The issue I'm facing is that the captions are aligning horizontally instead of vertically. Can anyone provide assistance wi ...

Looking to dynamically set a background image using data fetched from an API in a ReactJS project

Looking to incorporate a background image from an API response in ReactJS Here is some sample code: useEffect(() => { axios.get(`https://apiaddress=${API_KEY}`) .then(res=>{ console.log(res); setRetrieved(res.data); console.log(retrieved ...

Error copying cell width attribute when using border collapse

Currently, I am attempting to duplicate a table header by copying the tr HTML and then replicating the th widths. Unfortunately, this method is unsuccessful as the widths are displayed as (width minus W) pixels when border: 'Wpx' and border-colla ...

The CSS overflow scroller trims the excess background color

Attempting to build a website, I encountered an issue with displaying a scroll bar. Despite using the CSS property overflow: auto, I faced another problem. Let me illustrate the issue through a simple example. I have an outer div with the style overflow: ...

Flashing on objects, Chrome-exclusive, utilizing background-blend-mode exclusively

Our website, www.desirio.com, utilizes a background-image on the body with the following CSS property: background-blend-mode: luminosity An issue we have noticed in Chrome is that when hovering over elements, strange white lines randomly flash on the scr ...

The strange behavior of !important, display:none, and .height()

While working with a piece of JS code yesterday, I stumbled upon something peculiar. There was a div element that was initially hidden using display:none, and I was utilizing its height in some JavaScript calculations. Everything was functioning properly u ...

Changing the Title of UI Tabs

I am facing an issue with setting the title in tabs, as the title is appearing behind the background. If I set background: transparent; in the #tabss .ui-tabs-nav class, then my title shows up properly. Otherwise, it gets displayed behind the background. ...

How can you animate a CSS fill effect from the border outward?

I've been struggling to find an answer to this seemingly simple question online. Can anyone explain how to animate a fill-in with a specific color of an element, starting at the border? For example, the border expands gradually until the entire shap ...

The issue of Django/Python static files not loading is causing disruption

mysite/settings.py """ Configuration settings for the Django project called mysite. This file was generated by 'django-admin startproject' using Django 1.9.5. For more details about this file, visit https://docs.djangoproject.com/en/1.9/topics ...

Eliminate unnecessary white space on your webpage using CSS with the 960.gs grid system

As I work on building my portfolio website from scratch, I came across a CSS framework known as "960.gs". While I have experience with similar tools, this is my first time delving into a pure CSS framework, so I'm feeling a bit out of practice. Here ...

What is the method to retrieve the class name of an element when the div is created as '<div id 'one' class="element one"></div>'?

I have three elements named one, two, and three, declared as seen below: <div id =container> <div id 'one' class="element one"></div> <div id 'two' class="element two"></div> < ...

HTML is unable to recognize the CSS file. Maven and Spring are the solutions

I'm having some trouble connecting my CSS to my HTML. This is the structure of my folders: https://i.sstatic.net/B9bHx.png Here is my HTML code: https://i.sstatic.net/XyE4E.png I've tried several methods to make it work. Interestingly, every ...

"Eliminating Redundant JS and CSS Files in Magento to Improve

While analyzing the performance of a website, I noticed that GTmetrix found some CSS & JS files being loaded from different locations but containing the same file. An example: <action method="addItem" module="ves_blockbuilder" ifconfig="ves_blockbuilde ...

Struggling to align the header of a column to the center in a mat-table that is sortable?

I am working with a mat-table that has a sortable column similar to the example shown here, but I am having trouble centering the column headers using my CSS file. Interestingly enough, I am able to achieve this when making changes in the browser debugger! ...