Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use CSS to set both the Width and Height of the image to 100% and the image will be automatically stretched to fill the containing div, without the need for jquery. </p> <p>Also, you will not need to center the image as it will already be stretched to fill the div (centered with zero margins).</p> <p>HTML:</p> <pre><code>&lt;div id="containingDiv"&gt; &lt;img src=""&gt; &lt;/div&gt; </code></pre> <p>CSS:</p> <pre><code>#containingDiv{ width: 200px; height: 100px; } #containingDiv img{ width: 100%; height: 100%; } </code></pre> <p>That way, if your users have javascript disabled, the image will still be stretched to fill the entire div width/height.</p> <p>OR</p> <p>The JQuery way (SHRINK/STRETCH TO FIT - INCLUDES WHITESPACE):</p> <pre><code>$('img.shelf-img').each(function(i, item) { var img_height = $(item).height(); var div_height = $(item).parent().height(); if(img_height&lt;div_height){ //IMAGE IS SHORTER THAN CONTAINER HEIGHT - CENTER IT VERTICALLY var newMargin = (div_height-img_height)/2+'px'; $(item).css({'margin-top': newMargin }); }else if(img_height&gt;div_height){ //IMAGE IS GREATER THAN CONTAINER HEIGHT - REDUCE HEIGHT TO CONTAINER MAX - SET WIDTH TO AUTO $(item).css({'width': 'auto', 'height': '100%'}); //CENTER IT HORIZONTALLY var img_width = $(item).width(); var div_width = $(item).parent().width(); var newMargin = (div_width-img_width)/2+'px'; $(item).css({'margin-left': newMargin}); } }); </code></pre> <p>The JQuery way - CROP TO FIT (NO WHITESPACE):</p> <pre><code>$('img.shelf-img').each(function(i, item) { var img_height = $(item).height(); var div_height = $(item).parent().height(); if(img_height&lt;div_height){ //INCREASE HEIGHT OF IMAGE TO MATCH CONTAINER $(item).css({'width': 'auto', 'height': div_height }); //GET THE NEW WIDTH AFTER RESIZE var img_width = $(item).width(); //GET THE PARENT WIDTH var div_width = $(item).parent().width(); //GET THE NEW HORIZONTAL MARGIN var newMargin = (div_width-img_width)/2+'px'; //SET THE NEW HORIZONTAL MARGIN (EXCESS IMAGE WIDTH IS CROPPED) $(item).css({'margin-left': newMargin }); }else{ //CENTER IT VERTICALLY (EXCESS IMAGE HEIGHT IS CROPPED) var newMargin = (div_height-img_height)/2+'px'; $(item).css({'margin-top': newMargin}); } }); </code></pre>
    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