Skip to content

Instantly share code, notes, and snippets.

@cee-chen
Last active March 31, 2018 21:31
Show Gist options
  • Save cee-chen/fa7ec07132480c14b83085a81e58ff2e to your computer and use it in GitHub Desktop.
Save cee-chen/fa7ec07132480c14b83085a81e58ff2e to your computer and use it in GitHub Desktop.
Attempt to recreate object-fit: cover with just CSS. Not entirely perfect, some dependency on the media element's aspect ratio
// Parent container
.container {
position: relative;
width: 100vh;
height: 100vh;
overflow: hidden;
}
// Can be an img, video, or picture element
.media {
z-index: 0;
position: absolute;
top: 50%;
left: 50%;
max-width: 100vw;
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
transform: translateY(-50%) translateX(-50%);
// Image must have a square or wide aspect ratio
@media (orientation: portrait) {
max-width: none;
max-height: 100vh;
}
// If an image has a tall aspect ratio, detect it programmatically and give it this class
&--tall {
max-width: none;
max-height: 100vh;
@media (orientation: portrait) {
max-width: 100vw;
max-height: none;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment