views:

52

answers:

6

The HTML tag img can have width and height property, while it can also have CSS style width and height.

    <img src="xxx.img" width="16" height="16" 
style="width: 16px; height: 16px"></img>

What's the difference between the HTML property and CSS attributes? Should they have same effects?

A: 

you can try this

<img src="some_pic.jpg" width="100" />

it's height will be auto-size.

and this

<img src="some_pic.jpg" style="width:100px" />

it's height will not be auto-size.

just try more,and you know the difference

Tim Li
What's auto-size? Both properties are the same.
Marko
I try it just now.it's the same.sorry for my mistake.
Tim Li
Have you tried it? Setting 1 of the properties in CSS (either width or height) will still make the image resize to fit it's proportions.
Marko
A: 

I made a comparison up at: http://jsfiddle.net/jF8y6/ with 4 different states. The main difference is the way it is used via external stylesheets in terms of the ability to resize images for different stylesheets (desktop, mobile, print, etc) and the flexibility it brings. If you hardcode the sizes then it stops the flexibility.

Alex
A: 

http://www.w3schools.com/tags/tag_IMG.asp

  • I haven't used the image tag in a while for this purpose. But there is a difference, the attributes can be relative to the images dimensions, CSS style properties are absolute (either %, px, em...)
Tom
How can you use dimensions relative to the image's dimensions? I'm confused
Marko
A: 

They have the same effect.

<img width="100" height="100" /> has been used for a long time, same with the widht/height properties of say.. an HTML table.

There is no difference whether you specify it on the element itself or within the CSS, though I now prefer using CSS so I can keep the HTML clear and concise.

Here's an example http://jsfiddle.net/N2RgB/1/

I've loaded the same image 4 times, both proportional and non-proportional using HTML attributes AND CSS properties.

There is absolutely NO difference.

Marko
A: 

The difference is in semantic and general approach rather than in how the rendering will work. :) I personally prefer to concentrate all things like width and heights within CSS classes and avoid to use both attributes like "width" and inline styles like "style='width:100px'", just because it results in nice logical separation - HTML markup tells what should be displayed and CSS rules - how exactly it will look.

dimarzionist
+2  A: 

A hot debate about the subject can be found here:

To sum it up: Width attribute for image tag versus CSS

The gain from declaring a width value and an height value (which may not be the original physical dimensions of the image) or from css declarations (like width: [valueX]; height: [valueY];) is that it helps speed up the rendering of the page. The browser knows how much space to allocate a particular area of the page: it will even draw image placeholders on a first draw, a first parsing+rendering of the page. When one does not define any width and height, then the browser has to download the image and then figure out its dimensions, space to allocate and then redraw the page.

This seems to be the most beneficial effect in my opinion.

Laheab
+1 for mentioning rendering
Álvaro G. Vicario