Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fit an image inside a Bootstrap 3 div?

I am using a div tag in code as shown below:

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

The user can upload any kind of image and have it displayed here. I want to fit the image inside of the div, meaning specifically that I need to cover the full div using that image as it's resized.

I am using Bootstrap 3.3.7. Please advise.

like image 298
user1770268 Avatar asked Jan 28 '18 03:01

user1770268


People also ask

How do I autofit an image in a div?

To auto-resize an image or a video to fit in a div container use object-fit property. It is used to specify how an image or video fits in the container. object-fit property: This property is used to specify how an image or video resize and fit the container.

How do I make an image fit in Bootstrap?

“bootstrap 4 image size to fit” Code Answer's you have to set the width of image to 100%, for that you can use Bootstrap class "w-100". keep in mind that "container-fluid" and "col-12" class sets left and right padding to 15px and "row" class sets left and right margin to "-15px" by default. make sure to set them to 0.

How do I fit an image in Col?

Use <img src = "images/dummy_image. png" class = "img-responsive" width = "100%" /> for fitting the column. This will fit your image width-wise into the column and will automatically modify the height (keeping the aspect ratio in mind), so you do not have to worry about image getting illogically resized.

How do I create a responsive image using bootstrap?

Create responsive images by adding an .img-responsive class to the <img> tag. The image will then scale nicely to the parent element. The .img-responsive class applies display: block; and max-width: 100%; and height: auto; to the image: You can also use Bootstrap's grid system in conjunction with the .thumbnail class to create an image gallery.

How to auto-resize an image to fit a DIV container using CSS?

How to auto-resize an image to fit a div container using CSS? To auto-resize an image or a video to fit in a div container use object-fit property. It is used to specify how an image or video fits in the container.

How do I add an image to a Div using CSS?

Watch a video course CSS - The Complete Guide (incl. Flexbox, Grid & Sass) Create a <div> element with a class "box". Set the URL of your image in the <img> tag with the src attribute and specify the alt attribute as well. Set the height and width of the <div>.

How to insert an image inside a Div having border properties?

By using custom css to insert a image inside a div having border properties. By using bootstrap. Let us see the demonstration of the above-stated ways. By providing a border to the image.


3 Answers

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/

like image 76
Zahidul Islam Ruhel Avatar answered Oct 23 '22 18:10

Zahidul Islam Ruhel


For Bootstrap 4

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

bootstrap.css

.img-fluid {
  max-width: 100%;
  height: auto
}
like image 38
Dawit Abraham Avatar answered Oct 23 '22 18:10

Dawit Abraham


It is safe to assume that by fitting you mean covering all of the width. This is because

  1. You typically do not know the height just by using col-sm-6 or col-md-6 or col-lg-4.
  2. There is a huge probability of loss in aspect ratio of the image if you try to resize it according to your own will.

Use <img src = "images/dummy_image.png" class = "img-responsive" width = "100%" /> for fitting the column. This will fit your image width-wise into the column and will automatically modify the height (keeping the aspect ratio in mind), so you do not have to worry about image getting illogically resized.

like image 33
Pranav Totla Avatar answered Oct 23 '22 18:10

Pranav Totla