The basic way to add a picture to an HTML page
To add a picture to an HTML page, use the <img> tag with a src attribute that points to your image file. The tag looks like this:
<img src="photo.jpg" alt="A description of the photo">
The src attribute tells the browser where to find the image file. The alt attribute describes what the picture shows — this text appears if the image fails to load, and it helps people using screen readers understand the content. Both attributes matter: src makes the picture appear, and alt makes your page work for everyone.
Place the <img> tag anywhere in your HTML where you want the picture to show. It can go inside a paragraph, a div, or anywhere else on the page. Unlike some HTML tags, <img> does not need a closing tag.
Key Takeaways
- The <img> tag requires a src attribute pointing to your image file and an alt attribute describing the image content.
- Image file paths can be relative (pointing to a file in your project folder) or absolute (a full web address starting with http).
- The alt attribute is not optional — it serves people using screen readers and provides fallback text if an image fails to load.
- You can control image size with width and height attributes, either in pixels or as percentages of the container.
- Images load faster when saved in the right format: JPEG for photos, PNG for graphics with transparency, WebP for modern browsers.
Understanding file paths for images
The src attribute needs to know where your image file lives. If your image is in the same folder as your HTML file, you can use just the filename:
<img src="photo.jpg" alt="My photo">
If your image is in a subfolder called "images", use a relative path:
<img src="images/photo.jpg" alt="My photo">
If your image is on another website, use the full web address:
<img src="https://example.com/images/photo.jpg" alt="My photo">
Relative paths (like "images/photo.jpg") are usually better for your own website because they keep working even if you move your entire project to a different server. Absolute paths (full web addresses) work anywhere but depend on that other website staying online.
Setting image width and height
By default, images display at their actual size. To make an image smaller or larger, add width and height attributes:
<img src="photo.jpg" alt="My photo" width="300" height="200">
The numbers are in pixels. If you set only width or only height, the browser automatically scales the other dimension to keep the image from looking stretched. Setting both can distort the image if the numbers do not match its original proportions.
You can also use percentages to make an image scale with its container:
<img src="photo.jpg" alt="My photo" width="100%">
This makes the image take up the full width of whatever element holds it, which is useful for responsive design — pages that look good on phones and desktop screens.
Writing effective alt text
The alt attribute should describe what the image shows in plain language. If the image is a photo of a dog running through grass, write:
<img src="dog.jpg" alt="Brown dog running through green grass">
Do not write "image of" or "picture of" — screen readers already announce that it is an image. Do not leave alt empty unless the image is purely decorative (like a colored line or background pattern). For decorative images, use
<img src="line.jpg" alt="">with empty quotes so screen readers skip it.
If the image contains text — like a poster or a screenshot — include that text in the alt description. If the image is a chart or graph, describe what data it shows rather than trying to list every number.
Choosing the right image format
Different image formats work better for different purposes. JPEG is best for photographs and images with many colors because it compresses well and keeps file sizes small. PNG works better for graphics, logos, and images that need a transparent background. WebP is a newer format that produces smaller files than both JPEG and PNG, but older browsers do not support it.
To use WebP while keeping older browsers working, use the <picture> tag with multiple sources:
<picture><source srcset="photo.webp" type="image/webp"><img src="photo.jpg" alt="My photo"></picture>
Modern browsers load the WebP version, and older browsers fall back to the JPEG. This approach gives you smaller file sizes for most visitors without breaking the page for anyone.
Making images clickable links
To turn an image into a link, wrap the <img> tag inside an <a> tag:
<a href="https://example.com"><img src="photo.jpg" alt="My photo"></a>
When someone clicks the image, they go to the URL in the href attribute. This is common for thumbnails that link to larger versions, or for logo images that link to the home page.
If you use an image as a link, make sure the alt text describes where the link goes or what happens when clicked. Instead of just "logo", write "link to home page" so people using screen readers understand the purpose.
Responsive images for different screen sizes
On a phone, a large image wastes bandwidth and slows the page down. The srcset attribute lets you provide different image sizes for different screen widths:
<img src="photo-small.jpg" srcset="photo-medium.jpg 768w, photo-large.jpg 1200w" alt="My photo">
The browser picks the right version based on the screen size. The numbers (768w, 1200w) tell the browser the width of each image in pixels. This approach saves data on phones and loads faster for everyone.
Creating multiple versions of the same image takes extra work, but it is worth doing for images that appear prominently on your page. Tools like ImageOptim or TinyPNG can help you resize and compress images quickly.
Frequently Asked Questions
What happens if the image file does not exist?
The browser shows a broken image icon (usually a small picture frame with an X) and displays the alt text. This is why alt text matters — it tells visitors what was supposed to be there. Check that your file path is spelled correctly and that the file actually exists in that location.
Can I use images from other websites?
Technically yes, but you should not without permission. Using someone else's image without consent is copyright infringement. If you want to use an image from another site, contact the owner and ask. Many sites offer images under Creative Commons licenses that allow reuse if you give credit.
How do I make an image not stretch or distort?
Set only the width or only the height, not both. The browser automatically scales the other dimension to match the image's original proportions. If you must set both, make sure the ratio of width to height matches the original image ratio.
Should I use PNG or JPEG for my images?
Use JPEG for photographs and images with many colors. Use PNG for graphics, logos, and images that need a transparent background. PNG files are usually larger, so JPEG is better when file size matters. If you want the smallest files and do not need to support very old browsers, use WebP.
What is the difference between width and max-width?
Width sets a fixed size — the image will always be that many pixels wide. Max-width sets a maximum size but lets the image shrink smaller on narrow screens. For responsive design, max-width is usually better because it prevents images from being too large on phones.