Skip to content

Instantly share code, notes, and snippets.

@lujanfernaud
Last active September 27, 2018 05:35
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 lujanfernaud/7f600514523fa0cdd2d4d7113b2fc404 to your computer and use it in GitHub Desktop.
Save lujanfernaud/7f600514523fa0cdd2d4d7113b2fc404 to your computer and use it in GitHub Desktop.
CSS: Intrinsic Ratio

CSS: Intrinsic Ratio

Make the container of an image keep the size and aspect ratio of the image.

.intrinsic-ratio-container {
  position: relative;
  padding-bottom: 56.285714%; /* This defines the aspect ratio. */
  height: 0;
  color: #ddd; /* Same color as background-color to hide the image description text. */
  background-color: #ddd;
  overflow: hidden;
}

.intrinsic-ratio-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  color: transparent !important;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment