Skip to content

How to Add an Image in HTML

Short answer: use the <img> tag. src is where the image file is, and alt says what it shows: <img src="images/dog.jpg" alt="A brown dog catching a ball">. Add width and height so the page doesn’t jump while it loads. Every example below is live: edit the code and the preview changes.

The quick answer: the img tag

The <img> tag puts a picture on the page. It’s an empty (void) element, so there’s no </img>. You give it everything through attributes: src for the file, alt for the words that stand in for the picture, and width and height in pixels.

An img tag with src, alt, width and height

The examples run in a locked box: scripts are off, nothing is sent anywhere, and your edits go away when you reload. They use full image URLs, because a short path like images/dog.jpg has nothing to point to inside the box. To keep an example, copy it into the HTML Editor.

The src path: where is the file?

src is a path from your HTML file to the image. The browser reads it like directions. Say your page is index.html and your project looks like this: index.html, logo.png, a folder images/ with dog.jpg in it, and a folder blog/ with post.html.

You writeThe browser looks for
src="logo.png" A file in the same folder as the HTML file.
src="images/dog.jpg" The images folder next to the HTML file, then dog.jpg inside it.
src="../images/dog.jpg" One folder up first. From blog/post.html, ../ goes back to the main folder, then into images.
src="/images/dog.jpg" The images folder at the root of your website, wherever the page is. Handy on a real site, but it doesn’t work when you open the HTML file straight from your computer.
src="https://example.com/dog.jpg" A full URL: the image on another website (or your own), exactly where it says.

Here’s the same picture twice. The first src is a full URL, so it loads. The second points to an images folder that this example box doesn’t have, so you see what a broken image looks like: an icon and the alt text.

A path that works and one that doesn’t

Image not showing? Check these

When an image doesn’t show, the path is almost always the problem. A quick test: open the image address on its own in a new tab (right-click the broken image, or copy the path from your code). If it doesn’t load there, it won’t load on your page either.

  • The folder. images/dog.jpg only works if the images folder sits next to your HTML file. If your page is in a subfolder, you may need ../ first.
  • Upper and lower case. Dog.JPG and dog.jpg are the same file on Windows and on most Macs, but most web servers see two different names. That’s why an image works on your computer and breaks once it’s online. Keep file names lowercase.
  • The extension. .jpg and .jpeg are different names, and so are .png and .PNG. Copy the file name exactly. Also check for hidden doubled ones like photo.jpg.jpg, which Windows can hide when it hides extensions.
  • Spaces and odd characters. my photo.jpg usually works, but it’s easy to get wrong. Rename it to my-photo.jpg and save yourself the trouble.
  • A path from your own computer. src="C:\Users\you\Desktop\dog.jpg" or a file:/// address only works on your computer. Put the image in your project folder and use a path like images/dog.jpg.
  • http on an https page. Most browsers try the https address instead, and if that site has no https, the image doesn’t show. Use https URLs.
  • Images from other sites. Some sites block their images on other people’s pages, and the file can be moved or deleted any time. Save your own copy (if you’re allowed to use it) and link to that.

Alt text: good, bad and empty

The alt text is what screen readers read out, what shows when the image can’t load, and what search engines use to understand the picture. Write it like you’re describing the image to a friend on the phone: what’s in it, in one short sentence.

Alt textWhy
alt="Snowy peak above a lake at sunset" Good: says what’s in the picture.
alt="Add to cart" (on an image button) Good: when an image does something, say what it does.
alt="image" or alt="IMG_2041.jpg" Bad: tells the listener nothing.
alt="Picture of a mountain" Not needed: screen readers already say it’s an image. Just say alt="A mountain".
alt="" Right for decoration only, like an icon next to text that already says the same thing.
No alt at all Avoid: some screen readers read the file name out instead.

In this example the photo has a real description. The star is decoration (the words “Staff pick” say it all), so it gets alt="" and screen readers skip it.

A described photo and a decorative icon

Image size: width and height

The width and height attributes

width="160" height="100" sets the size in CSS pixels. Write plain numbers, no px and no %. If you change both, keep the same shape as the file, or the picture gets squashed. The sample image is 800 by 500, so 160 by 100 or 240 by 150 keeps its shape.

Even when CSS sets the real size, keep the attributes. The browser uses them to reserve the right amount of space before the image arrives, so the text below doesn’t jump down when it loads (this jump is called layout shift).

Resize an image with CSS

CSS wins over the attributes. Set one side and give the other auto, like width: 110px; height: auto;, and the browser works out the other side from the picture’s shape. The third image shows what happens when the numbers don’t match the shape.

Attributes, CSS, and a squashed image

Make images fit small screens

A big image is wider than a phone screen. These two lines make every image shrink to fit its box, and never grow bigger than its real size. In the example the “Without” image is cut off by its box; on a real page it would stick out and make the whole page scroll sideways. Many sites put them in their main stylesheet as img { ... }.

max-width: 100% and height: auto

This keeps one file for every screen. To send a smaller file to phones and a bigger one to large screens, use srcset and sizes. The Responsive Image Generator builds that code for you.

Fill a box without stretching: object-fit

For thumbnails or avatars you often want a fixed square, whatever the photo’s shape. Set both sizes and add object-fit: cover fills the box and crops the edges, contain shows the whole picture with empty space around it. object-position picks which part stays when it crops.

Stretched, cover and contain

Center an image

An image is an inline element, which means it sits in a line of text like a big letter. That’s why margin: 0 auto alone does nothing to it. Three ways that work:

  1. Make it a block with display: block, then margin: 0 auto.
  2. Center it like text: text-align: center on the parent element.
  3. Flexbox: display: flex; justify-content: center; on the parent.
Three ways to center an image

The old <center> tag is obsolete, and align="center" on an image never centered it anyway, so leave them out. For centering vertically too, or centering a whole div, see the center text and divs tutorial.

💡 Seeing a small gap under an image inside a box? That’s the space for letters like g and y, because the image sits on the text line. display: block or vertical-align: top on the image removes it.

Put the <img> inside an <a> tag. When the image is the only thing in the link, its alt text becomes the link text, so describe where the link goes (“See all the mountain photos”), not just the picture. A logo that links home usually gets the site name, like alt="Acme home".

A clickable image

In this example box the link has nowhere to go. On your own site it opens mountain-photos.html. Want different parts of one image to link to different pages? That’s an image map: draw the areas in the Image Map Generator.

An image with a caption

Wrap the image and its caption in <figure>, and put the caption in <figcaption> (as the first or the last thing inside). Browsers and screen readers then know the text belongs to that picture. The caption doesn’t replace the alt text: alt describes the picture, the caption adds something, like where it was taken or who made it.

figure and figcaption

<figure> has a default margin (1em above and below, 40px on each side), which is why the example sets margin: 0.

Background image with CSS

To put an image behind text, use CSS, not an <img>. background-image: url("...") sets the picture; the URL in it is relative to the CSS file (or to the page, for a <style> tag in the page). Add a background-color that looks close to the image. It shows while the image loads, or if it never does, so white text stays readable.

A banner with a background image
  • background-size: cover fills the whole box and crops what doesn’t fit. contain shows the whole picture and leaves empty space.
  • background-position: center picks which part stays when it crops, like top or right bottom.
  • background-repeat is repeat by default, which tiles a small image like wallpaper. Use no-repeat for a photo.
  • The background shorthand sets them all at once: background: #e5e7eb url("photo.jpg") center / cover no-repeat;. The size comes after the position, with a slash between them.
cover, contain and a repeating tile

img tag or background image?

Use <img> when the picture is content: a product photo, a chart, a photo in a story. It has alt text, search engines find it, and it prints. Use a background image for decoration behind other things, like a banner, a pattern or a texture. Background images have no alt text, and browsers leave them out when you print (unless the reader turns that on). For a solid color instead of a picture, see the background color tutorial.

Lazy loading

Add loading="lazy" and the browser waits to download the image until the reader scrolls close to it. On a long page with lots of pictures, the top of the page shows up faster and people who never scroll down don’t download images they never see. Every current browser supports it, and older ones just load the image straight away. In the small box below the image loads right away, because there’s nothing to scroll past (and browsers skip lazy loading when scripts are off).

loading="lazy"
  • Don’t lazy-load the first big image (like a banner at the top). It’s the one people see first, and waiting for it makes the page feel slower.
  • Keep width and height. They matter even more here, because lazy images arrive while the reader is already looking at the page.

JPG, PNG, WebP, AVIF, SVG or GIF?

All of these work in an <img> tag in every current browser. The right one depends on what the picture is.

FormatBest forGood to know
JPG Photos Small files, but no see-through parts. Saving it again and again loses a bit of quality each time.
PNG Screenshots, images with text, see-through backgrounds Keeps every pixel exactly, so photos come out much bigger than as JPG.
WebP Photos and graphics Usually smaller than JPG and PNG at the same quality, and it can be see-through.
AVIF Photos Often even smaller than WebP. Newer, so check older browsers you care about, or offer a fallback with <picture>.
SVG Logos, icons, simple drawings Drawn from shapes, not pixels, so it’s sharp at any size. All the pictures on this page are SVG.
GIF Small, simple animations Only 256 colors. For anything longer than a few seconds, a short video file is much smaller.

Need a picture of some HTML, like a card or a code sample? HTML to Image saves it as PNG, JPG or WebP. And for the little icon in the browser tab, use the Favicon Generator.

Questions people ask

How do I add an image in HTML?

Use the img tag with two attributes: src says where the image file is, and alt describes it in words. For example <img src="images/dog.jpg" alt="A brown dog catching a ball" width="600" height="400">. The img tag has no closing tag. Put the image file in your project folder and write the path from your HTML file to it.

Why is my image not showing in HTML?

Almost always the path in src is wrong. Check the folder (images/dog.jpg means a folder called images next to your HTML file), the letter case (Dog.JPG and dog.jpg are different files on most web servers), the extension (.jpg or .jpeg), and spaces in the file name. A path like C:\Users\you\dog.jpg only works on your own computer. Open the image address on its own in a new tab: if it doesn’t load there, the path is the problem.

How do I change the size of an image in HTML?

Set the width and height attributes in pixels, without units: <img src="dog.jpg" alt="..." width="300" height="200">. Or use CSS: img { width: 300px; height: auto; }. Setting only one size and height: auto keeps the shape of the picture. To make an image shrink on small screens, use img { max-width: 100%; height: auto; }.

How do I center an image in HTML?

An image is inline by default, so center it like text: put text-align: center on its parent. Or make the image a block and give it auto margins: img { display: block; margin: 0 auto; }. Flexbox works too: display: flex; justify-content: center; on the parent. The old <center> tag is obsolete, and align="center" on an image never centered it anyway.

Does the img tag need a closing tag?

No. img is a void element, so there is no </img>. You can write <img src="dog.jpg" alt="Dog"> or <img src="dog.jpg" alt="Dog" />. Both are valid HTML. The slash is only needed in XHTML and in JSX for React.

What should I write in the alt text?

Write what the image shows or does, in a short sentence, as if you were describing it on the phone: alt="Two cups of coffee on a wooden table". Skip "image of" and "picture of", since screen readers already say it’s an image. If the image is only decoration, use an empty alt (alt="") so screen readers skip it. If the image is a link, describe where the link goes.

Should I use an img tag or a CSS background image?

Use img when the picture is part of the content: a product photo, a chart, a photo in an article. It has alt text and search engines can find it. Use a CSS background image for decoration behind other content, like a banner behind a heading or a pattern. Background images have no alt text and browsers leave them out when you print, by default.

How do I make an image a link in HTML?

Put the img inside an a tag: <a href="gallery.html"><img src="dog.jpg" alt="See all dog photos" width="300" height="200"></a>. Because the image is the only thing in the link, its alt text becomes the link text, so describe where the link goes.

Which image format should I use on a website?

Photos: JPG, or WebP or AVIF for smaller files. Logos, icons and drawings: SVG, which stays sharp at any size. Screenshots and images that need a see-through background: PNG or WebP. GIF is only worth it for small, simple animations; a short video file is usually much smaller.

Keep going