The basic way to add a picture to HTML
To add a picture to an HTML page, use the <img> tag with a src attribute that points to your image file. The simplest version looks like this:
<img src="photo.jpg" alt="A description of the photo">
The src tells the browser where to find the image file. The alt text describes what the picture shows — this appears if the image fails to load and helps people using screen readers understand the content. Both attributes matter: src makes the picture appear, alt makes your page accessible to everyone.
Place the <img> tag anywhere in your HTML where you want the picture to show. It can go inside a paragraph, in a heading, or on its own line. The browser will display the image at its original size unless you add width and height attributes.
Key Takeaways
- The <img> tag requires a src attribute pointing to your image file and an alt attribute describing the image.
- Image file paths can be relative (pointing to a file in your project folder) or absolute (a full web address).
- Add width and height attributes to control how large the picture appears on the page.
- Always include meaningful alt text so the page works for people using screen readers and when images fail to load.
- Common image formats for web use are JPG, PNG, and WebP, each with different strengths for file size and quality.
Using relative and absolute file paths
A relative path points to an image file stored in your project folder. If your image is in the same folder as your HTML file, just use the filename:
<img src="photo.jpg" alt="My vacation photo">
If the image is in a subfolder called "images", use a forward slash to show the path:
<img src="images/photo.jpg" alt="My vacation photo">
An absolute path is a complete web address. This works for images hosted anywhere on the internet:
<img src="https://example.com/images/photo.jpg" alt="My vacation photo">
Relative paths are better for images you control — they keep your project portable and load faster. Absolute paths work when you want to display someone else's image or when your image is hosted on a separate server.
Setting image width and height
By default, an image displays at its actual pixel size. To make it smaller or larger, add width and height attributes:
<img src="photo.jpg" alt="My photo" width="300" height="200">
The numbers represent 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 picture if the proportions don't match the original.
For responsive design — images that resize on different screen sizes — use CSS instead of fixed pixel values. A common approach is to set width to 100% and let the height adjust automatically, so the image shrinks on mobile phones and grows on desktop screens.
Choosing the right image format
JPG is best for photographs and complex images with many colors. It compresses well, keeping file size small, but loses some detail in the compression. A JPG file of a landscape photo might be 50 to 200 kilobytes.
PNG preserves every detail without quality loss and supports transparent backgrounds. PNG files are larger than JPG — the same landscape photo might be 500 kilobytes or more as PNG. Use PNG when you need perfect quality or transparency, like for logos or icons.
WebP is a newer format that combines JPG's small file size with PNG's quality. Not all older browsers support it, so use it only if your audience uses recent versions of Chrome, Firefox, Safari, or Edge. You can offer WebP to modern browsers and JPG to older ones using the <picture> tag.
For web pages, start with JPG for photos and PNG for graphics. Compress your images before uploading — tools like TinyPNG or your image editor's export settings can cut file size in half without visible quality loss.
Making images clickable with links
To turn an image into a clickable link, wrap the <img> tag inside an <a> tag:
<a href="https://example.com"><img src="photo.jpg" alt="Click to visit example.com"></a>
When someone clicks the image, they go to the URL in the href attribute. The alt text should describe where the link goes, not just what the picture shows. Instead of "A blue button", use "Click to visit our contact page".
Clickable images often have a border or color change when you hover over them. You can add this styling with CSS, but the default behavior is enough for most uses.
Organizing images in your project
As your project grows, keep images in a dedicated folder to stay organized. Create a folder called "images" in your project directory and store all pictures there. Then reference them with the relative path:
<img src="images/photo.jpg" alt="My photo">
You can create subfolders within images too — for example, "images/backgrounds" for background pictures and "images/icons" for small graphics. Use clear, descriptive filenames like "sunset-beach.jpg" instead of "photo1.jpg" so you remember what each file contains.
Keep your image files on your own server or hosting account. Linking to images on someone else's server (called hotlinking) can break if they move or delete the file, and it uses their bandwidth instead of yours.
Frequently Asked Questions
What if my image doesn't show up on the page?
Check that the file path in src is correct — relative paths are case-sensitive on most servers, so "Photo.jpg" is different from "photo.jpg". Make sure the file actually exists in that location. Open your browser's developer tools (press F12) and look at the Console tab for error messages that point to the problem.
Can I add a caption below an image?
Yes, use the <figure> and <figcaption> tags. Put your <img> inside <figure> and add <figcaption> below it. This tells screen readers and search engines that the caption belongs to that specific image, which is better than just putting text underneath.
How do I make an image responsive on mobile phones?
Set width to 100% and height to auto so the image shrinks to fit the screen. You can do this with CSS: img { width: 100%; height: auto; }. This way, the image takes up the full width on phones but stays at a reasonable size on desktop screens.
Is it okay to use images from Google Images?
No — most images on Google Images are copyrighted. Use only images you own, images with a Creative Commons license that allows your use, or images from free stock sites like Unsplash, Pexels, or Pixabay. Always check the license terms before using someone else's image.
What's the difference between <img> and <picture>?
The <img> tag displays a single image. The <picture> tag lets you offer different images for different screen sizes or browsers — for example, a small version for phones and a large version for desktops. Use <picture> only if you need this level of control; <img> with width: 100% handles most responsive needs.