To insert an image in HTML, use the <img> tag with a src attribute that points to the image file and an alt attribute that describes it, for example <img src="images/logo.png" alt="Company logo">. Add width and height so the page does not jump while the image loads, and use CSS to control alignment and layout.
This guide covers file paths, sizing, alt text, alignment, turning an image into a link, and responsive images that work well on phones.
The img tag and its attributes
The <img> tag is an empty element, so it has no closing tag. A complete, well-formed image looks like this:
<img src="images/shop-front.jpg" alt="Our shop on Main Street" width="1200" height="800">
| Attribute | What it does | Needed? |
|---|---|---|
src | The path or URL of the image file | Yes |
alt | Text read by screen readers and shown if the image fails to load | Yes |
width, height | The image's dimensions in pixels, used to reserve space | Strongly recommended |
loading="lazy" | Delays loading images further down the page until they are needed | Optional |
srcset, sizes | Offer several file sizes so the browser can pick the best one | Optional |
Getting the file path and alt text right
Most broken images come from a wrong path in src. Paths are worked out from the location of the HTML page, not from the site root, unless you start them with a slash.
Relative paths
Relative paths are best for images in your own site, because they keep working if you move the site to a new domain or open it from a folder on your computer. Suppose your page is index.html and your images are in a folder called images next to it:
- Same folder as the page:
src="photo.jpg" - In a subfolder:
src="images/photo.jpg" - Up one folder, then into images (for a page inside a subfolder such as
blog/):src="../images/photo.jpg"
Root-relative and absolute paths
A path starting with a slash, such as src="/images/photo.jpg", starts from the top of the site. It works once the site is on a web server but often fails when you open a page straight from your hard drive. A full URL such as https://example.com/images/photo.jpg is only needed for images hosted on another site.
Watch the details
- Most web servers treat
Photo.JPGandphoto.jpgas different files. Use lowercase names. - Avoid spaces in file names. Use hyphens instead, such as
team-photo.jpg. - Use forward slashes, never Windows-style backslashes.
- Never point to a file on your own computer, such as a path starting with
C:. It will work for you and fail for everyone else.
If an image works on your computer but not online, see images not showing on a website.
Writing alt text
The alt attribute describes the image for people who cannot see it. Write what the image shows that matters in context, in a short sentence:
- Good:
alt="Chef pouring sauce over a plate of pasta" - Not useful:
alt="image1"oralt="photo"
If an image is purely decorative and adds nothing to the content, use an empty value, alt="", so screen readers skip it. Do not leave the attribute out entirely. Our guide on how to write alt text has more examples.
Setting the size
Set width and height to the image's real pixel dimensions. The browser uses them to work out the image's shape and keep space for it, so text does not jump down when the image arrives.
To make an image display smaller or scale with the screen, use CSS rather than changing those numbers. This rule in your stylesheet stops images overflowing their container on narrow screens, while keeping the correct proportions:
img { max-width: 100%; height: auto; }
Displaying a huge photo at a small size still makes visitors download the huge file. Resize images in an image editor to roughly the largest size they will be shown, and compress them, before adding them to your site. For which format to use, see the best image format for websites.
Aligning images with CSS
The old align attribute is obsolete in HTML5. Use CSS instead.
Centering an image
An image is an inline element, so first make it a block, then use automatic side margins:
.center { display: block; margin-left: auto; margin-right: auto; }
Then add class="center" to the image.
Wrapping text around an image
To place an image on the left or right with text flowing beside it, use float with a margin so the text does not touch it:
.left { float: left; margin: 0 16px 8px 0; }.right { float: right; margin: 0 0 8px 16px; }
On phones, floated images can squeeze the text into a thin column. A media query that removes the float on small screens fixes that.
Adding a caption
For an image with a caption, wrap both in a <figure>: <figure><img src="map.png" alt="Map of the route"><figcaption>The 5 km route</figcaption></figure>
Turning an image into a link
Wrap the image in an <a> tag:
<a href="gallery.html"><img src="images/thumb.jpg" alt="View the full gallery" width="300" height="200"></a>
When an image is the only thing inside a link, its alt text becomes the link text. Describe where the link goes, not just what the picture shows. A logo that links to the home page, for example, could use alt="Acme home page".
Responsive images for faster pages
Phones do not need the same large file as a wide desktop screen. With srcset you list several versions of the image and their widths, and sizes tells the browser how wide the image will be shown. The browser then downloads the most suitable file:
<img src="hero-800.jpg" srcset="hero-480.jpg 480w, hero-800.jpg 800w, hero-1600.jpg 1600w" sizes="(max-width: 600px) 100vw, 800px" alt="Hikers on a ridge at sunrise" width="800" height="450">
Also consider loading="lazy" for images well below the top of the page. Leave it off your main image at the top, so it appears as quickly as possible.
The easier way: drag an image onto the page
Getting paths, sizes and attributes right by hand takes care. Apollo Editor, a Windows desktop HTML editor, handles most of it for you.
- Drag and drop an image onto the page, or use the Insert Image button, and preview it before inserting.
- The Image Properties dialog sets alt text, size, alignment, CSS styling, image links and responsive image attributes.
- Relative paths are managed for you, and Site Explorer keeps links up to date (after asking) when you move or rename files.
- Works with JPEG, PNG, GIF, WebP and SVG images, plus AVIF where supported.
Apollo Editor does not include an image editor, so resize and compress photos in your usual tool first. Read more about the features.
Frequently asked questions
Why should I set width and height on images?
They let the browser reserve space before the image loads, so the page does not jump around as it appears.
How do I center an image in HTML?
Make the image display: block and give it margin-left: auto and margin-right: auto in CSS, or center its container's content.
Should image paths be relative or absolute?
Relative paths such as images/logo.png are best inside your own site, because they keep working when the site moves to a new address.
