Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline width for img or css width?

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;" />
like image 317
H Bellamy Avatar asked Nov 22 '11 16:11

H Bellamy


People also ask

Can we set width of inline elements in CSS?

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.

What is inline size 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 .

Is width an attribute of IMG tag?

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.


2 Answers

the latter is better (and more up2date).

<img src="foo.png" style="height:32px; width:32px;" />
like image 63
Jan Dragsbaek Avatar answered Oct 20 '22 02:10

Jan Dragsbaek


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.

like image 30
Koen027 Avatar answered Oct 20 '22 03:10

Koen027