console
var video = document.querySelector('video'),
container = document.querySelector('#container');
var setVideoDimensions = function() {
var w = video.videoWidth,
h = video.videoHeight;
var videoRatio = (w / h).toFixed(2);
var containerStyles = window.getComputedStyle(container),
minW = parseInt(containerStyles.getPropertyValue('width')),
minH = parseInt(containerStyles.getPropertyValue('height'));
var widthRatio = minW / w,
heightRatio = minH / h;
if (widthRatio > heightRatio) {
var newWidth = minW;
var newHeight = Math.ceil(newWidth / videoRatio);
} else {
var newHeight = minH;
var newWidth = Math.ceil(newHeight * videoRatio);
}
video.style.width = newWidth + 'px';
video.style.height = newHeight + 'px';
};
video.addEventListener('loadedmetadata', setVideoDimensions, false);
window.addEventListener('resize', setVideoDimensions, false);
<div id="container">
<video autoplay loop>
<source src="https://www.videezy.com/assets/videezy/masthead-video.mp4"
type="video/mp4">
</video>
<div id="overlay">
</div>
</div>
* {
margin: 0;
padding: 0;
}
#container {
overflow: hidden;
height: 400px;
background: #edeae8;
position: relative;
}
video {
position: absolute;
left: 50%;
transform: translateX(-50%);
top: 0;
}
#overlay {
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
}