HTML5 - Can I use Width and Height in IMG?
Asked Answered
J

6

23

It is Legal to use this code:

<img src="...(here image)...." width="50px" height="50px" />

Or I need to use:

<img src="...(here image)..." style="width: 50px; height: 50px;" />
Judiejudith answered 22/7, 2011 at 1:46 Comment(0)
B
37

First use is recommended as hints for the browser's rendering, second one works.

In the first form you must not add 'px'.

http://www.w3.org/TR/html5/embedded-content-0.html#dimension-attributes

Borglum answered 22/7, 2011 at 1:49 Comment(0)
A
6

According to the HTML 5 specification:

The width and height attributes on img ... may be specified to give the dimensions of the visual content of the element

Source: http://www.w3.org/TR/2011/WD-html5-20110113/the-map-element.html#dimension-attributes

Also according to the HTML 5 specification, all elements may have style attributes. Source: http://www.w3.org/TR/html5/elements.html#the-style-attribute

Therefore, since both are allowed, you are free to choose whichever one suits your fancy.

Arteriotomy answered 22/7, 2011 at 2:1 Comment(1)
@erisco, so which is prefered?Pier
P
3

CSS applied to 'img' will overwrite basic html width & height attributes on image tags.

<style>
   img {
      width: 100%;
      height: auto;
   }
</style>

<img src="assets/img/logo.png" width="500" height="100">

The above code will result in an image that stretches across the entire width of its container, and its height will be relational to its width.

This approach is helpful if you're loading retina-appropriate graphics from the get-go.

Phyto answered 11/3, 2013 at 19:34 Comment(0)
I
2

As already mentioned :

  • CSS overwrite <img> attrinutes in case where both are present,
  • <img> attrinutes are only hints for the browser.

Another difference is the use of units :

  • in HTML5 the width and height attributes should be integers without units, that are interpreted as CSS pixels. In practice the brouwsers tolerate % (that was valid in HTML4), but if you put something like width="5cm" it will be interpreted as width="5" (i.e. 5px).
  • When you use CSS, any length units and non integer numbers are allowed, for example width: 5.5cm is ok. And you should use units (for example width: 5 is not a valid CSS width).

So, IMO, you should use CSS if possible.

Incisor answered 20/3, 2016 at 10:32 Comment(0)
R
1

The style= way is preferred...actually it would be even better if you moved that styling out into a css file or a style tag in your header.

Roselleroselyn answered 22/7, 2011 at 1:49 Comment(2)
I disagree, and would say that in most cases the width and height attributes are preferred (although it depends on your use). W and H attributes are easily overridden with css in an external file, where as an inline style can only be overridden with an !important. The attributes provide you with more flexibility.Willtrude
I would argue that putting inline styles in any shape or form is not preferred...period. Regardless of the affect of css on those stylings. Inline style almost always === bad practice. You kind of make the case for this argument in your explanation of how the W and H properties are overridden by css properties...that breaks the fundamental concept behind the cascading part of cascading style sheets. If you inject a inline styles that actually act like higher order on the cascade tree, you will inevitably be taking advantage of browser specific and brittle tactics.Roselleroselyn
J
0

Google Pagespeed Insights recommends the first one to reduce layout shifting. As others have mentioned, it can be overridden easily with CSS, e.g. for responsive pages.

Also, HTML 5 doesn't support self-closing tags (/ at end) so best format would be:

<img src="...(here image)...." width="50" height="50">

Jiujitsu answered 18/8, 2021 at 23:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.