Video sections that adjust dynamically to fill the entire height and width of the screen without relying on the position:fixed property

My goal is to design a single-page website with multiple sections, each featuring a video that spans the height and width of the viewport (100vh and 100vw). These videos should stack on top of each other, filling the entire browser window. While I've come across tutorials suggesting the use of position fixed for responsiveness, I don't want the videos to act as background in all sections. Is there a way to achieve this without using position absolute, which doesn't maintain full width and height when resizing?

I'm looking for a solution where the responsive video sections remain intact without breaking or overflowing when the browser is resized.

Edit:

Below is my HTML code snippet:

<section class="section1">
    <video id="videoBG" autoplay muted loop>
    <source src="style/vid/1_first.mp4" type="video/mp4">
    </video>
</section>

<section class="section2">
<video id="videoBG2" autoplay muted loop>
    <source src="style/vid/2_second.mp4" type="video/mp4">
    </video>
</section>

And here's the CSS portion:

section {
height: 100vh;
}

.section1 #videoBG, .section2 #videoBG2 {
position: fixed;
z-index: -1;
}


/* Making videos responsive for various screen sizes */

@media (min-aspect-ratio: 16/9) AND (min-aspect-ratio: 8/5) AND (min-aspect-ratio: 4/3) AND (min-aspect-ratio: 3/2){
.section1 #videoBG, .section2 #videoBG2{
width: 100%;
height: auto;
}
}
@media (max-aspect-ratio: 16/9) AND (max-aspect-ratio: 8/5) AND (max-aspect-ratio: 4/3) AND (max-aspect-ratio: 3/2) {
    .section1 #videoBG, .section2 #videoBG2 { 
        width:auto;
        height: 100%;
    }
}

Answer №1

To address the query at hand, the solution involves stacking videos on top of each other with each video section set to 100vh in height.

To achieve this, it's recommended to enclose the video sections within a wrapper container (

<main id="videosWrapper"><main>
).

By utilizing object-fit:cover, we can effectively resolve the issue.

#videosWrapper{
 max-width:100vw;
 max-height:100vh;
}

Subsequently, for individual videos, employ the following CSS:

.videoCSS{
 width: 100%;
 height:100%;
 object-fit: cover;
}

SEE THE COMPLETE CODE BELOW:

*{
  margin:0;
  padding:0;
  box-sizing: border-box;
}

#videosWrapper{
  max-width:100vw;
  max-height:100vh;
}

.videoCSS{
  min-width: 100%;
  min-height:100%;
  width:100%;
}
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Stack of full screen videos</title>
  <link rel="stylesheet" href="./style.css">
</head>

<body>
  <main id="videosWrapper">
    <section class="section1">
      <video class="videoCSS" autoplay muted loop>
        <source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4"
          type="video/mp4" />
      </video>
    </section>


    <section class="section2">
      <video class="videoCSS" autoplay muted loop>
        <source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerMeltdowns.mp4"
          type="video/mp4" />
      </video>
    </section>

    <section class="section2">
      <video class="videoCSS" autoplay muted loop>
        <source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4"
          type="video/mp4" />
      </video>
    </section>
  </main>

</body>

</html>

Note: object-fit:cover functionality is not supported on IE/Edge browsers.

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

Angular 7 - Customize Mat-select-value color depending on a specific condition

One issue I am facing is changing the color of a selected value in a mat select based on a condition. This change should be visually apparent to the user. Although accessing mat-select-value from the styles is possible, implementing a condition using ng-cl ...

What could be causing my PHP login page to malfunction?

I'm facing an issue with my code where nothing happens when I click the submit button. This is puzzling as the same code worked perfectly fine in a previous project. I'm unable to pinpoint where the problem lies. Here's the HTML form: < ...

Placing a moving element over an image

I'm currently developing a solar system viewer app that features 8 objects orbiting a central point using a specific method. .orbitContainer { width:var(--s); height:var(--s); margin:auto; text-align: center; border-radius: 50%; animati ...

Should PHP be avoided in CSS files?

I've recently developed a CSS page named style.php and at the beginning, I included this line: <?php header("Content-type: text/css"); ?> What are your thoughts on this approach? Is it considered a bad idea? The reason behind doing this is tha ...

Is there a cast I should be looking for with a querySelector('#element') function?

After searching for a similar answer to my question with no luck, I decided to ask it myself. My issue involves selecting an HTML element by its designated ID. The following version of the code functions correctly: var button = document.getEl ...

What steps do I need to take in order for my web controls to show up on top of an activex

I'm currently facing a challenge in designing a website with activex reports. Even though I recommended switching to another reporting tool or technology, we're still using activex for now. I've noticed that graphical activex objects displa ...

Overlap of columns within a nested flexbox arrangement

While working with React and Styled Components, I encountered a problem of elements overlapping each other: https://i.stack.imgur.com/RuCYu.png There are a few instances of overlap, including: The padding below the <ul> element is colliding with ...

Creating individual background colors for each nested child element is achievable by using CSS selectors and the nth

I want to create a reddit comment layout similar to this: <div> <div class="comment" style="background: white;"> <div class="comment" style="background: grey;"></div> <div class="comment" style="background: grey;"> ...

Experimenting with @media queries to dynamically resize images for various devices such as phones, tablets, and desktop

I'm struggling with making the background images on my website responsive. I have already added media queries to my page, but they seem ineffective and I am unsure of what else to try. Below are the HTML and CSS codes for the three images. Any suggest ...

Maintaining hover effects even when elements are not in view

I am facing an issue with my absolutely positioned <div> (which serves as a menu) located in the corner of a webpage. The problem arises when I try to animate it on hover, but as soon as the cursor moves beyond the viewport, the hover action stops. I ...

Activate the button when a checkbox is ticked or when using the "select all" checkbox

I'm facing an issue with a script that should enable a button when at least one checkbox is selected and a checkbox for selecting all checkboxes is used. The problem arises when I select the "select all" checkbox as it activates the button, but if I ...

Dealing with Overflow in CSS DIV Elements

Link to site: Suddenly, without any changes to the css, I noticed that when I expand the "browse" categories, they overflow outside of the containing divs. Here is the css code for the divs: .bodyArea { width: 900px; background-image:url(/images/ ...

Incorporating the Flipclock jQuery Plugin into an angular.js Web Application

Currently, I am experimenting with AngularJS and have the task of incorporating a jQuery plugin known as flipclock.js. After conducting extensive research, I stumbled upon this informative article discussing how to wrap jQuery in an Angular directive. Fol ...

Guide on properly formatting arithmetic expressions in a TextView for TalkBack recognition

In search of a way to make a fitness app more accessible, I need assistance in displaying a message on a smartwatch emulator that can be easily understood by users utilizing the TalkBack feature. The challenge lies in ensuring that the word 'plus&apos ...

The ellipsis text-overflow feature occasionally malfunctions during a single session when using IE 11

.show-ellipsis { white-space: nowrap; text-overflow: ellipsis; overflow: hidden; } <div class="container"> <div class="row show-ellipsis"> {{record.bodyName}} </div> </div> My web application supports versions o ...

The min-height attribute of the tooltip functions, but the min-width attribute does

I found a helpful tooltip on this site: I customized it to work with an input element: <div class="tooltip"> <input type="text" placeholder="Name" name="dp_name"> <span>Full name asd as dasd as dasd</span> </div> ...

The DropDownList does not automatically refill with the previous selection after a page refresh

I'm encountering an issue with my HTML website where the second DropDownList is not being populated when I refresh the page or go back from a redirected page. How can I fix this? You can check out the problem I'm facing at GiveToTheYToday.org if ...

What is the best way to combine two distinct SVG images?

I recently separated two SVGs that used to be one combined SVG to have better control over the layers. Now I am trying to overlap them in my code. Below is the code snippet: <Grid container direction="row" justify="center" style={{ ...

Please reset the form fields after the most recent edit

I've created a form that includes multiple select elements. When an option is selected, it activates the next select element and updates it with values using Ajax and PHP. However, I'm facing an issue where changing a previous option only resets ...

Validation of multiple textboxes can be achieved by utilizing either JavaScript or Angular 1

How can I implement a validation in Angular 1 or JavaScript that will enable the submit button only when there is text in any of the 3 textboxes? If no text is present, the button should be disabled. Below is the code snippet: First name: <br> < ...