I would like to know which of the following is better to use, or is generally better:
<img src = "foo.png" height = 32 width = 32 />
OR
<img src = "foo.png" style = "height: 32px; width: 32px;" />
The height and width of an inline element cannot be set in CSS. You cannot set the height and width of block-level elements in CSS.
The inline-size CSS property defines the horizontal or vertical size of an element's block, depending on its writing mode. It corresponds to either the width or the height property, depending on the value of writing-mode .
The width attribute specifies the width of an image, in pixels. Tip: Always specify both the height and width attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded.
the latter is better (and more up2date).
<img src="foo.png" style="height:32px; width:32px;" />
If you want to keep it clean, never put a style attribute on any HTML tag. Always use an external CSS file. Use an ID or use a class, as they are intended for this. If you suddenly need to change something that's present on 50 HTML elements, you'll curse the day you decided to do them all inline instead of giving them all a class and defining the values for that class in an external css file. Because editing one line in a CSS file is a lot easier than finding all 50 of them in the HTML file.
Also, always enclose your attributes in either single or double quotation marks. This is a best-practice.
Also, always close your tags. I seriously cannot believe someone would, in this day and age, casually say you don't need to close your tags in HTML.
You don't have to, it's true. But I really thought we had all moved past that point of laziness.
So, my recommendation is you use this: <img src="foo.png" height="32" width="32" />
Also, there is no need whatsoever to put spaces before or after you = sign.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With