Day 3 — Links and images
Links are what make it a web. Images are where most accessibility problems start.
The anchor element
<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 as | Means |
|---|---|
https://example.com/page | Absolute - a full address on any site |
courses.html | A file in the same folder |
images/logo.png | Down into an images folder |
../index.html | Up one folder, then index.html |
/contact.html | From the root of this site, wherever the current page is |
/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>
Opening in a new tab, safely
<p>
<a href="https://example.com" target="_blank" rel="noopener noreferrer">
Opens in a new tab
</a>
</p>
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="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.
| Situation | Write |
|---|---|
| The image carries meaning | alt="Bar chart: sales doubled in Q3" |
| The image is decoration | alt="" - empty, so screen readers skip it |
| Any situation at all | No alt attribute. This is always wrong. |
Delete the alt in the box above and press Run. The Check tab flags it in red.
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
- Write three links: one to another site, one to a page in the same folder, one to an email address.
- Add an id to a heading and link down to it from the top of the page.
- Add an image with no alt, run it, and read the Check tab.
- Write good alt text for a photo of a classroom, and for a decorative background swirl.
- Explain why rel="noopener" belongs on any link with target="_blank".
