css - Center image in DIV and hide the overflowing -


this kind of unique case. trying achieve following:

  1. square images should 100% 100%. fill out entire div (which squared)
  2. all images should fill out entire height of div.
  3. all images should aligned center
  4. if overflow x-axis, should overflow , hide

https://jsfiddle.net/cxnylxfn/2/

.image-container {     position: relative;     width: 300px;     height: 300px;     display: inline-block;     margin: 20px;     border: 1px solid black;     float: left;     overflow: hidden; }  .image-container img {     height: 100%;     position: absolute;     left: 0;     right: 0;     margin: 0 auto; } 

i achieved this, except centering images no matter what. google logo should centered , rest fine are. how achieve 4 requirements?

i use flex approach this:

.image-container {    display: flex;    width: 300px;    height: 300px;    border: 1px solid black;    overflow: hidden;    position: relative;    align-items: center;    justify-content: center;  }    img {    display: block;    height: 100%;  }
<div class="image-container">    <img src="https://assets-cdn.github.com/images/modules/logos_page/github-mark.png" />  </div>  <div class="image-container">    <img src="https://i.kinja-img.com/gawker-media/image/upload/s--peksmwzm--/c_scale,fl_progressive,q_80,w_800/1414228815325188681.jpg" />  </div>  <div class="image-container">    <img src="http://www.zevendesign.com/wp-content/uploads/2015/04/google-logo-progress-270x480.jpg" />  </div>  <div class="image-container">    <img src="https://cdn.pixabay.com/photo/2015/10/31/12/56/google-1015752_960_720.png" />  </div>


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -