Images: choose a source, write alt text and set dimensions
A picture can explain a page, show an object or simply decorate a section. Learn how to display your own image file and describe its purpose so the page still makes sense when someone cannot see it.
Putting an img element in HTML does not copy the picture into the HTML file. The src attribute gives the image address, and the browser requests or reads that resource separately.
Use the original practice images supplied in the example ZIP. Place the images folder beside the HTML files. The desk drawing is images/study-desk.svg. SVG is a vector image format: shapes are described so that the image can scale clearly. You do not need to write SVG for this exercise.
The src path follows the same folder rules as href. Keep the filename, letter case and extension exact. An HTML page renamed to .png does not become an image.
Read the img syntax
HTML fragment — part of a document
<imgsrc="images/study-desk.svg"alt="A notebook and pencil on a study desk"width="360"height="220">
Part
What it supplies
img
The image element
src
The image resource address
alt
A text replacement appropriate to the image's purpose
width
Intended width in CSS pixels, written as an integer
height
Intended height in CSS pixels, written as an integer
Img is a void element. It has no content between opening and closing tags and does not use a closing img tag. Put any visible caption in a separate suitable element, covered in the next chapter.
Complete program: study-image.html
study-image.html
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Study Desk Image Practice</title></head><body><h1>Prepare a quiet study place</h1><p>The drawing shows the two items for today's writing task.</p><imgsrc="images/study-desk.svg"alt="A notebook and pencil on a study desk"width="360"height="220"></body></html>
Browser previewDocument title: Study Desk Image Practice
This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
Expected result: the supplied desk drawing appears below the paragraph. The width and height describe its 360 by 220 display size in CSS pixels before any later styling changes it. One CSS pixel does not necessarily equal one physical screen pixel on every device.
Write alt for the picture's job
Alt text is replacement text, not a place to add every keyword or the filename. Ask, “If the picture were unavailable, what would the reader need instead?”
For an informative picture, write a short, useful description. For a chart, the important information may need a nearby explanation or data table as well as a brief alt. A long technical chart cannot always be explained in a few words.
Avoid starting every alt with Image of. Reading tools can already identify an image. Do not depend on alt appearing as a hover tooltip; that is not its purpose. A title attribute is not a replacement for alt or for essential visible instructions.
Image purpose
Appropriate approach
Shows items needed for a task
Name those items briefly
Pure decoration, with no information to add
Use alt=""
Acts as the only content of a link
Describe the link's purpose or destination
Contains essential words
Provide the words in equivalent text; prefer real HTML text where possible
Contains complex data
Provide an accessible fuller explanation nearby
The same picture can need different alt text in different contexts. A desk photo in an equipment guide may identify equipment; the same photo beside a heading might be decorative if it adds no useful information.
Choose replacement text by purpose
1Does the image add information? Describe it
2Is it only decoration? Use empty alt
3Is it the only link content? Name the destination
4Is it complex? Add a fuller text explanation
Do not use a filename as a substitute for thinking about the image purpose.
Empty alt is different from missing alt
An empty value, alt="", deliberately marks a decorative image as having no replacement information. Omitting alt gives no such clear instruction; tools may announce a filename or URL instead. For our authored beginner pages, always make a deliberate choice.
The small practice mark below decorates a sentence whose words already communicate the full message. It therefore uses empty alt.
Complete program: decorative-image.html
decorative-image.html
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Decorative Practice Mark</title></head><body><h1>Your practice reminder</h1><p><imgsrc="images/practice-mark.svg"alt=""width="40"height="40"> Keep one checked example in your notebook.</p><p>The words carry the reminder; the small mark only decorates it.</p></body></html>
Browser previewDocument title: Decorative Practice Mark
This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
Expected result: a small mark appears beside a complete text reminder. Removing that decorative picture would not remove the instruction. Do not use empty alt for a picture that supplies information absent from the surrounding words.
Dimensions, shape and file size
Write width="360", not width="360px" in these HTML attributes. Use the original aspect ratio, the relationship between width and height. A 360 by 220 image forced to 360 by 100 can look flattened.
Supplying both dimensions lets the browser reserve appropriately proportioned space before the image arrives. This helps reduce unexpected layout movement. CSS can later make an image fit a narrow screen while keeping its proportions.
Smaller display dimensions do not necessarily mean a smaller download. A very large photo displayed as a tiny thumbnail may still transfer its full file. Resize or compress the actual resource appropriately. Later lessons cover responsive image selection with srcset and picture.
Loading and decoding
Loading="lazy" allows the browser to defer fetching an image until it is near the visible area. Use it for suitable images farther down a page, not automatically for the main picture readers need immediately. The default is eager loading.
Decoding="async" is a hint about decoding and presentation, not a request for lazy fetching. Fetchpriority="high" is a relative priority hint and should be reserved for a genuinely important image when warranted. It does not replace loading or guarantee an exact request order. These are optional hints; correct src, suitable alt and a sensible resource are the first priorities.
HTML fragment — part of a document
<imgsrc="images/study-desk.svg"alt="A notebook and pencil on a study desk"width="360"height="220"loading="lazy"decoding="async">
Problem and solution: the picture is broken
Problem: the browser shows a broken-image sign or replacement text.
Solution: first open the image file directly to check that it exists and is a real supported image. Then compare the HTML file's location with src. Check case, extension and folder spelling. Reload after saving. Do not remove alt to hide a broken image; fix the resource and keep appropriate replacement text.
Common mistakes and useful tips
Using a path from your own Downloads folder in a public page.
Writing a filename such as IMG_0003.png as the alt text.
Omitting alt for decoration instead of deliberately using an empty value.
Making important instructions available only inside a picture.
Writing CSS units inside the numeric width and height attributes.
Stretching the image by choosing unrelated dimensions.
Lazy-loading every image, including the first important image.
Copying pictures from websites without permission. Use your own or properly licensed assets.
Practice questions
What does src contain?
Why does img not need a closing tag?
How does alt="" differ from leaving alt out?
Are width="360px" and width="360" both correct HTML attribute spellings here?
Does shrinking the displayed width always reduce the downloaded file size?
Build a page with one informative image and one decorative mark. Explain each alt choice and then temporarily change a source filename to observe a missing image.
Check your practice answers
The image resource address.
Img is a void element and has no child content.
Empty alt deliberately supplies no replacement information for decoration. Missing alt does not clearly convey that intention.
Use the integer spelling width="360". Units belong in appropriate CSS values, not this numeric attribute.
No. The browser may download the same original file regardless of its displayed size.
Use the supplied image files, meaningful replacement text for the informative picture and empty alt for the decoration. Restore the correct src after checking the failure case.
Interview questions
What makes good alt text?
It replaces the image's useful purpose in its current context. It is concise when possible, avoids irrelevant details and does not merely repeat a filename.
Why provide image width and height?
They describe intended dimensions and help the browser reserve space in the correct proportions before loading. They do not themselves compress the source file.
How do loading and decoding differ?
Loading controls whether fetching may be deferred. Decoding is a hint about how image decoding relates to presentation. Neither fixes an incorrect source or unsuitable replacement text.
When should an image use empty alt?
When it is purely decorative or adds no information beyond equivalent nearby content. Do not hide a meaningful image or an otherwise unnamed functional control with empty alt.
Quick revision
Img points to a separate source resource and has no closing tag.
Choose alt by the picture's purpose.
Empty alt means intentional decoration; missing alt is different.
Keep dimensions proportional and resource sizes sensible.
Use loading hints only when their behaviour suits the page.