Last active
March 31, 2018 21:31
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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