HTML and CSS · Chapter 2 of 12
Links and images
By the end of this chapter you can link one page to another with <a href>, write a path that finds a file in the folder, link to an email address, a phone number and another site, jump to a part of a page with an id, and show an image with <img>, with the alt text that says it in words and the width and height that keep the page still. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: HTML: the structure
- 4 tasks
- HTML and CSS
- Free chapter
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.
Why you need this
Parda now has a home page and Datta's box office page, and nothing joins them. Sayali has promised every venue owner a page of their own, with a picture of their building, and the first two are ready: Kala Talkies and Rangdeep Studio Theatre. A person on the home page cannot get to them. Datta still answers about 150 calls a day, and many are "how do I find the page for Kala Talkies?" or "what is your email?".
Everything you know so far is text inside one file. Headings and paragraphs cannot open another page, start an email or show a picture. A web is pages joined by links, and that is the H in HTML: hypertext, text that leads somewhere.
The lesson
A link is an a with an href
Here is a line from Moti Mahal Talkies' page:
<p>Also in Kothrud: <a href="nilambari.html">Nilambari Cinema</a>.</p>
<a>is the link, an anchor. Its content,Nilambari Cinema, is what the page shows and what people click.href(hypertext reference) is the address it opens. Without anhref, anais not a link at all: nothing happens on a click.- An
asits inside a paragraph, among its words. The full stop is outside the link, so it is not underlined.
In the Preview, a link to another page in the chapter's folder opens it, and the Preview's header names the page you are on.
Paths: where the browser looks
nilambari.html is a relative path: it says where the file is from the page it is on. The browser joins it to the page's own address. Say Moti Mahal's page lives at https://parda.example/moti-mahal.html; these are the addresses the browser builds from a few hrefs (from Node's URL, which follows the same standard as browsers):
nilambari.html https://parda.example/nilambari.html
Nilambari.html https://parda.example/Nilambari.html
venues/nilambari.html https://parda.example/venues/nilambari.html
nilambari https://parda.example/nilambari
#seats https://parda.example/moti-mahal.html#seats
Every line is a different address. A file name is exact: most web servers treat Nilambari.html and nilambari.html as two files, so a capital letter that works on your laptop can break on the real site. venues/ means a folder called venues. A name without .html is another name. The Preview says what it looked for:
The link to Nilambari.html goes nowhere: there is no Nilambari.html in this chapter's folder.
An absolute URL is the whole address: the scheme (https:), the site and the path. It means the same on every page, so you use it for other sites. For your own pages, relative paths keep working when the whole site moves to another address.
Email, phone and other sites
The address does not have to be a page:
<p>Call the box office: <a href="tel:+912041231952">020 4123 1952</a>.</p>
<p>Questions? <a href="mailto:help@parda.example">Write to Parda</a>.</p>
<p><a href="https://parda.example/moti-mahal.html">Moti Mahal on Parda's site</a></p>
mailto:and an address opens the visitor's email app, with the address filled in.tel:and a number starts a call on a phone. Write the number with the country code and no spaces; the link text can be as people read it.https://opens another site.
The Preview has no internet, so it opens none of them and says so in its console:
The link to tel:+912041231952 does not open in the Preview, which has no internet. It works on a real site.
Link text says where it goes. Screen reader users often list a page's links and jump to one, hearing only the text. "Click here", three times, tells them nothing; "Nilambari Cinema" tells them everything.
A link within a page
Any element can be given a name with the id attribute, and a link whose address starts with # jumps to it. On Sahyadri Lawns' page:
<p><a href="#seats">Seats and prices</a></p>
...
<h2 id="seats">Seats and prices</h2>
lab tree shows both, the id on the heading and the address on the link:
$ lab tree sahyadri.html
...
<p>
<a href="#seats"> "Seats and prices"
<p> "Aundh, Pune. An open-air lawn since 2015, with 870 seats."
<h2 id="seats"> "Seats and prices"
- The id has no
#; the address has one.#seatsalone means "on this page". - An id is a name for one element: no other element on the page may have the same id.
moti-mahal.html#seatswould open another page and then jump.
Images
<img
src="moti-mahal.svg"
alt="Moti Mahal's white front, its name over the screen"
width="400"
height="240"
>
<img>is an empty element, like<meta>: one tag and nothing to close.srcis the image file's path, found exactly like a link's.- In this lab images are SVG files in the chapter's folder (drawings written as text). The Preview cannot show PNG or JPEG files.
alt: the image in words
A screen reader reads alt aloud, a search engine reads it, and the browser shows it when the image cannot load. Here is the same page with the file named moti-mahal.png by mistake: the Preview shows a small broken-image icon and, in its place, Moti Mahal's white front, its name over the screen. Leave alt out, and a validator says so:
$ lab validate no-alt.html
no-alt.html line 11: <img> is missing required "alt" attribute
Without alt, many screen readers read the file name instead. Write what the picture tells a person, in a few words. Do not start with "image of": the screen reader already says "image". A picture that is only decoration gets alt="", an empty alt, which tells a screen reader to skip it.
width and height: room before it loads
A page shows its text first and its images as their files arrive. If the browser does not know an image's size, it lays the page out with no room for it, then pushes everything down when the file comes in, and the paragraph you were reading jumps away. width and height, in pixels, are the size the file is drawn at, and the browser keeps that room from the start. lab box measures the image:
$ lab box moti-mahal.html img
moti-mahal.html at 1000 px wide: 1 element matches img
<img>
at x 144, y 162.3; 400 x 240 px, padding and border included (display: inline, box-sizing: border-box)
content 400 x 240, padding 0, border 0, margin 0
$ lab box moti-mahal.html img --width 400
...
at x 24, y 238.59; 352 x 211.19 px, padding and border included (display: inline, box-sizing: border-box)
At 400 px wide Ira's styles shrink it to fit the column, and it keeps its shape: 352 by 211, the same 5 to 3 as 400 by 240.
Your turn
Four tasks. Task 1 makes three words of the home page's new paragraph into links to the venue pages and the box office page; click each one in the Preview. Task 2 gives Kala Talkies' page Parda's logo, a drawing of the cinema with an alt you write, an email link and a link home. Task 3 gives Rangdeep's box office heading an id and adds a link that jumps to it (careful: the id has no #). Task 4 is an interview question: Datta's box office page has an image that does not show and three links that go nowhere. Click each, read the Preview's console, and compare with ls. Be ready for Ira's question: what is the difference between a relative and an absolute URL, and what is alt for? Press Check when a task looks right.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
index.html: links to the venue pages
Parda's home page has a new paragraph under its first h2: Read about Kala Talkies or Rangdeep Studio Theatre, and check the box office hours before you go. Make three of its words into links to pages in this folder: Kala Talkies opens kala-talkies.html, Rangdeep Studio Theatre opens rangdeep.html, and box office hours opens box-office.html.
Each link's text is exactly those words, and the three links stay in that one paragraph with its other words as they are. File names are exact: small letters, a hyphen, and .html at the end.
- Task 2
kala-talkies.html: the logo, a drawing and two links
Ira's design for Kala Talkies' page adds two images and two links. At the top of the body, before the h1, an image of logo.svg with alt="Parda", width="160" and height="48". Under the first paragraph, before the h2 Seats and prices, an image of kala-talkies.svg with width="600" and height="360" and an alt you write: what the drawing shows, in a few words (three or more), for someone who cannot see it; not the file's name, and not starting with "image of" or "picture of".
In the paragraph Questions about a show or a seat? Write to help@parda.example., the address help@parda.example is a link to mailto:help@parda.example. In the last paragraph, the words Parda's home page are a link to index.html. Keep the page's title, Kala Talkies, Sadashiv Peth: Parda.
- Task 3
rangdeep.html: a link within the page
Rangdeep's page is long, and most people want to know when its box office opens. Give the h2 Box office the id box-office (id="box-office"), and make the paragraph Box office hours, under the first paragraph, a link to #box-office, so clicking it jumps to that heading on the same page.
The link text is exactly Box office hours. Only the h2 has the id box-office. Keep the page's title, Rangdeep Studio Theatre, Erandwane: Parda.
- Task 4 · interview question
Interview: box-office.html, links that go nowhere
Datta added a drawing and three links to box-office.html. The drawing does not show and none of the links opens a page. Fix the page so that the image shows box-office.svg, with alt="Parda's box office window", width="320" and height="200"; the link Kala Talkies opens kala-talkies.html; the link Rangdeep Studio Theatre opens rangdeep.html; and the link Parda's home page opens index.html. Keep the text of each link as it is, and keep the page's title, Box office hours: Parda.
Ira will ask: what is the difference between a relative and an absolute URL, and what is alt for?
What you will learn
- <a href>: a link, the words people click and the address they open.
- Paths: a relative path finds a file from where the page is; an absolute URL is a whole address.
- mailto:, tel: and https:: links to an email, a phone call and another site.
- id and #: a name for one element, and a link that jumps to it.
- <img src alt width height>: an image, its words for someone who cannot see it, and its size.
More in HTML: the structure
Build "Links and images" yourself
Parda is launching its first website for Pune's own cinemas and stages. You build it, page by page. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.