The menu photos on a food stall's page, the logo in the top corner, your friend's profile picture: they're all image files, separate from the HTML file. You don't put the image itself inside the HTML. You only write the address of the image file in an <img> tag (from image). Then the browser gets that file and shows it.
<img> is different from <p> or <h1>: it doesn't wrap any text, so it has no closing tag. All its information goes in attributes. An attribute is extra information inside the tag, written as name="value".
Putting an image on the page
Here is the tag for one image:
<img
src="https://images.unsplash.com/photo-1501785888041-af3ef285b470"
alt="A mountain and lake landscape"
>
This is one <img> tag, just written over several lines so it's easier to read. The tag starts at <img and ends at >.
<imgstarts the image tag.src="..."is the address of the image file (from source). In this example it is the address of a photo on the Unsplash site. It's long, but you don't need to memorise it. Just copy it.alt="..."is the replacement text for the image: what's in the image, written in words.>closes the tag. There's no</img>.
The result: the landscape photo shows on the page. The alt text doesn't show if the photo loads successfully. You can also write the attributes on one line. It works the same.
How to write attributes
- Separate attributes with a space:
src="photo.jpg" alt="Photo", notsrc="photo.jpg"alt="Photo". - Put the value in double quotes:
alt="Profile Photo". Without quotes, the browser readsalt=Profile Photoas justProfile. - Spell the attribute's name exactly:
src, notscr. - End the tag with
>. If you forget it, the browser can't tell where the tag ends, so the image or the text after it may not show. Sometimes you'll see/>at the end, as in<img src="photo.jpg" />. It works the same as>.
src: where the image is
The image's address always goes in src. Don't confuse it with href: href is the address attribute for <a> links, which you'll learn in the next lesson. So <img href="photo.jpg"> shows nothing.
The address must lead directly to the image file, not to a web page that has the image on it. To check, open the address in a new tab. You should see only the image. An image from another site needs the full address, starting from https://. An image file in the same folder as your HTML file only needs its name, like src="photo.jpg".
alt, width and height
Here's the same image, now with a size:
<img
src="https://images.unsplash.com/photo-1501785888041-af3ef285b470"
alt="A mountain and lake landscape"
width="300"
height="200"
>
altis the text that screen readers (programs that read pages aloud for blind users) read, because their users can't see the image. Search engines like Google also use it to learn what the image shows. So write what is in the image, for examplealt="Profile Photo". This text also shows on the page if the image fails to load.widthis how wide,heightis how tall. The unit is pixels, the tiny dots a screen is made of. Write just the number, without addingpx(short for pixel).
In the browser, the same photo is now 300 pixels wide and 200 pixels tall. Fill in width and height with the real size of the file, or with the same ratio of width to height. A 600 x 400 photo can be 300 x 200 (both halved), but with width="300" height="300" it looks squashed.
Why write the size? An image file takes time to load. If the browser knows the image's size from the start, it can keep an empty space ready for the image. Without width and height, the text below the image can jump (get pushed down suddenly) when the image appears.
Two images means two <img> tags, each with its own src and alt.
⚠️ What often goes wrong in an image tag
- Writing
</img>.<img>has no closing tag. Just end the tag with>. - Mixing up
widthandheight.widthis how wide,heightis how tall. - A huge image file. The browser still downloads all of a 5 MB photo, even when the photo shows small. Make the file smaller first, or use an image format with smaller files, like WebP.
Which format for which image:
- JPG: photos. Every browser can open it.
- WebP or AVIF: photos too, usually with a much smaller file than JPG. Modern browsers can show both.
- PNG: screenshots, images with text, and images with a transparent background.
- SVG: logos, icons and diagrams. They stay sharp at any size.
💡 Images far down the page can wait. Add the attribute
loading="lazy"and the browser only loads that image when the visitor scrolls close to it. The page opens faster. Do not add this attribute to an image that is visible as soon as the page opens, such as a large photo at the top: that image then appears later, not sooner.
💡 Images wider than a phone screen. A photo 600 pixels wide does not fit on a phone screen 360 pixels wide, so the page becomes wider than the screen and must be scrolled sideways. In the CSS lessons, the rule
img { max-width: 100%; height: auto; }makes images shrink to fit the screen without being squashed.
TL;DR: You add an image with <img>, which has no closing tag. Its information goes in attributes, written name="value". src holds the image file's address (not href), and alt holds the replacement text: what's in the image, in words. width and height hold the size in pixels, without px. Separate attributes with spaces, put values in double quotes, and end the tag with >.
⚡ An image with no closing tag
Write:
<img
src="https://images.unsplash.com/photo-1501785888041-af3ef285b470"
alt="A mountain and lake landscape"
>
Guess first: where's the closing tag?
There isn't one. <img> doesn't wrap any text, so there's no </img>. The tag ends at >.
Meaning: src holds the image file's address, alt says what's in the image in words. Screen readers read the alt, and it shows on the page if the image fails to load.
⚡ The image's address always goes in src
Guess first: what does <img href="photo.jpg"> show?
Nothing. href is used on <a> links, not on images.
Meaning: Attributes (extra information inside a tag, like src) are written name="value", separated by spaces, with the value in double quotes. An image file in the same folder as your HTML file only needs its name, like src="photo.jpg".
⚡ A size so the page doesn't jump
Write:
<img
src="https://images.unsplash.com/photo-1501785888041-af3ef285b470"
alt="A mountain and lake landscape"
width="300"
height="200"
>
Meaning: width is how wide, height is how tall. The unit is pixels (the tiny dots on a screen), and you write the number without px. When the browser knows the size from the start, it can keep a space ready before the image finishes loading, so the text below doesn't jump.