HTML images: img, alt text, sizes and formats
Add images the right way: alt text, dimensions, file formats, figures and captions.
In this lesson you will
- Add images with src and alt
- Write alt text that helps everyone
- Pick the right image format and size
- Add captions with figure and figcaption
About 11 minutes
Adding a picture
Images use the <img> element. It is a void element, so there is no closing tag, and it needs two attributes to be useful: src (where the file is) and alt (a text description).
<img src="images/sunset.jpg" alt="Orange sunset over a calm sea">
The src works exactly like a link’s href. You can use a relative path to a file in your project or a full URL. Prefer your own copies, because a picture on someone else’s server may vanish or may be copyrighted.
The alt text is not optional
The alt attribute describes the image in words. It does three important jobs.
- Accessibility. Screen readers read it aloud to people who cannot see the picture.
- Resilience. If the file fails to load, the browser shows the alt text instead of an empty box.
- Search. Search engines cannot see pictures, so alt text tells them what the image is.
| Image | Weak alt | Good alt |
|---|---|---|
| A photo of a chef chopping onions | alt="image1" |
alt="Chef chopping onions on a wooden board" |
| A company logo | alt="logo" |
alt="Skills in Minutes" |
| A purely decorative swirl | alt="swirl" |
alt="" (empty, so screen readers skip it) |
Imagine reading the page aloud to a friend over the phone. What would you say in place of the picture? That sentence is your alt text. Do not begin with “Picture of”, because screen readers already announce that it is an image.
Size and shape
<img src="images/team.jpg" alt="The team at their desks" width="800" height="533">
Always include width and height as plain numbers (pixels). They do not force the size on screen if CSS overrides them, but they let the browser reserve the right amount of space before the picture downloads. Without them, text jumps down as each image arrives, a flaw called layout shift that annoys visitors and hurts search rankings.
Choosing the right file type
| Format | Best for | Notes |
|---|---|---|
| JPG | Photographs | Small files, no transparency |
| PNG | Logos, screenshots, sharp text | Supports transparency, larger for photos |
| WebP | Almost everything | Smaller than JPG and PNG, supported in all modern browsers |
| SVG | Icons, logos, simple graphics | Vector, stays sharp at any size, tiny files |
| GIF | Tiny animations | Large and low quality, prefer video or WebP |
Speed matters: a phone on mobile data may take many seconds to load a 5 MB photo. Resize photos to the size they will actually appear, compress them, and aim for under 200 KB for most images on a page.
Figures and captions
When a picture has a caption, wrap both in a <figure> and add a <figcaption>. The caption is then linked to the image in meaning, not only in position.
<figure>
<img src="images/bridge.jpg" alt="Suspension bridge at dawn" width="900" height="600">
<figcaption>The bridge at dawn, shot on a phone.</figcaption>
</figure>
Add an image using the web address below. Give it meaningful alt text, a width of 300, and wrap it in a figure with a caption.
<img src="https://picsum.photos/id/237/300/200">
Add alt="A black puppy sitting". Wrap it in <figure> and add <figcaption>My caption</figcaption> after the image.
<figure>
<img src="https://picsum.photos/id/237/300/200" alt="A black puppy sitting" width="300" height="200">
<figcaption>A black puppy.</figcaption>
</figure>
The sample image comes from a free placeholder service. If you are offline or it is slow, the box will show your alt text, which is exactly why alt text matters.
Responsive images in one line
You will meet srcset and picture later in the course for serving different sizes to different screens. For now one line of CSS makes any image shrink to fit small screens instead of overflowing: img { max-width: 100%; height: auto; }. Almost every professional stylesheet starts with it.
Copyright and where to find images
An image you find through a search engine belongs to someone. Using it without permission can lead to takedowns or fines. Safer sources are your own photos, free libraries such as Unsplash, Pexels and Pixabay (check each licence), or images you create yourself. When a licence asks for credit, put it in the caption.
Never link directly to images on other sites (called hotlinking). The owner may block you, swap the picture, or bear the bandwidth cost. Download it, if the licence allows, and host it yourself.
Common mistakes
- Leaving out the alt attribute entirely.
- Writing the wrong path, which shows a broken-image icon. Check capital letters and folders.
- Uploading a 6000-pixel photo straight from a camera.
- Using images to hold text. Search engines and screen readers cannot read it, and it does not resize.
Practice exercises
- Create an
imagesfolder in your project and put two small pictures in it. - Display both with good alt text and explicit width and height.
- Turn one into a link to another page, as you learned in lesson 5.
- Rename a file on purpose to see the broken-image icon and read how your alt text appears.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Which attribute holds the image file location?
hrefsrcpathfile
The src attribute tells the browser where to find the picture.
What is alt text for?
- Making the image bigger
- Describing the image for screen readers and when it fails to load
- Adding a border
- Speeding up downloads
Alt text serves accessibility, search engines and failed loads.
Which format is best for a photograph on a modern site?
- GIF
- WebP or JPG
- SVG
- BMP
Photographs compress best as WebP or JPG. SVG suits flat graphics.
Why add width and height attributes?
- To stop the picture loading
- So the browser reserves space and avoids layout jumps
- To change the file type
- To add a caption
Reserved space prevents text from jumping as images load.
A decorative divider adds no information. The best alt is:
alt="line"alt="decorative image of a line"alt=""- Leave out alt completely
An empty alt tells screen readers to skip it. Missing alt may make them read the file name.
Summary
- Every image needs src and a useful alt.
- Set width and height to prevent layout shift.
- WebP and JPG for photos, PNG and SVG for graphics.
- Use only images you have the right to use.
Questions
Can I use any image from Google?
No. Most images are copyrighted. Use your own photos or sources with a clear free licence, and follow any credit requirement.
How big should my images be?
No wider than they will display, which is usually under 1600 pixels for full-width pictures, and compressed to a few hundred kilobytes at most.
What if I forget the alt attribute?
The page still works, but screen reader users may hear the file name, and your page loses accessibility and search value. Always include it.
Should I put text inside images?
Avoid it. Real text is searchable, resizable, translatable and readable by assistive technology.
