I have some photos which have big sizes, I would like to set them to the Parent's dimension size. If one time the parent dimension is 1200x800, I would like to set the photo dimensions to 1200x800, but also I would like to see the whole image. If my parent dimension is 500x300, I would like the image to be 500x300 and so on.
Is this even possible? I want to shrink the image or expand it according to it's parent's dimension.
Thanks!
The image is set as a BLOCK element, min-width/height both set to 100% means to resize the image no matter of its size to be the minimum of 100% of it's parent. min is the key. If by min-height, the image height exceeded the parent's height, no problem.
If your image is portrait, and your container is landscape, you must set height="100%" on the image. If your image is landscape, and your container is portrait, you must set width="100%" on the image.
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.
The css property that you want is max-width
:
Css
img { max-width:100%; }
You could add a container around your image and set overflow:hidden
to prevent images to get bigger than the defined width/height.
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