5 replies [Last post]
larmyia
Offline
Elder
London
Last seen: 11 years 47 weeks ago
London
Timezone: GMT+1
Joined: 2005-01-25
Posts: 1060
Points: 0

hello.

I was wondering why exactly you should declare the height and width of an image. I'm doing it but have no idea why...

I've read (what seems like!) loads of stuff and it's not mentioned as far as I can see.

thanks

larmyia

thepineapplehead
thepineapplehead's picture
Offline
Guru
Last seen: 20 weeks 5 days ago
Joined: 2004-06-30
Posts: 9668
Points: 801

<img> width and height declaration

As fas as I know, the page won't validate without the width and height attribute.

I think it may be to do with broken images still displaying the right sized placeholders, although even if you don't specify a width and height, it shows it the same.

Interesting point, I'd like to hear other responses.

Verschwindende wrote:
  • CSS doesn't make pies

antibland
antibland's picture
Offline
Leader
Pittsburgh
Last seen: 11 years 31 weeks ago
Pittsburgh
Joined: 2005-01-17
Posts: 603
Points: 0

<img> width and height declaration

The height and width attributes should be included to speed up the page a little. The browser has a smidgen less work to do when you feed it the dimensions of your image. If you don't provide these dimensions, the browser needs to examine the image in greater depth to determine these dimensions before rendering the document.

jmcn
Offline
newbie
Savannah, GA
Last seen: 15 years 29 weeks ago
Savannah, GA
Timezone: GMT+2
Joined: 2005-01-17
Posts: 5
Points: 0

<img> width and height declaration

Back in the "old days" before broadband you used to have to wait for the images to load. If the web page didn't have the images height and width from the <img> tag the browser would assume a very small size for the image and only allocate a tiny square. Once the image finally loaded it would pop into place and would then throw off the content on the rest of the page since it was no longer a tiny little square of unknown dimensions. Height and width declarations were essential then so that the browser would reserve the right amount of space for all the images while they took hours to load.

Nowadays the browsers are smarter and they load the images height and width before the image itself actually loads. (They know the images size before it loads.) So they leave the right amount of room. Connections are much faster too, so most pages will load all at once, images included, with broadband connections.

larmyia
Offline
Elder
London
Last seen: 11 years 47 weeks ago
London
Timezone: GMT+1
Joined: 2005-01-25
Posts: 1060
Points: 0

&lt;img&gt; width and height declaration

ahhhh, now that all makes much more sense! I hate writing code that I don't understand Smile

ta very much

larmyia

Cyberman
Cyberman's picture
Offline
Regular
Last seen: 15 years 26 weeks ago
Joined: 2005-02-11
Posts: 37
Points: 0

&lt;img&gt; width and height declaration

jmcn wrote:
Back in the "old days" before broadband

Not just in the old days - even today I see sites(image heavy sites like fora) where you have to wait till it´s completely rendered before you can start to read - because the text is jumping around all the time...

Also, providing the image dimensions means that your site layout won´t break just because the image isn´t available.