JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://www.adweek.com/wp-content/uploads/files/blogs/istock-unfinished-business-hed-2015.jpg"></script>
<div class="bg">
</div>
CSS
* {
margin: 0;
padding: 0;
}
.bg {
background-image: url("http://www.adweek.com/wp-content/uploads/files/blogs/istock-unfinished-business-hed-2015.jpg");
width: 100%;
height: 100%;
background-size: cover;
background-repeat: no-repeat;
position: absolute;
}
.small {
background-size: contain;
}
JavaScript
var aspectRatio = 1920 / 1280;
var bg = document.getElementsByClassName('bg')[0];
window.addEventListener('resize', resizeListener);
function resizeListener() {
if (bg.offsetWidth / bg.offsetHeight <= aspectRatio) {
bg.className = 'bg small';
} else {
bg.className = 'bg';
}
}