Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ajithrn
Forked from rotassator/css-centre-crop-image.css
Created September 21, 2018 00:39
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 ajithrn/f516dd7443d70364c4c9a33f910cab2c to your computer and use it in GitHub Desktop.
Save ajithrn/f516dd7443d70364c4c9a33f910cab2c to your computer and use it in GitHub Desktop.
CSS: centre and crop an image with a fixed height
/* Centre and crop an image with a fixed height */
.crop {
position: relative; /* provide a positioning context */
overflow: hidden;
height: 200px; /* make space */
}
.crop img {
position: absolute;
left: -100%; /* anchor the image corners outside the viewable area (increase for large images) */
right: -100%;
top: -100%;
bottom: -100%;
width: auto; /* dynamic width based on viewable area */
height: 200px; /* set height (swap these for variable height) */
margin: auto; /* centre the image */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment