Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://jsfiddle.net/itay1989/J7a5R/10/" rel="noreferrer"><strong>jsFiddle Demo</strong></a></p> <pre><code>div { width: 200px; height: 200px; overflow: hidden; margin: 10px; position: relative; } img { position: absolute; margin: auto; min-height: 100%; min-width: 100%; /* For the following settings we set 100%, but it can be higher if needed See the answer's update */ left: -100%; right: -100%; top: -100%; bottom: -100%; } </code></pre> <hr> <h1>Update - Improvement</h1> <p>As <a href="https://stackoverflow.com/users/87015/salman-a">Salman A</a> mentioned in the comments, the original solution has a flaw - it doesn't work well if the <em>img</em>'s height or width (or both) are (at least) 3 times* larger than the containing <em>div</em>. </p> <p>The problem is shown <a href="http://jsfiddle.net/itay1989/J7a5R/768/" rel="noreferrer">here</a>.</p> <p>Salman also gave a really simple and elegant fix. We just need to update the <em>img</em>'s position coordinates (top, left, bottom, right) to work with higher percents. The following fix works with <em>1000%</em>, but of course you can adjust it according to your needs.</p> <p>The fix is shown <a href="http://jsfiddle.net/itay1989/J7a5R/769/" rel="noreferrer">here</a>.</p> <p><a href="https://i.stack.imgur.com/AQOyv.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/AQOyv.jpg" alt="Before and after the fix"></a></p> <p>* The reason is that when we set the <em>img</em>'s <em>left</em> and <em>right</em> (or: <em>top</em> and <em>bottom</em>) coordinates to be <em>-100%</em> (of the containing <em>div</em>), the overall allowed <em>width</em> (or: <em>height</em>) of the <em>img</em>, can be at most <em>300%</em> of the containing <em>div</em>'s <em>width</em> (or: <em>height</em>), because it's the sum of the <em>div</em>'s width (or: <em>height</em>) and the <em>left</em> and <em>right</em> (or: <em>top</em> and <em>bottom</em>) coordinates.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload