Mozilla’s original article.
The humble (and empty) <img> element needs at minimum a src=”attribute.jpg” to be useful. The src can be either a relative or absolute url just like the href=”http://www.attribute.com” in the <a> or anchor tag.
If your image is in the same directory as the web page it could be this simple:
<img src="bird.svg">
If it’s in an images subdirectory:
<img src="images/bird.svg">
Google recommends that you use an images folder as it reads the folder and filenames for Search Engine Optimization. This is also why you should give your images a descriptive filename.
You could embed the image using its absolute Uniform Resource Locator (URL):
<img src="https://www.example.com/images/bird.svg">
…but that would be a pointless waste of typing and processing. The browser would have to re-check the Domain Name System (DNS) server to look up the Internet Protocol (IP) address again. You’ll almost always keep your images on the same server as your HyperText Markup Language (HTML).
Warning: do not use someone else’s image on your site unless you have written permission, or ample proof the image is in the public domain.
“Hotlinking” steals someone else’s bandwidth. You also don’t have control over what the image could be replaced with.
Note: elements like <img> and <video> are sometimes referred to as replaced elements. This is because the element’s content and size are defined by an external source (like an image or video file) and not by the elements themselves.
Alternative Text
The alt=”attribute” is a text description of the image. This is for use with a screen reader or if bandwidth is to slow to load the image. For example:
<img src="images/bird.svg"
alt="the underside of a white bird with its wings spread open">
The easiest way to check your alt text is to misspell your filename. Then the browser will display the alt text instead if the image.
Other reasons to use alt text are:
- browser can’t display the image type;
- text for search engines;
- user turned of images to save bandwidth (common with mobile or in 3rd world countries).
If your image is for decoration only, include empty alt text so the screen reader doesn’t read out the image Uniform Resource Locator (URL):
<img src="images/background.svg" alt="">
Empty alt text can also be used if the content describes the image enough. This is to reduce redundancy.
If you turn your image into a link use either the link text or the alt text. Not both.
You shouldn’t put text inside an image. But if you can’t avoid doing this, supply the text an an alt attribute.
The main thing is to keep all the content accessible to as many people as possible.
Width and Height
You can use the width and height attributes to set the size of your image.
If you are on Android you could use Photo Resizer HD and save the image as 800×600(svga):
<img src="images/bird-800x600.jpg" alt="the underside of a white bird with its wings spread open"
width="800"
height="600"
This is a good idea as it will leave space for the image while the browser is loading.
However, don’t alter the size of the image using HyperText Markup Language (HTML). This can cause both pixelation and distortion.
Image Titles
It is possible (though not recommended) like a link, to add a title=”attribute” to your image:
<img src="images/bird-800x600.jpg" alt="the underside of a white bird with its wings spread open"
width="800"
height="600"
title="a bird in flight"
Because the title element only works for users with a mouse it is not suggested. Better to put the information in the article itself or in the alt text.
My hotlinked image code:
<img src="https://raw.githubusercontent.com/mdn/learning-area/master/html/multimedia-and-embedding/images-in-html/dinosaur_small.jpg" alt="the bones of a rather large bird" title="this title deserves to be accessible"
width="200"
height="171">
Figures and Captions
Here is one way to make a caption:
<img src="images/bird.svg" alt="the underside of a white bird in flight"
height="400"
width="200">
<p>Here is a dove ascending</p>
…this is OK. But what if there were 50 images and captions? How would a screen reader be able to link them properly?
A better solution is to use the HyperText Markup Language 5 (HTML5) <figure> and <figcaption>:
<figure>
<img src="images/bird.svg"
alt="the underside of a white bird with its wings open"
width="400"
height="800">
<figcaption>A dove ascends from above</figcaption>
</figure>
Captions and alt=”text” should say different things. Captions are visible to people who can see the image. Alt text is in the images’ stead.
A figure doesn’t have to be an image. It can be multiple images, a video a code snippet or a table.
My example code:
<figure>
<img src="images/bird.svg"
alt="a white bird flying"
height="171"
width="200">
<figcaption>
The dove ascends and alights
</figcaption>
</figure>
Cascading Style Sheets (CSS) Background Images
If an image has no meaning at all and is purely for decoration, then you can use CSS to render it. For example behind every paragraph:
p {
background-image: url("images/bird.svg");
}
This is easier to place and style in CSS than HTML. However, if you want your image to have meaning, put it in using HTML and give it alt=”text” & a go<figcaption>.
My Assessment Answer Post On Mozilla Discourse: