Learn Images in HTML

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…

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 >.

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

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"
>

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

Which format for which image:

💡 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.

What you will learn