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?
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.
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.
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.
just do this:
img {
width: 100%;
max-width: 300px;
height: auto;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With