Skip to content
skillbld
HTML & CSS · 14-day planAll days
Day 3 of 14 25 min

Learn attributes, then use them to link pages together with the anchor element, jump within a page, link to email and phone numbers, and add images with good alt text, sizes and captions.

By the end of today you can

  • Write attributes in an opening tag
  • Link to other sites and pages with absolute and relative URLs
  • Jump to a section of the same page with an id and a hash link
  • Add images with src, alt, width and height
  • Write useful alt text and caption images with figure

Links are what make the web a web. Today you'll connect pages, and add images that everyone can understand, including people who can't see them.

Mind map · click a dot to fold, drag to pan

Attributes

Sketch
An element with attributes: <a href='https://example.com' target='_blank'>Visit</a>. The element name a comes first. Each attribute is a name, an equals sign and a value in quotes, such as href='https://example.com'. There can be several, separated by spaces. Attributes give extra information or settings; the content Visit is what you see.

Attributes add information to an element. They go in the opening tag as name="value", separated by spaces:

index.html · edit it, the preview updates
Page preview

Some attributes, like reversed, need no value: being there switches them on.

The anchor element, <a>, makes a link. href says where it goes:

index.html · edit it, the preview updates
Page preview

Click the links: the preview shows where each would go, underneath.

Sketch
Absolute and relative links in a small site folder. The folder my-site contains index.html, about.html, a blog folder with first-post.html, and an images folder with logo.png. From index.html, href='about.html' links to the page next to it, href='blog/first-post.html' goes into the blog folder, and from first-post.html, href='../index.html' goes up one folder. An absolute URL like https://example.com/about works from anywhere.
hrefGoes to
https://example.com/aboutAbsolute: a full address, any site
about.htmlRelative: a file in the same folder
blog/first-post.htmlA file inside the blog folder
../index.htmlUp one folder, then index.html
#contactThe element with id="contact" on this page
mailto:… / tel:…Opens email / the phone dialler

target="_blank" opens a new tab. Use it sparingly, since people can choose that themselves. Add rel="noopener" for safety when you do.

Jumping within a page

Give an element an id (unique on the page), then link to it with #:

index.html · edit it, the preview updates
Page preview

Screen reader users often skim a list of just the links. "Click here" means nothing out of context; describe the destination instead:

  • ❌ To read our pricing, click here.
  • ✅ Read our pricing.

(The bold words are the link.)

Images

<img> has no closing tag. src is the image file, alt describes it:

index.html · edit it, the preview updates
Page preview

The last image doesn't exist, so the browser shows its alt text instead. That's one job of alt text; the other is telling people who can't see images what they show.

Writing alt text

Sketch
Choosing alt text. If the image carries information, describe what matters in context: alt='Line chart: sales doubled between 2022 and 2024'. If the image is a link or button, say where it goes or what it does: alt='Home'. If it's purely decorative, like a swirl, use an empty alt: alt=''. Never leave alt out or write image or a file name.
ImageGood alt
A photo in an articleWhat it shows that matters: "Volunteers planting trees in the rain"
A logo that links homeWhere it goes: "Trailhead home"
A chartThe point it makes: "Sales doubled from 2022 to 2024"
Decoration (swirls, dividers)Empty: alt="", so screen readers skip it

Always include width and height too: the browser can then reserve space and the page won't jump as images load.

Captions

<figure> groups an image (or chart, or code) with its caption, <figcaption>:

index.html · edit it, the preview updates
Page preview

Image formats

FormatBest for
JPGPhotos
PNGScreenshots, images with transparency
SVGLogos, icons, diagrams: sharp at any size
WebP / AVIFModern, smaller versions of photos

Put an image inside a link to make it clickable:

index.html · edit it, the preview updates
Page preview

Recap

  • Attributes go in the opening tag: name="value".
  • <a href="…"> links: absolute URLs, relative paths, #id, mailto:, tel:. Write link text that makes sense on its own.
  • <img src alt width height>: always write alt (or alt="" for decoration) and the size.
  • <figure> + <figcaption> caption an image.

Tomorrow: structuring whole pages with semantic elements, and tables.

Knowledge check

Test what you learned

5 questions · pick an answer to see if you're right

0/5
  1. 1

    Which attribute holds a link's destination?

  2. 2

    What's the difference between href="https://example.com/about" and href="about.html"?

  3. 3

    How do you link to the section <h2 id="faq">?

  4. 4

    What should the alt text of a photo of a red bicycle by a lake say?

  5. 5

    Why give an <img> width and height attributes?

Was this lesson helpful?

Need a breather? Take a two-minute break

Keep going