Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize image to parent div

I have an image with a 600px width.

It needs to be inside a bootstrap col-xs-6 or 12 div with a width of 300px and has to be resized if the screen width is smaller than 300px.

How can I realize that using width and max-width in CSS?

like image 376
Applecow Avatar asked Jul 14 '15 11:07

Applecow


People also ask

How do I stretch an image to fit in a div?

Answer: Use the CSS max-width Property You can simply use the CSS max-width property to auto-resize a large image so that it can fit into a smaller width <div> container while maintaining its aspect ratio.

How do I make div fit to parent div?

Method 1: First method is to simply assign 100% width and 100% height to the child div so that it will take all available space of the parent div. Consider this HTML for demonstration: HTML.

How do I make an image fit in HTML size?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels. For example, the original image is 640×960.


2 Answers

just do this:

img { 
  width: 100%; 
  max-width: 300px; 
  height: auto; 
}
like image 178
Pmpr.ir Avatar answered Oct 20 '22 17:10

Pmpr.ir


I think this will solve your problem.

img {
    width: 100%;
    max-width: 300px;
}

If the div width is bigger than 300px image will have maximum width of 300px. If div is smaller than 300px image should fill 100% width of div.

like image 22
hywak Avatar answered Oct 20 '22 15:10

hywak