Languages
[Edit]
EN

JavaScript - fill/cover with image all div container keeping image ratio

3 points
Created by:
Walter
586

In this article we are going to show how in JavaScript fill with image all div to cover all available space keeping image ratio.

Note: for pure CSS version with img element read this article.

CSS with JavaScript conditional styling example

In this section we select one case of styling depending on containing div and image ratio to fill all available div space and do not lose image ratio, centering image vertically and horizontally.

The effect is similar to background-size: cover; but with additional image centering.

Note: it is important to set container size in this example.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    #container1 { border: 1px solid red; width: 120px; height: 200px; }
    #container2 { border: 1px solid red; width: 300px; height: 120px; }

  </style>
</head>
<body>
  <div id="container1">
    <img id="image1" src="https://dirask.com/static/bucket/1590005168287-pPLQqVWYa9--image.png">
  </div>
  <br />
  <div id="container2">
    <img id="image2" src="https://dirask.com/static/bucket/1590005168287-pPLQqVWYa9--image.png">
  </div>
  <script>

    function centerImage(hContainer, hImage, callback)
    {
        hContainer.style.position = 'relative';
        hContainer.style.overflow = 'hidden';
        hImage.style.position = 'absolute';
        hImage.style.left = '50%';
        hImage.style.top = '50%';
        hImage.style.transform = 'translate(-50%, -50%)';

        hImage.addEventListener('load', function()
        {
            var containerWidth = hContainer.offsetWidth;
            var containerHeight = hContainer.offsetHeight;
            var pictureWidth = hImage.offsetWidth;
            var pictureHeight = hImage.offsetHeight;

            var containerRadio = containerWidth / containerHeight;
            var pictureRadio = pictureWidth / pictureHeight;

            if(containerRadio > pictureRadio)
            {
                hImage.style.width = '100%';
                hImage.style.minWidth = '100%';
                hImage.style.maxWidth = '100%';
                hImage.style.height = 'auto';
                hImage.style.minHeight = 'none';
                hImage.style.maxHeight = 'none';
            }
            else
            {
                hImage.style.width = 'auto';
                hImage.style.minWidth = 'none';
                hImage.style.maxWidth = 'none';
                hImage.style.height = '100%';
                hImage.style.minHeight = '100%';
                hImage.style.maxHeight = '100%';
            }

            if(callback)
                callback(true);
        });

        hImage.addEventListener('error', function()
        {
            if(callback)
                callback(false);
        });
    };

    // Usage example:

    var container1 = document.querySelector('#container1');
    var container2 = document.querySelector('#container2');
    var image1 = document.querySelector('#image1');
	var image2 = document.querySelector('#image2');

    centerImage(container1, image1);
	centerImage(container2, image2);

  </script>
</body>
</html>

Used resource:

Alternative titles

  1. JavaScript - position image in div filling/covering all available space keeping image ratio
  2. JavaScript - image size to cover div container with centering inside div
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join