Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontally and vertically centered iframe with aspect ratio 16:9 that uses as much screen estate as possible without being cropped anywhere

Requirements:

  • The HTML: The iframe HAS to be inside of a containing div. See code down below.
  • The CSS: The container should be able to have ANY valid width and height using the vw and vh viewport units. Se code down below.
  • Yes, the width and height HAS to be in vw and vh.
  • The static video preview image should NEVER be cropped.
  • The static video preview image should NOT have any black bars above and below (letterboxing).
  • The static video preview image should NOT have any black bars to the left or to the right (pillarboxing).
  • The static video preview image should use as much space estate as possible inside the div that contains it.
  • The static video preview image should ALWAYS keep its aspect ratio of 16:9.
  • Scrollbars should NEVER appear.
  • The static video preview image should be centered vertically as well as horizontally inside the div that contains it.
  • Responsive Web Design.

When resizing the browser or viewport all of the above requirements should be fulfilled.

HTML:

<div class="container">
   <iframe></iframe>
</div>

CSS:

.container {
   width:90vw;
   height:50vh;
}
like image 663
PussInBoots Avatar asked Jun 12 '16 18:06

PussInBoots


2 Answers

Same solution, but no extra markup for keeping the ratio.

JsFiddle with same comments totally not needed.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8">
<title>Fully Container Centred Iframe</title>
<meta name="generator" content="PSPad editor, www.pspad.com">
<style>
.container {
    display:table-cell; /* (specs: omitted table parts, the browser will insert mandatory elements in the dom tree) */
    position:relative;
    padding:; /* optional, margins ignored */
    width:100vw; /* any value */
    height:1vh; /* will expand by the :before element */
    overflow:hidden; /* hide eventual black bars */
    background:tan; /* bg-colors just for demo testing */
    vertical-align:middle;
    text-align:center;
}
.container:before {
    display:block;
    padding-top:56%; /* keeps the 16/9 ratio for the AP */
    height:0;
    background:red;
    content:"\a0";
}
.container iframe {
    position:absolute; /* to be ratio consistent */
    top:-.5%;
    left:-.5%; /* overflow eventual black bars */
    border:0;
    width:101%; /* grow some to avoid thinner black bars */
    height:101%;
    overflow:hidden; /* for html5 browsers the html attribute is depreciated */
    background:gold;
}
</style>
</head><body>

<div class="container">
    <iframe scrolling="no" src=""></iframe>
</div>

</body></html>
like image 87
Erik.J Avatar answered Oct 17 '22 08:10

Erik.J


Using JavaScript, you can listen for the resize event, which fires whenever the browser's window changes shape. Then, with some simple algebra you can calculate the dimensions of the iframe based on the dimensions of the container. Here is a demo that shows all of the requirements.

"use strict";

var container = document.querySelector('.container');
var frame = container.querySelector('iframe');

function resizeVideo() {
	frame.width = frame.height = 0;

	var width = container.offsetWidth;
	var height = container.offsetHeight;

	if (height * (16 / 9) <= width) {
		frame.height = height;
		frame.width = height * (16 / 9);
	} else {
		frame.width = width;
		frame.height = width * (9 / 16);
	}
}

window.addEventListener('load', resizeVideo);
window.addEventListener('resize', resizeVideo);
.container {
	width: 90vw;
	height: 50vh;

	display: table-cell;
	text-align: center;
	vertical-align: middle;
}
<div class="container">
	<iframe src="https://www.youtube.com/embed/BKhZvubRYy8" frameborder="0" allowfullscreen></iframe>
</div>
like image 33
rvighne Avatar answered Oct 17 '22 07:10

rvighne