Skip to content

Instantly share code, notes, and snippets.

@dm-heinze
Created April 29, 2021 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dm-heinze/605e722f571f43a9838d5249b816e62f to your computer and use it in GitHub Desktop.
Save dm-heinze/605e722f571f43a9838d5249b816e62f to your computer and use it in GitHub Desktop.
.wrapper-class {
display: block;
position: relative;
overflow: hidden;
height: 0;
// Give parent container a percentile padding to top, to reserve place of image.
// Then just position the child element (image) absolute.
// height px / width px * 100 = ratio in %
// e.g: 536 / 1024 * 100 = 52,344%
padding-top: 52.344%;
img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover; // Optional: if some images of your collection does have a different ratio
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment