🚀 New batches open: Advanced Excel • Power BI • SQL • AI for Analytics — Book a free demo

Day 3 — Links and images

Links are what make it a web. Images are where most accessibility problems start.

The anchor element

<a href="where it goes">what you click</a>
<p>Read more at <a href="https://dai-academy.com">DAI Academy</a>.</p>
<p><a href="courses.html">Our courses</a></p>
<p><a href="mailto:hello@dai-academy.com">Email us</a></p>
<p><a href="tel:+919818143018">Call us</a></p>

Links in the preview are inert on purpose - this is a sandbox, not a browser you can navigate away in.

Absolute and relative paths

Written asMeans
https://example.com/pageAbsolute - a full address on any site
courses.htmlA file in the same folder
images/logo.pngDown into an images folder
../index.htmlUp one folder, then index.html
/contact.htmlFrom the root of this site, wherever the current page is
The leading slash matters /contact.html always means the site root. contact.html means "next to the page you are on". Mixing these up is the reason links work on your machine and 404 once the site is in folders.

Jumping within a page

Give an element an id, then link to #thatid.

<p><a href="#fees">Skip to fees</a></p>

<h2>About</h2>
<p>Some text about the course.</p>

<h2 id="fees">Fees</h2>
<p>Rupees 18,000 for the full programme.</p>
An id must be unique Use the same id twice and the browser jumps to the first one and ignores the rest. Try it - the Check tab catches duplicate ids and tells you which line the first one was on.

Opening in a new tab, safely

<p>
  <a href="https://example.com" target="_blank" rel="noopener noreferrer">
    Opens in a new tab
  </a>
</p>
target="_blank" without rel="noopener" is a security hole The page you opened gets a handle on yours through window.opener and can redirect it somewhere else while the user is looking at the new tab. Modern browsers mostly patch this now, but write rel="noopener noreferrer" anyway - interviewers ask about it.
Also: do not open internal links in new tabs. It takes the Back button away from people.

Images

<img src="the file" alt="what it shows">
<img src="https://dai-academy.com/assets/logo.png"
     alt="DAI Academy logo"
     width="200">

<p>Above: a logo. The alt text is what a screen reader says,
   and what shows if the image fails to load.</p>

alt text is not optional

Every <img> needs an alt. There are two right answers and one wrong one.

SituationWrite
The image carries meaningalt="Bar chart: sales doubled in Q3"
The image is decorationalt="" - empty, so screen readers skip it
Any situation at allNo alt attribute. This is always wrong.

Delete the alt in the box above and press Run. The Check tab flags it in red.

Describe the point, not the picture alt="image" and alt="photo.jpg" are useless. Ask yourself: if this image vanished, what sentence would I put there instead? That is the alt text.

Sizing, and why the attributes matter

<img src="https://dai-academy.com/assets/logo.png"
     alt="DAI Academy logo" width="300" height="94">

Setting width and height lets the browser reserve the right space before the image arrives, so the text below does not jump down when it loads. That jumping is called layout shift, and Google measures it.

A figure with a caption

<figure>
  <img src="https://dai-academy.com/assets/logo.png"
       alt="DAI Academy logo" width="220">
  <figcaption>The academy logo, used across the site.</figcaption>
</figure>

<figcaption> is a visible caption for everybody. alt is a replacement for people who cannot see the image. They do different jobs, so do not write the same words in both.

Try these yourself

  1. Write three links: one to another site, one to a page in the same folder, one to an email address.
  2. Add an id to a heading and link down to it from the top of the page.
  3. Add an image with no alt, run it, and read the Check tab.
  4. Write good alt text for a photo of a classroom, and for a decorative background swirl.
  5. Explain why rel="noopener" belongs on any link with target="_blank".