Skip to content

Instantly share code, notes, and snippets.

@moreta
Created December 18, 2019 04:20
Show Gist options
  • Save moreta/cf90b9dd21598e4250c4187fa68a046b to your computer and use it in GitHub Desktop.
Save moreta/cf90b9dd21598e4250c4187fa68a046b to your computer and use it in GitHub Desktop.
ratio media - image, video
.ratio {
position: relative;
width: 100%;
overflow: hidden;
&:before {
content: "";
display: block;
padding-top: 75%; /* 4:3 - 75%, 3:4 - 133.33%, 16:9 - 56.25% , 3:2 - 66.66%, 8:5 - 62.5% */
}
img {
position: absolute;
width: 100%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 2;
}
video {
position: absolute;
width: 100%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 4;
}
}
<div class="ratio">
<img src="https://xxx">
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment