Adjust the iframe video size to match the page layout

I am currently working on a web page that incorporates a video background with no sound in the background. While I can easily achieve this using a locally saved video and the HTML video tag, I am encountering issues when using an iframe as the styles do not seem to apply properly. My goal is to style the embed so that it fills the entire page, scaling appropriately for mobile users instead of just fitting itself to the page while maintaining its aspect ratio.

.banner {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
<div class="banner">
  <iframe title="vimeo-player" src="https://player.vimeo.com/video/76979871" width="100%" height="100%" frameborder="0" allow="autoplay;" allowfullscreen></iframe>
</div>

Answer №1

It appears that Vimeo displays videos in a similar manner to using the CSS property object-fit: contain. This means the video is shown uncropped with either full height or full width depending on how its aspect ratio compares to the viewport.

However, if the goal is to have the video cover the entire viewport while maintaining its aspect ratio, then the solution lies in utilizing object-fit: cover. This approach will ensure that the whole viewport is covered by the video, with the sides being cropped or the top and bottom depending on the relationship between the video's aspect ratio and the viewport's.

To achieve this effect, a JavaScript function can be employed to dynamically adjust the video size based on the actual viewport dimensions relative to the video's aspect ratio. By calculating the appropriate scale using transform: scale(), the video can be expanded to cover the viewport as needed.

In the provided code snippet, it assumes a 16:9 aspect ratio for the video. While this ratio is common, users may need to determine the exact aspect ratio for different videos if necessary.

The code includes styling elements for a fixed-position outer container containing a div for the video player. The script calculates the scaling factor required to fit the video within the viewport dimensions and updates the video accordingly upon loading or window resizing events.

Note that the code may not run directly as an SO snippet due to potential nested iframes conflicts. However, it has been successfully tested on Edge and Chrome browsers running on Windows 10, along with various emulators, without issues. For practical usage, adjust the positioning of the container elements as needed within the document structure.

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

Can someone provide guidance on creating a JavaScript function that locates an image within an <li> element and sets that image as the background-image property in the li's CSS?

Let's dive deeper into this concept: <li class="whatever"> <img src="/images/clients/something.jpg"> </li> <li class="whatever"> <img src="/images/clients/whatever.png"> </li> He ...

Initiate the transmission of numerous files immediately following a specified URL

It's interesting how websites like Facebook are able to directly send their .js and .css files when you visit the site. Most "normal" sites typically send back the index.html or index.php first, followed by the necessary .js and .css files later on. ...

What is the best way to create a Div element that functions as a button, becoming active when clicked while deactivating all other Div elements

function toggleButton1() { $("#button1").toggleClass("button-active button-inactive"); $("#button2").toggleClass("button-inactive button-active"); $("#button3").toggleClass("button-inactive button-active"); } function toggleButton2() { $("#butto ...

How to keep text always locked to the front layer in fabric.js without constantly bringing it to the front

Is it possible to achieve this functionality without using the following methods? canvas.sendBackwards(myObject) canvas.sendToBack(myObject) I am looking to upload multiple images while allowing them to be arranged forward and backward relative to each o ...

Utilizing jQuery's .clone() function to duplicate HTML forms with radio buttons will maintain the radio events specific to each cloned element

I'm currently developing front-end forms using Bootstrap, jQuery, HTML, and a Django backend. In one part of the form, users need to input "Software" information and then have the option to upload the software file or provide a URL link to their hoste ...

I am eager to incorporate my own unique 'add to cart' button

Currently, I'm utilizing the Motta theme for my WooCommerce store. However, I find the add to cart button to be too large for my liking. I am hoping to simplify it to resemble a plus button. Any assistance in achieving this would be greatly appreciate ...

What is the best way to adjust the width and height of react-flip-page through CSS for different media queries?

Incorporating the react-flip-page npm package to add animation to a book on my web application has been exciting. However, I've encountered an issue with adjusting the width when using media queries due to the available props for setting size and colo ...

Choose an image for a better view with the use of HTML and JavaScript/JQuery

Requesting assistance in creating a simple code using plain html5 and JavaScript/jQuery (without plugins) that will enlarge an image upon clicking on it from a list of images. Here is the HTML snippet provided: <!-- Large image holder --> & ...

Tips for including a lang attribute in HTML tags within Next.js

When I ran a performance check on my Next.js portfolio site, I discovered that the main index.html is missing a lang attribute. This absence results in a deduction from the accessibility score. I could add the locale by configuring i18n setup in the next. ...

Tips for maintaining HTML lines within an 80-character limit

Someone mentioned to me the importance of keeping HTML lines under 80 characters. My code editor, Atom, even displays a reminder for this limit. I've come across many discussions on this topic but haven't found any examples. For instance, consid ...

If an AngularJS Form Item is hidden with jQuery's hide() method, will it still be validated?

Even though we're in 2020, I'm still working with AngularJS 1.x instead of the newer version due to work requirements. Despite this limitation, I am facing a challenge with setting up a form that requires exclusive-or validation between two field ...

Addressing the Summer Note Problem within a Bootstrap Popup

I am currently facing an issue with the functionality of the Summernote Text plugin in my project. The text inside the Summernote editor does not display what I am typing until I resize the browser window. Below is the code snippet for reference: <%-- ...

Elusive shadow fails to appear in ThreeJS scene

<style> body { margin: 0; } </style> <script async src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0267712f6f6d66776e672f716a6a6f716a6f717a333c343c71">[email protec ...

Easily transform a CSS file for optimal use on dark backgrounds from scratch

I've got around 200 CSS files that look like this: /** * GeSHi Dynamically Generated Stylesheet * -------------------------------------- * Dynamically generated stylesheet for bnf * CSS class: , CSS id: * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 ...

phperror message is included and echoed

I am facing an issue while trying to utilize the php include tag for my header and footer sections. Even though my index file and header.php file seem to be error-free, the included file is not showing up when I preview it in a browser. Is there an easy fi ...

Tips for changing font styles using Polymer

I'm new to working with Polymer.js and CSS. My goal is to have text in todo.item display normally when todo.done is false, and with a line-through style when todo.done is true. I'm considering using CSS for this task, but I'm unsure how to ...

Use either Jquery or CSS3 to hide a div if one of its inner divs is empty

I have multiple data sets to display in divs with the same class name. Each div contains two inner divs. <div class="div1"> <div class="div2">Abc</div> <div class="div3"><?php echo $row['SOME VALUE']; ?>< ...

Learn the process of showcasing database content on a webpage with an interactive feature that enables users to choose and access additional details

Apologies if this question has been asked before, I have searched for a solution but my web development knowledge is limited. To better understand my issue, you can visit the site at 000freewebhost by following this link: In summary, I am trying to select ...

Poor quality picture captured with the use of the getUserMedia() Javascript function

Is there a way to improve the resolution of mobile phone camera screenshots taken with JavaScript's getUserMedia function? if (navigator.mediaDevices) { navigator.mediaDevices.getUserMedia({ video: { width: { min: 1280, }, heig ...

An alternative to CSS clearfix for children with fixed positioning

I understand that using clearfix can help with the zero-height container issue for floated elements. However, I am curious if there is a similar technique to clearfix for fixed elements? I am currently stuck trying to implement the clearfix method. .n ...