{module 3}
Links and images
Your first page in HTML20 min read
What you will be able to do
- Write links to a page in your own folder, to another website and to an email address.
- Add an image with alternative text that says what it shows, or an empty one when it shows nothing that matters.
- Choose link text that makes sense when it is read on its own.
Why it matters
Baltic Amber Studio makes jewellery from amber in Gdańsk, Poland. Its owner, Marta Kowalska, put a photo of her best pendant on the shop's page. Visitors from abroad found the page, looked at the pendant, and left. Marta received almost no emails.
When she asked a friend to try the page, two problems appeared. The page had no way to reach her: her email address was written as text, and nothing happened when you tapped it. And a visitor who uses a screen reader heard only "IMG underscore 4471 dot jpg" where the photo was.
Links tell visitors what they can do next, and alternative text tells everyone what an image shows. This lesson adds both.
The idea
A link is an a element
The href attribute of a is the address the link goes to. The text between the tags is what the visitor sees and clicks:
<p><a href="care.html">How to look after amber</a></p>The browser shows link text underlined and in blue until CSS changes it. A link is not a paragraph by itself, so here it sits inside a p.
Addresses inside your folder and outside it
care.html is a relative address. It means "the file called care.html in the same folder as this page". It keeps working when you move the folder, and when you put it online, because it does not say where the folder is.
For a page on another website, write the absolute address: the whole address, starting with https://, as you see it in the browser's address bar.
<p><a href="https://www.openstreetmap.org/">Find Gdańsk on OpenStreetMap</a></p>Without https://, the browser reads the address as relative. It looks in your own folder for a file called www.openstreetmap.org, and finds nothing. Always copy the whole address from the address bar.
A link that opens an email
An address that starts with mailto: opens a new email in the visitor's email program, already addressed:
<p><a href="mailto:marta@balticamber.example">Email Marta at the studio</a></p>There is no space after mailto:. The ending .example is reserved for examples, so no real email address uses it. On a real page, write the real address.
Link text that makes sense alone
A screen reader can list all the links on a page, the same way it lists the headings. Its user hears only the link text. If every link says "click here", the list is three times "click here", and nobody can tell them apart. Write link text that says where the link goes.
Images are files
An image is a separate file, such as a photo or a drawing, that sits in your folder beside the page. The img element puts it on the page:
<img
src="pendant.svg"
alt="An amber pendant shaped like a drop, on a thin silver chain"
width="200"
height="240"
/>Like meta, img has no content and no closing tag. An element with several attributes may be written over several lines, as here, so that each one is easy to read.
srcis the address of the image file, written like anhref. Here it is a relative address: the filependant.svgbeside the page. A file ending in.svgis a drawing described in text. A photo usually ends in.jpgor.png.altis the alternative text: words that stand in for the image.widthandheightare the size of the image in pixels, the tiny dots a screen is made of. They are written as numbers with no unit.
What to write in alt
A screen reader reads the alternative text aloud in place of the image. The browser also shows it when the image cannot load, for example on a slow connection.
- Say what the image shows, in one short sentence. Ask yourself: if I described this image on the phone, what would I say?
- Do not start with "picture of" or "image of". The screen reader already says it is an image.
- If the image only decorates the page and adds nothing a visitor needs, write
alt="", with nothing between the quotes. A screen reader then skips it. - Never leave
altout. Without it, some screen readers read the file name aloud, which is what happened on Marta's page.
Why write the size
The browser draws the page before every image has arrived. With width and height, it knows how much room to keep, so the text below does not jump down when the image appears. A photo straight from a phone is often thousands of pixels wide and several megabytes, which is slow to load on a phone connection. Before you put a photo on a page, make a smaller copy of it in an image editor.
Worked example
Make a folder called baltic-amber in your course folder. In it, create a file called pendant.svg and type this drawing into it. You do not need to understand it: it draws a pendant.
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="240" viewBox="0 0 200 240">
<rect width="200" height="240" fill="#f6f1e7" />
<path d="M40 10 Q100 90 160 10" fill="none" stroke="#9aa0a6" stroke-width="3" />
<circle cx="100" cy="72" r="6" fill="none" stroke="#9aa0a6" stroke-width="3" />
<path d="M100 80 C70 130 60 160 60 180 A40 40 0 0 0 140 180 C140 160 130 130 100 80 Z" fill="#d98a1e" />
<ellipse cx="88" cy="170" rx="8" ry="16" fill="#f2b84b" />
</svg>Next to it, create index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Baltic Amber Studio, Gdańsk</title>
</head>
<body>
<h1>Baltic Amber Studio</h1>
<p>Jewellery made by hand from Baltic amber, in the old town of Gdańsk.</p>
<h2>This month's pendant</h2>
<img
src="pendant.svg"
alt="An amber pendant shaped like a drop, on a thin silver chain"
width="200"
height="240"
/>
<p>Amber drop on a silver chain, $85.</p>
<p><a href="care.html">How to look after amber</a></p>
<h2>Visit or write to us</h2>
<p>The studio is in Mariacka Street, near the river.</p>
<p><a href="https://www.openstreetmap.org/">Find Gdańsk on OpenStreetMap</a></p>
<p><a href="mailto:marta@balticamber.example">Email Marta at the studio</a></p>
</body>
</html>And a second page, care.html, in the same folder:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>How to look after amber, Baltic Amber Studio</title>
</head>
<body>
<h1>How to look after amber</h1>
<ul>
<li>Take it off before you swim or wash your hands.</li>
<li>Keep it away from perfume and hair spray.</li>
<li>Clean it with a soft, dry cloth.</li>
</ul>
<p><a href="index.html">Back to the studio's page</a></p>
</body>
</html>Save all three files and open index.html in your browser. You see the pendant under "This month's pendant". This is the text around it:
Baltic Amber Studio
Jewellery made by hand from Baltic amber, in the old town of Gdańsk.
This month's pendant
Amber drop on a silver chain, $85.
How to look after amber
Visit or write to us
The studio is in Mariacka Street, near the river.
Find Gdańsk on OpenStreetMap
Email Marta at the studioThese are the three links, as a screen reader would list them. Each one makes sense alone:
How to look after amber
Find Gdańsk on OpenStreetMap
Email Marta at the studioClick "How to look after amber". The care page opens, and the tab shows its title:
How to look after amber, Baltic Amber StudioClick "Back to the studio's page" to return. The image is not shown as text, but the browser has its alternative text and its size:
An amber pendant shaped like a drop, on a thin silver chain
200
240Click "Email Marta at the studio". If your computer has an email program, it opens a new email addressed to marta@balticamber.example. If it has none, nothing may happen, or the browser may ask which program to use.
Common mistakes
The image is in another folder
The learner wrote images/pendant.svg, but the file sits beside the page, not in a folder called images:
<img
src="images/pendant.svg"Where the image should be, the page shows the alternative text, often with a small broken-image sign. The Console in the developer tools shows an error. This is what the checker printed when it opened the page:
Baltic Amber Studio
page error: Failed to load resource: net::ERR_FILE_NOT_FOUND"Failed to load resource" means the browser asked for a file and did not get it. ERR_FILE_NOT_FOUND says why: there is no file at that address. Compare the src with where the file really is, letter by letter. Here the fix is src="pendant.svg".
An image with no alt
<img src="pendant.svg" width="200" height="240" />The page looks exactly the same. Here is the value of its alt attribute:
nullnull means there is no such attribute. A screen reader may read the file name instead. Add an alt that says what the image shows.
Every link says "click here"
<p>To learn how to look after amber, <a href="care.html">click here</a>.</p>The page reads well to someone who sees it. Here are its links, as a screen reader lists them:
click here
click here
click hereThree links, and no way to tell them apart. Put the link on the words that say where it goes: <a href="care.html">how to look after amber</a>.
Think it through
Yuki is adding three images to the page of a small bakery: the bakery's logo, which shows its name, a photo of the cake of the week, and a thin wavy line that separates two parts of the page. Each needs an alt.
Understand. In: three images. Out: for each one, the words a person who cannot see it needs, or nothing at all.
Break down. One question for each image: what would a visitor miss if this image were gone?
Plan. The logo carries the bakery's name, so its alt is the name. The cake photo is what the page is selling, so it needs a short description. The wavy line carries no information, so its alt is empty.
Build. She writes alt="Cedar Bakery" for the logo, alt="A lemon cake with white icing and sliced almonds" for the photo, and alt="" for the line.
Test. She reads the page aloud as a screen reader would, saying each alt where its image is. The page still makes sense. The odd case: she renames the cake file so that it cannot load. The browser shows the description in its place, and a visitor still knows what the cake is.
Reflect. The question "what would a visitor miss?" decided all three. The same image can need different text on different pages: on a page about the bakery's history, the logo might need a description of how it looks.
At work
A link is a choice you offer the visitor. Before you add links, find out which choice matters most to the client. Marta first said: "Put everything on the page." A careful developer asks:
When someone has read the page, what do you most want them to do next: email you to order, come to the studio, or read about the amber? If it is only one of these, which one?
Marta answers: "Email me. Most orders come from people who live abroad." That changes the page. The email link goes right after the pendant and its price, where the visitor decides. Its text says what happens: "Email Marta to order this pendant". The map stays, lower down, for the few who visit. Without the question, the email link would have stayed at the bottom, after everything else.
Your tasks
Task 1: Build the pages and follow every link
Build the three files of the worked example. Follow the link to the care page and back. Click the email link and see what your computer does.
You are done when both links between the pages work, and you have read the pendant's alternative text in the Elements panel.
Task 2: Break the image on purpose
Change the src to images/pendant.svg. Save, reload, and look at the page and at the Console. Then put it back.
You are done when you have seen the alternative text where the image should be, and the error in the Console, and the image is back.
Task 3: An address without https
In the map link, delete https:// from the address. Save, reload, click the link, and read the address in the address bar. Then put it back.
You are done when you can say in one sentence where the browser looked for the page, and why.
Task 4: An image of your own
Draw a second image for the studio by copying pendant.svg to ring.svg and changing the colours, or use any small image you made yourself. Add it to the page with an alt you wrote by asking what a visitor would miss without it.
You are done when the new image shows, its alt describes it in one short sentence without "picture of", and its width and height are set.
Recap
- A link is an
awith anhref. A file name is a relative address. An address on another site starts withhttps://. mailto:and an email address make a link that opens a new email.- Link text says where the link goes. Never "click here".
- Every
imghas asrcand analt: one short sentence of what it shows, oralt=""when it adds nothing. widthandheightare numbers of pixels, with no unit, so the browser keeps room for the image.
Go deeper
- MDN: Creating links covers relative and absolute addresses, email links and good link text.
- MDN: HTML images explains
img, its attributes and how to write alternative text. - MDN: The img element is the reference for every attribute of
img, includingwidthandheight. - WebAIM: Alternative text shows, with many examples, what to write for a photo, a logo and a decoration.