Last active
          November 7, 2017 09:35 
        
      - 
      
 - 
        
Save JulienSansot/ad1fdb73a4f3e53237661e95c7a22b6b to your computer and use it in GitHub Desktop.  
    center image in div
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | - 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