Sub topic 8
Adding images with img
This chapter covers the img element: how it works, how the browser finds the file, and what goes wrong. It assumes you can write a basic HTML document (html, head, body), open it in a browser, and read file and folder paths. It also assumes you know what an element and an attribute are.
What img is
The img element embeds an image in the page. It is a void element. That means it has no content and no closing tag. You write one tag and put everything in attributes.
<img src="images/red-fox.jpg" alt="A red fox standing in snow">Two attributes matter most:
srcis the location of the image file. Without it, nothing loads.altis a text alternative. It is shown or read aloud when the image is not visible. Later chapters cover writing goodalttext and sizing, so this chapter only uses them in simple form.
Always include alt, even when the page works without it. A missing alt attribute is a validation error. Screen readers may also read out the file name instead.
How the browser loads an image
The browser does not store the image inside your HTML. The HTML file only holds a reference. When the browser parses the img tag, it makes a separate request for the file named in src.
This has practical effects. A page with ten images makes at least eleven requests. Each image can succeed or fail on its own. The page text appears whether or not the images load.
Resolving the src path
The value of src is a URL. It follows the same rules as href on a link. You can use an absolute URL, a root-relative path, or a path relative to the current page.
<img src="https://example.com/images/logo.png" alt="Example Co. logo">
<img src="/images/logo.png" alt="Example Co. logo">
<img src="images/logo.png" alt="Example Co. logo">
<img src="../shared/logo.png" alt="Example Co. logo">Relative paths are resolved against the URL of the page that contains the tag. They are not resolved against the location of the image or of your editor's project root. Suppose the page is /blog/post.html:
| `src` value | Resolves to |
|---|---|
| `cover.jpg` | `/blog/cover.jpg` |
| `images/cover.jpg` | `/blog/images/cover.jpg` |
| `../images/cover.jpg` | `/images/cover.jpg` |
| `/images/cover.jpg` | `/images/cover.jpg` |
Use a root-relative path when the same image appears on pages in different folders. The path then stays valid if you move the page. Use a relative path when a page and its images travel together, for example in a folder you might copy to another site.
A project layout that works
A common layout keeps images in their own folder:
site/
index.html
about/
team.html
images/
red-fox.jpg
team-photo.jpgFrom index.html, the fox is images/red-fox.jpg. From about/team.html, the same file is ../images/red-fox.jpg, or /images/red-fox.jpg as a root-relative path. This only works if you serve the site from a server whose root is site/. If you open the files directly from disk, a leading / points to the root of your drive, and the image will not load.
Common reasons an image does not appear
Most failures come from the path, not from the tag.
- Wrong folder. You wrote
images/fox.jpgon a page inside a subfolder, so the browser looks in the wrong place. Compare the page's location with the image's location. - Wrong case. Many web servers treat
Fox.JPGandfox.jpgas different files. Your computer may not, so a page that works locally can fail after you deploy it. Pick one naming style, such as lowercase, and keep to it. - Wrong extension. The file is
fox.jpegbut the tag saysfox.jpg. - Spaces and special characters. A file named
red fox.jpgneedsred%20fox.jpgin the URL. Avoid the problem by using hyphens:red-fox.jpg. - Not an image. The URL returns an HTML error page or a file in an unsupported format. The browser cannot decode it and shows the broken-image state.
- Empty
src. Ansrc=""attribute can cause the browser to treat the page itself as the image address. Do not leave it empty.
Open the browser's developer tools and look at the Network tab. A failed image shows a status such as 404, and the request URL shows exactly what the browser tried to load.
Choosing a file format
The browser must support the format you use. These are the formats you will meet most often:
| Format | Best for | Notes |
|---|---|---|
| JPEG (`.jpg`) | Photographs | Small files, lossy compression, no transparency |
| PNG (`.png`) | Screenshots, graphics with sharp edges | Lossless, supports transparency |
| SVG (`.svg`) | Logos and icons | Scales to any size without blurring |
| WebP (`.webp`) | Photos and graphics | Smaller than JPEG or PNG, widely supported |
| AVIF (`.avif`) | Photos | Often the smallest files, newer |
| GIF (`.gif`) | Simple animation | Large files, limited colors |
The picture element, covered in a later chapter, lets you offer newer formats with a fallback.
How an image sits in the page
An img is an inline element. It sits in the line of text like a very large character. This has two visible effects:
- An image placed inside a paragraph flows with the surrounding text.
- A small gap can appear below an image that sits alone in a block, because the line reserves space for text descenders.
Without width and height, the page does not know the image size until the file arrives. The text then jumps down when the image loads. The sizing chapter explains how to prevent that. Here is the full tag with the attributes you will normally write:
<p>
Foxes are mostly active at dusk.
<img src="images/red-fox.jpg" alt="A red fox standing in snow" width="640" height="427" loading="lazy">
</p>Hotlinking and external images
You can point src at an image on another site. This is called hotlinking. It has three problems:
- The other site can delete or rename the file, and your image breaks.
- The other site can block requests from other pages, or serve a different image.
- Every visitor to your page makes a request to a server you do not control, which reveals their visit to that server.
Copy images you have the right to use into your own project, unless you have a specific reason to load them from elsewhere, such as a content delivery network that you set up.
Checklist for every img
- The
srcpath resolves correctly from the page that contains the tag. - The file exists on the server with the same spelling and case.
- The format is one the browser supports.
- The tag has an
altattribute. - The tag has
widthandheightwhen you know the image's dimensions.
Exercise
A page at /blog/2024/post.html contains <img src="images/cover.jpg" alt="Cover">. The image file is at /images/cover.jpg on the same server. The image does not load. Give two different src values that would fix it.
Show answer
The relative src is resolved from /blog/2024/, so the browser looks for /blog/2024/images/cover.jpg. Fix it with the root-relative path "/images/cover.jpg" or the relative path "../../images/cover.jpg" (up out of 2024, then out of blog, then into images).
Exercise
A page works when you open it from your laptop but shows a broken image after you upload it to a Linux web server. The tag is <img src="images/Fox.jpg" alt="A fox"> and the file on disk is named fox.jpg. What is the likely cause, and what is the fix?
Show answer
The server treats file names as case-sensitive, so Fox.jpg and fox.jpg are different files. Your laptop's file system ignored the difference. Change the src to "images/fox.jpg" to match the real file name, and adopt a lowercase naming rule for all files.