Loading...

HTML essentialsContent

Links & images

5 min lesson

Next
  • A link takes the user to another page, and an image shows a picture on the page.
  • Links use <a href="...">text</a>; images use <img src="..." alt="...">.
  • The alt text describes the image for people who cannot see it and for search engines.
  • Links can go to another site (absolute URL) or to a file in your project (relative path).
  • Images are self-closing: <img> has no closing tag, and the browser fetches the file from src.
  • This builds on Lists and earlier Content lessons: you now make content clickable and visual.
  • The example has a link to another page, an internal link to a section, and an image with alt text.
  • Next, Figure, figcaption & image best practices adds captions and rules for images.
Keep in mind
  • Always give links clear text and images meaningful alt text.
  • Use relative paths for files in your project.
  • Every img needs alt; use alt="" only for purely decorative images.
  • Test every link by clicking it, not just reading the code.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Links & images</title>
</head>
<body>
  <!-- A link to another site; opens in the same tab -->
  <p><a href="https://example.com">Visit example.com</a></p>
  <!-- An internal link to a heading with id="details" -->
  <p><a href="#details">Jump to details</a></p>
  <!-- An image with alt text; src points to a file in the project -->
  <img src="cat.jpg" alt="A small orange cat sitting on a chair">
  <!-- The target of the internal link -->
  <h2 id="details">Details</h2>
  <p>You jumped here by clicking the link above.</p>
</body>
</html>

What happens: A page with the link "Visit example.com", the link "Jump to details", an image of a small orange cat, and the heading "Details" followed by "You jumped here by clicking the link above."

Breaking it down

a href absolute

<a href="https://example.com">...</a>

An absolute URL points to another site. The browser goes straight to that address when the link is clicked.

a href relative

<a href="about.html">...</a>

A relative path points to a file in your project. The browser looks in the same folder as the page.

a href #id

<a href="#details">...</a>

Jumps to the element with id="details" on the same page. Useful for tables of contents.

img

<img src="cat.jpg" alt="...">

Embeds the image at src. alt describes the picture and shows if the image fails to load.

Link & image parts

AttributeOnPurpose
hrefaWhere the link goes
target="_blank"aOpens in a new tab
srcimgThe image file path
altimgA text description of the image

A helpful analogy

A link is a doorway to another room, and the href is its address. An image is a framed picture on the wall; alt text is the label underneath that describes it for anyone who cannot see it.

Nesting rules

  • 1.Do not nest a link inside another link.
  • 2.img has no closing tag, so never write </img>.

Common mistakes

Missing alt text on an image

Avoid

<img src="cat.jpg">

Use instead

<img src="cat.jpg" alt="A small orange cat">

Screen readers read the alt text out loud. Without it, blind users hear only the file name or nothing at all.

Using a relative path that does not exist

Avoid

<img src="images/cat.jpg"> when the file is in the same folder

Use instead

<img src="cat.jpg">

A wrong path means a broken image icon. Match the path to the real folder layout.

Quick tips

  • •Write link text that makes sense out of context; avoid "click here".
  • •Check the Network panel to see whether each image and link target returned 200, not 404.
Previous
Next