39

The HTML <img> element can have the width / height attribute, and it can also have the CSS width / height properties:

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

What's the difference between the HTML attribute and CSS property and should they have the same effect?

Meraj al Maksud
  • 1,528
  • 2
  • 22
  • 36
Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230

8 Answers8

17

A hot debate about the subject can be found here: Width attribute for image tag versus CSS

To sum it up:

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.

Meraj al Maksud
  • 1,528
  • 2
  • 22
  • 36
Ibrahim AshShohail
  • 2,072
  • 14
  • 19
  • 4
    Warning, the linked blog post is from 2005 and CSS standards may be different now than then. – wberry Jul 27 '16 at 20:48
  • 1
    The above comment also suggests there's no difference! Declaring it as an attribute or in the css helps speed up the rendering of the page. – Shiraz Jan 12 '21 at 23:28
11

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
  • 71,361
  • 28
  • 124
  • 158
  • I heard CSS is being turned standard, and so the attributes might be deprecated sometime. – heltonbiker Dec 02 '11 at 16:02
  • 4
    And according to my experiments, if the attributes contradict the style, the style wins. (Firefox, Chrome) – wberry Jul 27 '16 at 21:05
  • 3
    Well, there is absolutely a difference. Browsers always download HTML first then style sheets, which means you can reserve some width with attribute then apply the correct style later, when you want to pursue user experience. – KinoP Oct 18 '16 at 07:51
  • One of the difference I've spotted is when you are using lazy loading: https://web.dev/browser-level-image-lazy-loading/#images-should-include-dimension-attributes . – microHoffman Apr 19 '23 at 15:25
4

When width and height are used in the <img> tag as :

<img src="xxx.img" width="25" height="25">

then width and height attributes are called presentational attributes.

And when width and height are used in the <img> tag as :

<img style="width: 16px; height: 16px">

then width and height are said to be defined using inline CSS styles

So we can define width and height for an <img> tag as presentational attributes or using inline CSS styles, in both the ways we will get same result.

But the only difference between defining width and height for an <img> as presentational attributes and by using inline CSS styles is, defining width and height as presentational attributes is the weakest way to define width and height for <img> tag whereas defining width and height using inline CSS style is the strongest way of defining width and height for the <img> tag.

So if we define width and height in both ways (that is as presentational attributes and by using inline CSS styles) then the definition for width and height using inline CSS style will override the definition of width and height as presentational attributes

So basically presentational attributes on <img> tag is a good idea but presentational attributes are also weak and are usually overridden by inline CSS styles

For reference check this

Prateek Gupta
  • 2,422
  • 2
  • 16
  • 30
2

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
  • 1,584
  • 1
  • 19
  • 29
0

I can see a difference... Check the following code snippet out stolen from: http://jqueryui.com/resources/demos/button/icons.html

    <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Button - Icons</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<!-- redefine source of background-image -->
<style>
    .ui-icon { width: 64px; height: 64px; }
</style>

<script type="text/javascript" >
$(function(){
    $("img").button({
        icons: {
            primary: "ui-icon-locked"
        },
        text: false
    });
});
</script>
</head>
<body>

<img width="32px" height="32px"> <!-- size of img tag -->

</body>
</html>

This is the result:

enter image description here

Taylan Aydinli
  • 4,333
  • 15
  • 39
  • 33
user2928048
  • 3,940
  • 2
  • 12
  • 12
0

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
  • 6,947
  • 7
  • 46
  • 76
0

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
  • 18,517
  • 4
  • 22
  • 23
-1

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
  • 215
  • 1
  • 2
  • 8