Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 6 of 9
Anchor Links & Smooth Scrolling
Anchor links navigate to a specific section within the same page using the # symbol followed by the target element's id. Combined with CSS smooth scrolling, they create a polished user experience.
How Anchor Links Work
An anchor link has two parts:
- 1The link: <a href="#about">Go to About</a>
- 2The target: <section id="about">About</section>
Smooth scrolling: html { scroll-behavior: smooth; }
On-page hash anchors jump directly to sections with matching id attributes
Anchor Links Example
<!DOCTYPE html>
<html>
<head>
<style>
html { scroll-behavior: smooth; }
.toc { background:#f8f9fa; padding:16px; border-left:4px solid #229c6b; margin:16px 0; }
.toc a { display:block; padding:4px 0; color:#229c6b; text-decoration:none; }
section { min-height:300px; padding:20px; margin:10px 0; border:1px solid #eee; }
.back-to-top { position:fixed; bottom:20px; right:20px; background:#229c6b; color:white; padding:10px 16px; border-radius:50%; text-decoration:none; }
</style>
</head>
<body id="top">
<h1>Smooth Scrolling Demo</h1>
<nav class="toc">
<strong>Table of Contents</strong>
<a href="#intro">1. Introduction</a>
<a href="#basics">2. HTML Basics</a>
<a href="#advanced">3. Advanced Topics</a>
</nav>
<section id="intro"><h2>1. Introduction</h2><p>Welcome...</p></section>
<section id="basics"><h2>2. HTML Basics</h2><p>HTML stands for...</p></section>
<section id="advanced"><h2>3. Advanced Topics</h2><p>Let's dive deeper...</p></section>
<a href="#top" class="back-to-top">β</a>
</body>
</html>Quick Quiz
Finished this lesson?