I have a grid of fixed size divs, and this must remain CSS-only. Inside each div, I have a random size image inside.
I need the image to scale to the div size while keeping the aspect ratio, and also be centered both horizontally and vertically within the div.
  #holder {
    width: 800px;
    margin: 0 auto;
  }
  .tile {
    display: inline-block;
    padding: 10px 15px;
    border: 1px solid black;
    text-align: center;
    /*vertical-align: middle;*/
    width: 300px;
    height: 300px;
  }
  .tile img {
    /*vertical-align: middle;*/
    outline: 1px dashed red;
    max-height:100%;
    max-width:100%;
  }
Can't center vertically. Everything else seems to work fine.
UPDATE: Also this doesn't work when the img is smaller than the div.
 
     
     
     
    