Center-aligned video text overlay that adapts to different screen sizes for a responsive design

I am looking to showcase a full-width video with overlay text that is perfectly centered both vertically and horizontally on the video. The centering should adapt to changes in viewport width so that it remains centered at all times. Additionally, I would like a "caption" (displayed as an h2 tag in the example) to always appear right below the video, regardless of viewport size.

Below is my sample code - any assistance would be greatly appreciated.

Thank you

Dennis

<head>
<style>

.header-unit {
       margin-top: 10px;
     }
     
#video-container {
       height:100%;
       width:100%;
       overflow: hidden;
       position: relative;
     }

#video-overlay {
       position: absolute;
       z-index: 1;
       font-size: 50px;
       color: red;
       margin: 0;
       transform: translate(-50%, -50%);
       width: 85%;
       text-align: center;
       top: 25%;
       left: 50%;
     }

video {
       position: absolute;
       z-index: 0;
     }
     video.fillWidth {
      width: 100%;
     }
     
</style>
</head>
    <div class="header-unit">
        <div id="video-container">
            <p id="video-overlay">Get A Quote!</a></p>
            <video autoplay muted loop class="fillWidth">
              <source src="https://www.w3schools.com/html/mov_bbb.mp4"/>
            </video>
        </div>
    </div>  
<h2>Test Caption</h2>

Answer №1

Kindly update your CSS code as follows:

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}

.header-unit {
       margin-top: 10px;
  height: 100%;
     }
     
#video-container {
       height:100%;
       width:100%;
       overflow: hidden;
       position: relative;
     }

#video-overlay {
       position: absolute;
       z-index: 1;
       font-size: 50px;
       color: red;
       margin: 0;
       transform: translate(-50%, -50%);
       width: 85%;
       text-align: center;
       top: 50%;
       left: 50%;
     }

video {
       position: absolute;
       z-index: 0;
     }
     video.fillWidth {
      width: 100%;
           height: 100%;
    object-fit: cover;
     }
<div class="header-unit">
        <div id="video-container">
            <p id="video-overlay">Get A Quote!</a></p>
            <video autoplay muted loop class="fillWidth">
              <source src="https://www.w3schools.com/html/mov_bbb.mp4"/>
            </video>
        </div>
    </div>  
<h2>Test Caption</h2>

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

Issue with Angular: Mobile view toggle button in the navbar is unresponsive

While the dropdowns are functioning correctly in web mode, the navbar toggle isn't working as expected in small screens or mobile mode. I've been trying to figure out the issue by referring to code from CodePen that I am learning from. Despite i ...

"Developing CSS styles that work seamlessly across all

What methods do professional web designers use to develop cross-browser compatible CSS? Is it typically a manual process, or are there specific toolkits available to simplify the task similar to YUI for JavaScript? I am looking to avoid WYSIWYG editors s ...

Using jQuery to delay hiding for 2 seconds

Hey there! I'm facing an issue with my code. The function is supposed to make #aboutPopOut slide to the left, and then after a 2-second delay, fade out the fadescreen by hiding it. The sliding part works fine, but the waiting and hiding do not seem to ...

Updating the value of a localStorage key after each successful AJAX call in JavaScript

I'm currently facing a challenge with my local storage key value. The issue is that it doesn't update the value when the AJAX call successfully returns data. It only retains the old stored data, requiring me to manually refresh the page by pressi ...

What is the best way to transfer attribute values from multiple elements and paste them each into a separate element?

I have multiple elements with the tag <a class="banner2">. Each of these elements has a different URL for the background image and href value. <a href="#" target="_blank" class="banner2" style="background-image:url('<?php echo get_templat ...

Retrieve the position of an element using Python and selenium

Currently, I am working on a project and need to extract the location and size of an element using the following xpath: "//div[@class='titlu']" Upon inspection, it appears that the element is visible with no distinct features. However, I have e ...

The calculator is experiencing issues with JavaScript functionality

Having some trouble developing a calculator using HTML5, CSS, and JavaScript. After passing my HTML and CSS through validators successfully, I encountered issues when adding JavaScript functions to enable the functionality of the buttons on the calculator. ...

Padding causing links to break exclusively in Firefox

As a seasoned front-end developer, I recently encountered an issue that has stumped me. My main navigation bar has extensive right padding to ensure the hover dropdowns have ample space. The problem seems to be in this piece of code: HTML: <div id="h ...

Mobile Size Setting

Could someone please explain to me why when I test this code on Google Chrome using the mobile emulator for Galaxy S3, it displays the correct size (640x360), but when I try to load it on my actual Galaxy S5 or any other device, the size is different from ...

A guide to entering information into an input field with JavaScript for logging in successfully

https://i.stack.imgur.com/TF51Z.gif https://i.stack.imgur.com/HHsax.png https://i.stack.imgur.com/HUztt.png When attempting to input text using document.getelement('').value="" , it doesn't behave as expected. The text disappear ...

Ways to invoke a JavaScript function within a child window that is already open

I am working on a project where I have a main html file. In this file, I want the user to click on something that will trigger a new window or tab to open. This new window/tab should display the dynamically generated content of a hidden div in the main htm ...

AngularJS: Issue with ngChange not being triggered from directive

A Summary of the Issue at Hand I've created a directive that dynamically displays a list of checkboxes. The directive has a parameter named options, which should be an array structured like this in order for the checkboxes to display correctly: var ...

Ways to showcase an icon within a select options tag

Is there a way to show Font Awesome icons in select options tag? I have tried using it outside like this <i class="fas fa-chart-pie"></i> But I'm not sure how to display it within the <option> tags instead of text. <select id="s ...

What other technique can I use to replace the creation of a jquery division?

I`m relatively new to all things related to web technologies and I am currently practicing and learning about them. On my practice page, I've experimented with various elements, including changing div heights based on the viewport height. Here's ...

Mobile browser not resizing window width in fluid layout on WordPress site

Currently, I am working on developing a basic web layout that can adapt to mobile view. I have made some progress which you can check out here. The layout successfully adjusts when the window is resized in Firefox or Chrome. However, when I tried to acce ...

Tips for using Python to capture specific HTML snippets

Here is a Python code snippet that I am working on: def parseAddressInfo(text): text = re.sub('\t', '', text) text = re.sub('\n', '', text) blocks = re.findall('<div class="result-box" ...

Adjust the size of the container DIV based on the content within the child DIV

I have been searching for a solution to my question without success. Here is the issue: I have a DIV that acts as a container for a Post in my project. Inside this post are classes for the poster avatar, name, timestamp, text, and more. The problem arise ...

Sorting through a table based on the name of the element

I am currently working on filtering an HTML table using a search form. It's working great, but I'm facing an issue where the filtered elements are trying to fill the entire width of the table instead of maintaining their original width (which is ...

Entity designation experiencing operational difficulties

Having trouble converting my HTML to XHTML. Whenever I replace the greater than sign with &gt;, my if statement stops working properly. The same issue occurs when replacing the ampersand with & and the less than sign with <. Even CDATA isn' ...

I possess a solitary div element that requires dynamic replication

I have a single container and an unspecified number of rows of data. I want to display this data on HTML cards that are generated dynamically based on the number of rows. For example, if there are 10 rows of data, I need to create 10 card elements with ea ...