Adjusting and Image Size to Fit a Div with Bootstrap

Adjusting and image size to fit a div with Bootstrap

You can explicitly define the width and height of images, but the results may not be the best looking.

.food1 img {
width:100%;
height: 230px;
}

jsFiddle


...per your comment, you could also just block any overflow - see this example to see an image restricted by height and cut off because it's too wide.

.top1 {
height:390px;
background-color:#FFFFFF;
margin-top:10px;
overflow: hidden;
}

.top1 img {
height:100%;
}

How to fit an image inside a Bootstrap 3 div?

Just add you images width to 100%.

But as you are saying user will upload various kind of images, so can use object-fit property.

Add the CSS like this:

.fit-image{
width: 100%;
object-fit: cover;
height: 300px; /* only if you want fixed height */
}

<div class="col-sm-6 col-md-6 col-lg-4">
<img src="images/dummy_image.png" class="img-responsive fit-image">
</div>

You will find the details about object-fit and object-position here : https://css-tricks.com/on-object-fit-and-object-position/

Bootstrap 4: fitting image content inside parent div

I hope this is what u are expecting. I have added h-100classes to img tag and h-100 to img parent col tag

<div class="container-fluid h-100">
<div class="row h-25">
<div class="col h-100">
<img src="https://getbootstrap.com/docs/5.0/assets/img/bootstrap-icons.png" class="img-fluid h-100" alt="Responsive image">
</div>
</div>
<div class="row h-75">
<div class="col">
75 Percent Height
</div>
</div>
</div>

.row {
outline: 3px solid rgba(0, 0, 255, 1);
}

html,
body {
height: 100%;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">

<div class="container-fluid h-100">
<div class="row h-25">
<div class="col h-100">
<img src="https://getbootstrap.com/docs/5.0/assets/img/bootstrap-icons.png" class="img-fluid h-100" alt="Responsive image">
</div>
</div>
<div class="row h-75">
<div class="col">
75 Percent Height
</div>
</div>
</div>

How to scale image to fit the container?

Change the height and width to max-height and max-width. The image won't be any bigger than it's parent.

.thumbnail img {
max-height: 100%;
max-width: 100%;
}

Updated Fiddle

Stretch image to fit full container width bootstrap

Check if this solves the problem:

<div class="container-fluid no-padding">
<div class="row">
<div class="col-md-12">
<img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=1300%C3%97400&w=1300&h=400" alt="placeholder 960" class="img-responsive" />
</div>
</div>
</div>

CSS

.no-padding {
padding-left: 0;
padding-right: 0;
}

Css class no-padding will override default bootstrap container padding.

Full example here.


@Update
If you use bootstrap 4 it could be done even simpler

<div class="container-fluid px-0">
<div class="row">
<div class="col-md-12">
<img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=1300%C3%97400&w=1300&h=400" alt="placeholder 960" class="img-responsive" />
</div>
</div>
</div>

Updated example here.



Related Topics



Leave a reply



Submit