I'm loading a bunch of img's, and I'd like them to take up space on the document even if they are not loaded, or have not completed loading yet.
I tried specifying width and height (both as attributes themselves, and within a style attribute), and find it frustrating that the images will not take up space if they don't load.
Surely, there must be a way to force an img to specific dimensions, **even if that image fails to load.
Thanks
How to fit image without stretching and maintain aspect ratio? Answer: If you want to use the image as a CSS background, there is an elegant solution. Simply use cover or contain in the background-size CSS3 property. contain will give you a scaled-down 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.
By setting the width property to 100%, you are telling the image to take up all the horizontal space that is available. With the height property set to auto, your image's height changes proportionally with the width to ensure the aspect ratio is maintained. The end result is an image that scales up or down perfectly.
There certainly is:
<img src="path/to/image.png" height="50" width="20" />
Also, the reason specifying the width and height within the style="/* css */"
attribute didn't work is because images are, by default, in-line elements. Had you specified display: block
the image would've accepted the width
and height
values.
If you add, to the css/style:
display: inline-block;
It should work. I'm not sure why Firefox doesn't respect the width/height attributes, but still. Even IE, with a defined doctype
should respect display: inline-block
, since img
elements are, by default, in-line anyway.
The simple answer: wrap the image in a <div>
with a fixed width.
<div style="width: 400px;"><img src="404.png" /></div>
If you set padding, border etc to 0
, you won't notice that the div is there.
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