Skip to content

Instantly share code, notes, and snippets.

@keithdevon
Created September 25, 2014 12:36
Show Gist options
  • Save keithdevon/c35d84ccd1afdc4ba40c to your computer and use it in GitHub Desktop.
Save keithdevon/c35d84ccd1afdc4ba40c to your computer and use it in GitHub Desktop.
Responsive vertically centered images
<div class="responsive-container responsive-container--square">
<img class="vertically-centered" src="path/to/image.jpg" />
</div>
.responsive-container {
position: relative;
padding-top: 0px; /* change this to accommodate YouTube chrome, e.g. 35px */
height: 0;
overflow: hidden;
background: white;
}
.responsive-container--square {
padding-bottom: 100%;
}
.responsive-container--16x9 {
padding-bottom: 56.25%;
}
.responsive-container--4x3 {
padding-bottom: 75%;
}
.vertically-centered {
position: absolute;
top:50%;
left: 0;
width: 100%;
height: auto;
transform: translateY(-50%);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment