Learn HTML and CSS: Links
Links
Every day, tourists ask Maggie the same things: which flavours are there, and how do I get here by train? With links, they find the answers themselves.
The a element
You make a link with <a>, short for "anchor". The text between the tags is what the visitor clicks on.
Where the link goes is in the attribute href: an extra bit of information in the opening tag. An attribute has a name, an = and a value in quotes: <a href="flavours.html">our flavours</a>.
Visitors don't see the address, only the underlined text. So choose text that says where they'll end up, not "click here".
<main> <p>Wondering what's in the display case today? Have a look at <a href="flavours.html">our flavours</a>.</p></main>Absolute and relative
An absolute link is a full address, with https:// in front: https://en.wikipedia.org/wiki/Brighton. It works from anywhere, including to another website.
A relative link is a path from the page you're on: flavours.html is a file in the same folder. Ideal for your own site: if the site moves to another address, the links still work.
Forget https:// on another website's address, and the browser thinks it's a file on your own site.
<main> <h2>Explore</h2> <p><a href="flavours.html">Our flavours</a></p> <p><a href="contact.html">Contact</a></p> <p><a href="https://en.wikipedia.org/wiki/Brighton">All about Brighton</a></p></main>Try it yourself
Make Our flavours a link to flavours.html on Maggie's own site, and All about Brighton a link to https://en.wikipedia.org/wiki/Brighton.
<main> <h2>Want to know more?</h2> <p>Our flavours</p> <p>All about Brighton</p></main>body { margin: 0; font-family: system-ui, sans-serif; background: #fff8f0; color: #3b2a1a;}header { padding: 24px 16px; background: #ffd6e0;}.logo { display: inline-block; margin: 8px 0 0; padding: 6px 18px; border-radius: 999px; background: #ffffff; font-size: 28px;}.slogan { margin: 8px 0 0;}main { padding: 8px 16px 24px;}Ready for more?
The full lesson has 5 exercises, from easy to challenging, with hints, points and a streak you can build.
Start the lesson