Chapter 3 of 15

HTML Links and Images (with File Paths Explained)

Links join pages together. Images make pages friendly. Both need something called an attribute.

What is an attribute?

An attribute gives extra information to a tag. It is written inside the opening tag like name="value".

Link code with labels for tag, attribute href, value about.html and link text
An attribute sits inside the opening tag: name="value".
HTML
<a href="https://www.wikipedia.org">Go to Wikipedia</a>

Here a is the tag (a = anchor, which means link). href is the attribute. Its value is the address to open.

Open a link in a new tab

HTML
<a href="https://example.com" target="_blank" rel="noopener">Open in new tab</a>

target="_blank" opens a new tab. rel="noopener" keeps your page safe from the other website. Use new tabs only when really needed, because most people like to decide this themselves.

Email, phone and download links

Links can do more than open pages:

HTML
<a href="mailto:hello@example.com">Email us</a>
<a href="tel:+919800000000">Call us</a>
<a href="files/price-list.pdf" download>Download the price list</a>
  • mailto: opens the visitor's email app with your address filled in.
  • tel: starts a phone call on mobile. Write the number with the country code and no spaces.
  • download tells the browser to save the file instead of opening it.
Show the details tooSome people use a computer with no email app set up. Write the email address or phone number as visible text as well, so they can copy it.

Adding images

Use the <img> tag. It has no closing tag.

A picture of mountains next to a box with its alt text, and labels for src, alt, width and height
Always write alt text. If the image does not load, the alt text shows instead.
HTML
<img src="images/taj-mahal.jpg" alt="Taj Mahal in the morning sun" width="800" height="533">
  • src: where the image file is.
  • alt: a short description of the image. Blind users hear this text. Google also reads it. Always write alt text.
  • width and height: the real size of the image. This stops the page from jumping while the image loads.

Which image format should I use?

Four tiles: JPG for photos, PNG for screenshots, SVG for logos, WebP for small fast images
Pick the image format by what the picture is.
FormatBest for
JPGPhotos
PNGScreenshots, images that need a see-through background
SVGLogos and icons (stays sharp at any size)
WebP / AVIFModern format, much smaller files. Best for speed.
Keep a clean folderPut all pictures in an images folder. Use small letters and dashes in file names: red-car.jpg, not Red Car (1).JPG.

Keep image files small

A photo straight from a phone can be 4 to 8 MB. On a slow mobile connection, that one photo can take many seconds to load, and Google ranks slow pages lower. Before you add any photo:

  1. Resize it. If it will show at 800px wide, there is no need for a 4000px image. Save it at about 1600px wide at most (twice the size, for sharp screens).
  2. Compress it. Free tools like Squoosh make files much smaller without a visible change. Aim for under 200 KB for most photos.
  3. Use WebP where you can. It is usually much smaller than JPG for the same quality.

Mini project: a 3-page website

Make this folder structure:

Folders
my-website/
  index.html
  about.html
  gallery.html
  images/
    photo1.jpg
    photo2.jpg

Add the same small menu at the top of all three pages, so visitors can move between them:

HTML
<nav>
  <a href="index.html">Home</a> |
  <a href="about.html">About</a> |
  <a href="gallery.html">Gallery</a>
</nav>

On gallery.html, add two photos with good alt text and width and height. Click every link to check that it works. You now have a real multi-page website.

Why is my image or link broken?

A broken image shows a small icon or the alt text instead. Check these, in this order:

CheckExample of the problem
Is the path right from this file?The page is inside blog/, so it needs ../images/cat.jpg
Do capital letters match exactly?The file is Cat.JPG but the code says cat.jpg. This often works on Windows but breaks online.
Is the file ending right?.jpeg and .jpg are different names
Are there spaces in the name?Rename my photo.jpg to my-photo.jpg
Quick testRight-click the broken image and open it in a new tab. The address bar shows exactly where the browser looked for the file.

Practice time

  1. Make two pages, index.html and about.html, and link them to each other.
  2. Add one photo with good alt text.
  3. Add a link that jumps to the bottom of the page.
  4. Add an email link and a phone link to your About page.
  5. Compress one photo with Squoosh and compare the file size before and after.

Quick summary

  • Attributes give extra info: href, src, alt.
  • Use relative paths for your own pages, full URLs for other sites.
  • Always add alt text and width/height to images.