Back to subject
HTML5/HTML Images

Add a Picture

Practical lesson

Let's add an image and inspect every important part of the element.

- <img> displays an image and is a void element, so it does not need a closing </img> tag. - The src attribute tells the browser which image file to load. - The alt attribute gives a text alternative for people who cannot see the image and for cases where the image cannot load. - Write useful alt text when the image communicates information. - If an image is purely decorative, an empty alt="" can tell assistive technology to ignore it. - width and height can describe the intended image dimensions and help the browser reserve space. - Keep the image file path correct; a wrong src means the browser cannot find the file. - The image should be placed where it makes sense in the page's content. - For more advanced responsive images, HTML also provides <picture> and <source>.

The most important beginner rule is never to treat alt as an optional decoration. It is part of making the image understandable.

Try it yourself
Code

Starter example

Code
<img src="cat.jpg" alt="A small orange cat">
<img src="https://placehold.co/300x180" alt="A simple placeholder image">
Live preview