Skip to content

Instantly share code, notes, and snippets.

@JulienSansot
Last active November 7, 2017 09:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JulienSansot/ad1fdb73a4f3e53237661e95c7a22b6b to your computer and use it in GitHub Desktop.
Save JulienSansot/ad1fdb73a4f3e53237661e95c7a22b6b to your computer and use it in GitHub Desktop.
center image in div
- 1
the image uses the full width or full height of the div
we always see all the image, nothing is cropped
<div class="container"></div>
.container {
width: 500px;
height: 300px;
border: 1px solid black;
background-image: url("http://placehold.it/200x400");
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
- 2
the image always covers completely the div by using full width and height.
image is cropped and center vertically or horizontally
http://stackoverflow.com/questions/11757537/css-image-size-how-to-fill-not-stretch
<div class="container"></div>
.container {
width: 500px;
height: 300px;
border: 1px solid black;
background-image: url("http://placehold.it/200x400");
background-repeat: no-repeat;
background-position: 50% 50%;
background-size: cover;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment