Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force an <img> to take up width, even if the image is not loaded

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

like image 471
Sambo Avatar asked Sep 25 '10 00:09

Sambo


People also ask

How can I fix a picture without stretching it?

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.

How do I make an image fit in a div responsive?

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 you maintain the aspect ratio of an image?

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.


2 Answers

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.

like image 129
David Thomas Avatar answered Oct 26 '22 21:10

David Thomas


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.

like image 30
Tomas Aschan Avatar answered Oct 26 '22 21:10

Tomas Aschan