Piyush Dive
Piyush Dive

Reputation: 172

how to fit image inside a div?

I am building an instagram clone & currently working in posts so my question is that, i have created a card and inside of that i have created a div containing posts(images) but the problem is that, when i upload images with multiple size it straches my card & it doesn't get fit inside of that div. Also my right div changes according images...

This is small image This is small image

This is big image This is big image

Code:

<div class="mx-auto w-75">
    <div class="card my-3">
        <div class="card-body p-0">
            <div class="row">
                <div class="col-sm-8 ms-2 mb-0 ps-1 pe-0">
                    <div id="carouselExampleFade" class="carousel m-0 slide carousel-fade" data-bs-ride="carousel">
                        <div class="carousel-inner">
                            <div class="carousel-item active">
                                <img src="{% static 'images/post-1.jpg' %}" style="height:auto;" class="d-block w-100 img-fluid" alt="">
                            </div>
                            <div class="carousel-item">
                                <img src="{% static 'images/profile-1.jpg' %}" style="height:auto;" class="d-block w-100 img-fluid" alt="">
                            </div>
                            <div class="carousel-item">
                                <img src="{% static 'images/profile-2.jpg' %}" style="height:auto;" class="d-block w-100 img-fluid" alt="">
                            </div>
                        </div>
                        <button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleFade" data-bs-slide="prev">
                            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                            <span class="visually-hidden">Previous</span>
                        </button>
                        <button class="carousel-control-next" type="button" data-bs-target="#carouselExampleFade" data-bs-slide="next">
                            <span class="carousel-control-next-icon" aria-hidden="true"></span>
                            <span class="visually-hidden">Next</span>
                        </button>
                    </div>
                </div>
                <div class="col-sm-3 p-0">
                    <div class="card pt-0" style="width:325px; height:72px; padding:16px; margin-top:-1px;">
                        <div class="card-body">
                            
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Upvotes: 1

Views: 11516

Answers (1)

Rvector
Rvector

Reputation: 2442

To fit an image in a div, use the css object-fit property.

html code

<div>
    <img src="abidjan.jpg" alt="Abidjan" width="400" height="300">
<div>

NB : Note the width and height attributes of the img.

CSS code

img {
    width: 200px;
    height: 300px;
    object-fit: fill;
    /* You can also use cover instead of fill but they are different */
}

The image is squished to fit the container of 200x300 pixels (its original aspect ratio is destroyed)

More info here

Upvotes: 4

Related Questions