w3-video.com logo

HTML5 Tutorial

Home HTML5 XAMPP .htaccess Firefox Notepad++

Share it



img width, img height attributes

The width, height attributes on the img element:


width, height attributes:

  • their value must be a valid positive integer (a positive whole number) expressed in pixels: e.g. width="12" height="12"
  • width is a specific attribute → can be applied ONLY on the next HTML5 elements: canvas, embed, iframe, img, input, object, video
  • height is a specific attribute → can be applied ONLY on the next HTML5 elements: canvas, embed, iframe, img, input, object, video
  • are optional attributes
  • image dimensions specified through width and height attributes is a useful information for user agents when rendering the page

Warning

The width and height attributes should be used to specify ⁄give the dimension of the image and not to set the dimension of it thus:

Examples

<imgattribute="value(s)" >Examples
5.height= pixels<img src="blue" alt="blue" width="50" height="70">
6.width= pixels<img src="blue" alt="blue" width="50" height="70">

Video demonstration img width, height attributes

HTML5 img width, height attributes Tutorial

min video details
00:02 width and height attributes on img, video tutorial
00:08 the width and height attributes should be used to give the dimensions of the image and NOT to set them
00:14 giving the dimensions of the image → the correct way
setting the dimensions of the image → the wrong way
00:21 the difference between "giving the dimensions" and "setting the dimensions" is presented next
00:44 "giving the dimensions"
00:44 the embedded image has the dimensions: 200 x 150; we can add its width=200 and its height=150 in page for the user agents to know it when they render the page
01:02 no change in page: this information is for user agents
01:08 now let's say we have a large image
01:44 its dimensions are: 4608 x 3456; so we can add its width=4608 and height=3456 in page for user agents to know when they render the page
02:09 about the large image:
02:06 if you want a smaller image, you need to resize it manually using a proper tool; once the image has been resized, you can embed it into your HTML page; this is the correct way
02:23 now we will present the wrong way
02:30 "setting the dimensions"
02:31 changing image's real dimensions → we force the browser to resize it
02:40 the browser resized the image with this cost:
1. the quality of the image: lower
2. page's loading speed: decreased
3. bandwidth consumption: increased
02:57 adding the right (= manually resized) image in page to highlight the difference
03:06 1. the quality of the image is affected: a lower quality when the browser has to resize it
03:24 the loading speed is affected : the speed decreases when the browser has to resize the image
note how long it takes for the 1st image to load
03:50 2. the loading speed is decreased: 2.94s versus 160ms

3. the bandwidth consumption is increased: 3.9MB versus 7.9KB
img intro img optional img occurrences img browser display img parents server-side image map (ismap) vs client-side image map (usemap) img syntaximg alt attributeimg ismap attributeimg usemap attributeimg src attributeimg accesskey attributeimg class attributeimg draggable attributeimg hidden attributeimg id attributeimg lang attributeimg style attributeimg tabindex attributeimg title attributeimg translate attribute