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.
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 write | The 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.
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.jpgonly works if theimagesfolder sits next to your HTML file. If your page is in a subfolder, you may need../first. - Upper and lower case.
Dog.JPGanddog.jpgare 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.
.jpgand.jpegare different names, and so are.pngand.PNG. Copy the file name exactly. Also check for hidden doubled ones likephoto.jpg.jpg, which Windows can hide when it hides extensions. - Spaces and odd characters.
my photo.jpgusually works, but it’s easy to get wrong. Rename it tomy-photo.jpgand save yourself the trouble. - A path from your own computer.
src="C:\Users\you\Desktop\dog.jpg"or afile:///address only works on your computer. Put the image in your project folder and use a path likeimages/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 text | Why |
|---|---|
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.
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.
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 { ... }.
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.
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:
- Make it a block with
display: block, thenmargin: 0 auto. - Center it like text:
text-align: centeron the parent element. - Flexbox:
display: flex; justify-content: center;on the parent.
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.
Make an image a link
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".
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> 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.
-
background-size: coverfills the whole box and crops what doesn’t fit.containshows the whole picture and leaves empty space. -
background-position: centerpicks which part stays when it crops, liketoporright bottom. -
background-repeatisrepeatby default, which tiles a small image like wallpaper. Useno-repeatfor a photo. - The
backgroundshorthand 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.
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).
- 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
widthandheight. 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.
| Format | Best for | Good 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
Tools that help
- Responsive Image Generator Build srcset, sizes and picture code for sharp images on every screen.
- Image Map Generator Draw clickable areas on an image and copy the map code.
- Favicon Generator Turn text, an emoji or your logo into the little tab icon.
- HTML to Image Save a piece of HTML as a PNG, JPG or WebP picture.
More tutorials
- How to Center Text, a Div or an Image in HTML & CSS
- How to Add a Line Break in HTML
- How to Add Spaces in HTML
- How to Change Text Color in HTML
- How to Change Background Color in HTML
- What Is a URL? Meaning, Parts and Examples
- How to Redirect a Page in HTML
- How to Get an Image URL
- Canonical URL
- What Is HTML? What It Stands For and How It Works
- How to Create, Open and Run an HTML File
- How to Link CSS to HTML
- How to See the HTML Code of a Website
- How to Link JavaScript to HTML with the script Tag
- HTML hr Tag
- HTML Comments
- How to Make Text Bold in HTML
- HTML Font and Font Size
- All tutorials