Skip to content

Instantly share code, notes, and snippets.

@patpaev
Last active November 2, 2015 23:41
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 patpaev/70c88040f95fd095c76b to your computer and use it in GitHub Desktop.
Save patpaev/70c88040f95fd095c76b to your computer and use it in GitHub Desktop.
Resize to fit image in div, and center horizontally and vertically
/* answer: http://stackoverflow.com/a/31699529 */
.container {
overflow: hidden;
width: 400px;
height: 300px;
position:relative
}
img {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto; /* this centers image inside */
width: 100%; /* this is to shrink the images of large width */
height: auto;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment