Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image width/height as an attribute or in CSS? [duplicate]

Tags:

html

css

What's the "correct" semantic way to specify image height and width? In CSS...

width:15px; 

or inline...

<img width="15" 

?

CSS seems like the right place to put visual information. On the other hand, few would argue that image "src" should not be specified as an attribute and the height/width seem as tied to the binary image data as the "src" is.

(Yes, I realize from a technical, end-user perspective this really doesn't matter.)

like image 871
Wayne Kao Avatar asked Mar 12 '09 19:03

Wayne Kao


People also ask

How do I make an image width and height the same CSS?

. container { width: 50%; } . container img { width: 100%; height: 400px; //this should be the same as the width value.. }

Is it a good idea to use the IMG tag's length and width attributes for every image or is it better to let the browser determine the size Why?

specifying width and height in the image tag is a good practice.. this way when the page loads there is space allocated for the image and the layout does not suffer any jerks even if the image takes a long time to load.

Which attribute specify the width and height of an image?

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.

Is width a tag or attribute?

Definition and UsageThe width attribute specifies the width of the element, in pixels. Note: For input elements, the width attribute is used only with <input type="image"> .


1 Answers

It should be defined inline. If you are using the img tag, that image should have semantic value to the content, which is why the alt attribute is required for validation.

If the image is to be part of the layout or template, you should use a tag other than the img tag and assign the image as a CSS background to the element. In this case, the image has no semantic meaning and therefore doesn't require the alt attribute. I'm fairly certain that most screen readers would not even know that a CSS image exists.

like image 116
VirtuosiMedia Avatar answered Sep 23 '22 06:09

VirtuosiMedia