Center middle image and maintain aspect ratio
// source http://jsbin.com/yefeju
'use strict';
var images = document.getElementsByClassName('image');
Array.prototype.map.call(images, function (img) {
var temp = new Image();
temp.src = img.src;
var aspectRatio = temp.width / temp.height;
if (aspectRatio > 1) {
img.style = 'height: 100%';
} else {
img.style = 'width: 100%';
}
});html, body {
height: 100%;
}
.center {
height: 100%;
display: flex;
flex-wrap: wrap;
flex-direction: row;
align-items: center;
justify-content: center;
}
.section {
width: 300px;
height: 300px;
margin: 1rem;
display: flex;
flex-wrap: nowrap;
flex-direction: column;
align-items: center;
justify-content: center;
}
.container {
position: relative;
width: 100%;
height: 0;
overflow: hidden;
padding-top: 100%;
}
.image {
position: absolute;
display: block;
right: 0;
bottom: 0;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Center middle image and maintain aspect ratio</title>
<style id="jsbin-css">
html, body {
height: 100%;
}
.center {
height: 100%;
display: flex;
flex-wrap: wrap;
flex-direction: row;
align-items: center;
justify-content: center;
}
.section {
width: 300px;
height: 300px;
margin: 1rem;
display: flex;
flex-wrap: nowrap;
flex-direction: column;
align-items: center;
justify-content: center;
}
.container {
position: relative;
width: 100%;
height: 0;
overflow: hidden;
padding-top: 100%;
}
.image {
position: absolute;
display: block;
right: 0;
bottom: 0;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="center">
<div class="section">
Landscape Image
<div class="container">
<img src="http://unsplash.it/1600/900" class="image" />
</div>
</div>
<div class="section">
Portrait Image
<div class="container">
<img src="http://unsplash.it/900/1600" class="image" />
</div>
</div>
</div>
<script id="jsbin-javascript">
'use strict';
var images = document.getElementsByClassName('image');
Array.prototype.map.call(images, function (img) {
var temp = new Image();
temp.src = img.src;
var aspectRatio = temp.width / temp.height;
if (aspectRatio > 1) {
img.style = 'height: 100%';
} else {
img.style = 'width: 100%';
}
});
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Center middle image and maintain aspect ratio</title>
</head>
<body>
<div class="center">
<div class="section">
Landscape Image
<div class="container">
<img src="//unsplash.it/1600/900" class="image" />
</div>
</div>
<div class="section">
Portrait Image
<div class="container">
<img src="//unsplash.it/900/1600" class="image" />
</div>
</div>
</div>
</body>
</html></script>
<script id="jsbin-source-css" type="text/css">html, body {
height: 100%;
}
.center {
height: 100%;
display: flex;
flex-wrap: wrap;
flex-direction: row;
align-items: center;
justify-content: center;
}
.section {
width: 300px;
height: 300px;
margin: 1rem;
display: flex;
flex-wrap: nowrap;
flex-direction: column;
align-items: center;
justify-content: center;
}
.container {
position: relative;
width: 100%;
height: 0;
overflow: hidden;
padding-top: 100%;
}
.image {
position: absolute;
display: block;
right: 0;
bottom: 0;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}</script>
<script id="jsbin-source-javascript" type="text/javascript">const images = document.getElementsByClassName('image');
Array.prototype.map.call(images, img => {
let temp = new Image();
temp.src = img.src;
const aspectRatio = temp.width / temp.height;
if (aspectRatio > 1) {
img.style = 'height: 100%';
} else {
img.style = 'width: 100%';
}
});</script></body>
</html>