Languages
[Edit]
EN

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

3 points
Created by:
Marcin
2462

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:

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.