Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to resize an image to a css-grid

I am trying to resize an image to a css grid. However, the image is not "stretching" to the grid cell. Any ideas how I can make this happen?

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Test 4x4 responsive grid</title>
        <style>

        #grid-wrapper {
            display: grid;
            grid-template-columns: 1fr 1fr;
            aspect-ratio: 16 / 9;
        }

        #picture {
            width: 100%;
            height: 100%;
        }

        #img {
            width: 100%;
            height: 100%;
        }
        </style>
    </head>

    <body>
        <div id="grid-wrapper">
            <div id="picture">
                <picture>
                    <source srcset="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" type="image/jpeg">
                    <img alt="Test image" src="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" />
                </picture>
            </div>

            <div>
                <picture>
                    <source srcset="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" type="image/jpeg">
                    <img alt="Test image" src="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" />
                </picture>
            </div>

            <div>
                <picture>
                    <source srcset="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" type="image/jpeg">
                    <img alt="Test image" src="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" />
                </picture>
            </div>

            <div>
                <picture>
                    <source srcset="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" type="image/jpeg">
                    <img alt="Test image" src="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" />
                </picture>
            </div>
        </div>
    </body>
</html>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can archive this by using the image as background. What you already mentioned. But In your original code the problem was the selector. instead #img use only the img tag name for example.

        #grid-wrapper {
            display: grid;
            grid-template-columns: 1fr 1fr;
            aspect-ratio: 16 / 9;
        }


        img {
            width: 50%;
            height: 50%;
        }
        <div id="grid-wrapper">
            <div id="picture">
                <picture>
                    <source srcset="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" type="image/jpeg">
                    <img alt="Test image" src="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" />
                </picture>
            </div>

            <div>
                <picture>
                    <source srcset="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" type="image/jpeg">
                    <img alt="Test image" src="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" />
                </picture>
            </div>

            <div>
                <picture>
                    <source srcset="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" type="image/jpeg">
                    <img alt="Test image" src="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" />
                </picture>
            </div>

            <div>
                <picture>
                    <source srcset="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" type="image/jpeg">
                    <img alt="Test image" src="https://upload.wikimedia.org/wikipedia/commons/9/96/Serbian_artilery_at_Adrianopoli.png" />
                </picture>
            </div>
        </div>
    </body>
</html>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading