0

I know, it has been asked before. The answers are insufficient although they are marked as correct.

I need something that fills a container with an image by showing the most of the image, centers it and preserves its ratio. Should be ajquery-plugin, a angular-directive or plain JS.

<div ratio-fill>
  <img>
</div>

Of course should be enough where the script takes action then.


Solution for interest:

  <div ratio-fill="http://img.url"></div>

CSS

  *[ratio-fill] { background-size: cover;
    background-position: center; }

Script (jQuery)

  /* img-ratio-fill */
  $(window).load(function(){

   /* ratio-fill-directive */
   $('*[ratio-fill]').each(function() { 

      var $this = $(this),
          imgUrl = $this.attr( "ratio-fill" );

      $this.css( "background-image", "url('" + imgUrl + "')" );

    });

  })
Joao Polo
  • 2,153
  • 1
  • 17
  • 26
Chris Pillen
  • 810
  • 8
  • 27

1 Answers1

2

with css on the DIV background-size: cover; background-position: center;

Dux
  • 309
  • 1
  • 5