HTML REFERENCE · 03
Links and Images
Links take visitors to another location. Images bring a separate visual file into the page. Their attributes tell the browser where to go and what the content means.
Links and Images at a glance
| Element or attribute | What it means | When to use it |
|---|---|---|
a |
An anchor creates a hyperlink when it has an href attribute. | Use descriptive link text for navigation, such as “Read the text lesson.” |
href |
The destination URL of a link. | Use a relative path for a nearby page, a full https URL for another website, or # followed by an element ID for a place on this page. |
id |
A unique identifier for an element in this document. | Give a destination an ID so a fragment link can point to it. Match spelling and capitalization. |
target="_blank" |
Requests a new browsing context, usually a new tab. | Use when opening a separate tab is useful, and tell readers in the link text. Normal links stay in the same tab. |
rel="noopener" |
Prevents a newly opened page from controlling the original tab through its opener. | Include it explicitly with target="_blank". Modern browsers also give _blank links this protection by default. |
img |
Embeds an image. It is a void element with no closing tag. | Use for images that belong to the content. The image file must exist at the given location. |
src |
The URL from which the browser loads an image. | Use a path relative to this HTML file or a full image URL. Paths and filename case must match the actual file. |
alt |
A text alternative conveying an image’s purpose in context. | Describe meaningful information. Use alt="" for a decorative image. If an image is the only link content, describe the destination or action. |
width / height |
Image dimensions in CSS pixels; together they also establish an aspect ratio. | Use the image’s intrinsic dimensions to reserve space while it loads. CSS can adapt the displayed size. |
figure / figcaption |
A self-contained figure and its visible caption. | Use when an image needs an accompanying caption. Put figcaption first or last inside figure; a caption does not replace alt. |
Choosing a destination
A relative path starts from the folder containing this page. Here, ../ goes up to the guide folder. A full URL includes its scheme and website name.
Code
<p><a href="../02-text/index.html">Read the text lesson</a></p>
<p><a href="https://www.w3schools.com/html/"
target="_blank" rel="noopener">W3Schools HTML reference (opens in a new tab)</a></p>
Output
Write link text that explains the destination. “Click here” gives little information when a link is read on its own.
Linking within this page
The value after # matches the destination’s ID. IDs must be unique within a document. This small example jumps to its own destination paragraph.
Code
<p><a href="#trail-description">Jump to the trail description</a></p>
<p id="trail-description">The ridge trail follows the mountainside.</p>
Output
The ridge trail follows the mountainside.
Loading an image and adding a caption
This lesson includes trail.svg in the same folder as its HTML file. No folder prefix is needed. The caption names the illustration; the alternative describes its visual content.
Code
<figure>
<img src="trail.svg" alt="A pale trail curves between two green mountains."
width="126" height="70">
<figcaption>A simple ridge trail illustration.</figcaption>
</figure>
Output
For a decorative flourish that adds no information, use an empty alt attribute. Omitting alt is different: assistive technology may announce the filename instead.
Using an image as a link
Put the image inside the anchor. Because this image is the link’s only content, its alternative names the destination rather than describing the mountains.
Code
<a href="../index.html">
<img src="trail.svg" alt="HTML Guide: all topics" width="126" height="70">
</a>
Output
The examples use a local image so they can render offline. A full external image URL depends on another server remaining available.
Further reading: W3Schools: Links and Images. The explanations and examples here are written for this guide.
Study questions
Answer in your own words before opening each explanation.
What does ../ mean in a link?
It goes up one folder from the HTML file’s location. From this lesson, ../index.html reaches the guide homepage.
How does href differ from src?
Href gives a link destination; src tells the browser which image resource to load into the page.
What must match in a fragment link?
The text after # must match an element’s unique id in the destination document.
Why write descriptive link text?
Readers should understand the destination without needing the surrounding sentence. This also helps people navigating a list of links with assistive technology.
When should alt be empty?
When the image is purely decorative or repeats information already available in the same context. Keep the attribute present as alt="".
What should the alternative say for an image-only link?
It should explain the link’s destination or action, such as “HTML Guide: all topics.”
Does figcaption replace alt?
No. A visible caption and a text alternative serve different purposes. Choose alternative text that supplies the useful visual information in context.
Why include width and height?
They let the browser reserve appropriately shaped space before the image finishes loading, reducing layout movement.