0
0
HTMLmarkup~15 mins

Linking to sections using IDs in HTML - Deep Dive

Choose your learning style9 modes available
Overview - Linking to sections using IDs
What is it?
Linking to sections using IDs means creating clickable links that jump to specific parts of a webpage. Each section on the page has a unique identifier called an ID. When you click a link with a matching ID, the browser scrolls directly to that section. This helps users navigate long pages easily.
Why it matters
Without linking to sections, users would have to scroll manually to find information, which can be frustrating and time-consuming. Section links improve user experience by making navigation faster and clearer. They also help organize content logically, making websites feel more professional and easier to use.
Where it fits
Before learning this, you should understand basic HTML structure and how links work. After mastering section linking, you can explore advanced navigation techniques like smooth scrolling, table of contents, and accessibility improvements.
Mental Model
Core Idea
An ID is a unique label on a page that links can target to jump directly to that spot.
Think of it like...
It's like putting a bookmark in a book so you can quickly flip to your favorite chapter without searching.
Page structure with IDs:

┌─────────────┐
│ Header      │
├─────────────┤
│ Section 1   │ ← id="section1"
├─────────────┤
│ Section 2   │ ← id="section2"
├─────────────┤
│ Section 3   │ ← id="section3"
└─────────────┘

Link example:
<a href="#section2">Go to Section 2</a>

Clicking the link scrolls the page to the box labeled Section 2.
Build-Up - 7 Steps
1
FoundationWhat is an ID attribute
🤔
Concept: Introduce the ID attribute as a unique name for HTML elements.
In HTML, every element can have an ID attribute. This ID must be unique on the page. For example:
About us content
The ID 'about' labels this section uniquely.
Result
The section now has a unique name 'about' that can be referenced.
Understanding that IDs uniquely identify elements is the first step to linking directly to parts of a page.
2
FoundationCreating basic links with href
🤔
Concept: Explain how the href attribute creates links to other pages or parts of the same page.
Links use the tag with href to point somewhere. For example: Visit site This link goes to another website.
Result
Clicking the link opens the target website.
Knowing how links work is essential before linking inside the same page.
3
IntermediateLinking to an ID on the same page
🤔Before reading on: Do you think adding '#' before an ID in href links to that section or reloads the page? Commit to your answer.
Concept: Show how to link to a section on the same page using '#' plus the ID name.
To link to a section with ID 'about', write: About us Clicking this scrolls the page to the element with id="about".
Result
The page scrolls smoothly or jumps instantly to the 'about' section.
Knowing that '#' plus an ID in href targets that section unlocks internal page navigation.
4
IntermediateUsing IDs on different elements
🤔Before reading on: Can IDs be used on any HTML element or only on headings? Commit to your answer.
Concept: Explain that IDs can be on any element, not just headings or sections.
You can add IDs to paragraphs, divs, images, or any tag:

Welcome text

Go to intro This link jumps to the paragraph with id="intro".
Result
Clicking the link scrolls to the paragraph with the ID.
Understanding that IDs are flexible lets you link to any part of the page, not just big sections.
5
IntermediateCombining links and IDs for navigation menus
🤔
Concept: Show how to build a menu that links to multiple sections using IDs.
Example menu: Sections:
...
...
...
Result
Clicking menu links jumps to each section smoothly.
Using IDs with links creates easy-to-use navigation menus on long pages.
6
AdvancedAccessibility and focus with section linking
🤔Before reading on: Does linking to an ID automatically move keyboard focus to that section? Commit to your answer.
Concept: Explain how linking to IDs affects keyboard users and screen readers, and how to improve accessibility.
When clicking a link to an ID, the browser scrolls but may not move keyboard focus. To help, add tabindex="-1" to the target element:
Contact info
This allows scripts or browsers to focus the section for screen readers.
Result
Keyboard and screen reader users can better follow navigation jumps.
Knowing accessibility needs ensures all users benefit from section linking.
7
ExpertSmooth scrolling and URL hash behavior
🤔Before reading on: Does clicking a link with a hash always cause an instant jump or can it be smooth? Commit to your answer.
Concept: Explore how browsers handle scrolling to IDs and how CSS or JavaScript can make it smooth.
By default, clicking a link with href="#section" jumps instantly. You can add CSS: html { scroll-behavior: smooth; } This makes scrolling smooth and visually pleasant. Also, the URL updates with the hash, allowing bookmarking or sharing links to sections.
Result
Users see smooth scrolling and can share URLs that open at specific sections.
Understanding browser behavior and enhancements lets you create polished navigation experiences.
Under the Hood
When a link with href="#id" is clicked, the browser looks for the element with that ID in the current page. It then scrolls the viewport so that element is visible, usually aligning it at the top. The URL in the address bar updates with the hash (#id) without reloading the page. The browser maintains this state so refreshing keeps the scroll position.
Why designed this way?
This design allows quick navigation inside long documents without loading new pages. It evolved from early web needs to jump to parts of text, like bookmarks in physical books. Using IDs ensures uniqueness and prevents confusion. The hash in URLs is a lightweight way to represent position without server requests.
User clicks link with href="#section2"
        ↓
Browser finds element with id="section2"
        ↓
Browser scrolls viewport to show that element
        ↓
URL updates to include #section2
        ↓
User sees the target section immediately
Myth Busters - 4 Common Misconceptions
Quick: Does clicking a link with href="#id" reload the whole page? Commit yes or no.
Common Belief:Clicking a link with a hash reloads the entire page.
Tap to reveal reality
Reality:Clicking a hash link only scrolls to the element with that ID without reloading the page.
Why it matters:Thinking it reloads causes unnecessary page reloads or complex workarounds, hurting performance and user experience.
Quick: Can two elements share the same ID on a page? Commit yes or no.
Common Belief:Multiple elements can have the same ID to link to the same spot.
Tap to reveal reality
Reality:IDs must be unique per page; duplicates cause unpredictable scrolling behavior.
Why it matters:Duplicate IDs confuse browsers and assistive technologies, breaking navigation and accessibility.
Quick: Does adding an ID automatically make the element visible or styled differently? Commit yes or no.
Common Belief:Adding an ID changes how the element looks or behaves by default.
Tap to reveal reality
Reality:An ID is just a label; it does not affect appearance or visibility unless styled with CSS or targeted by scripts.
Why it matters:Assuming IDs change style leads to confusion when nothing visually changes, causing wasted debugging time.
Quick: Does linking to an ID always move keyboard focus to that section? Commit yes or no.
Common Belief:Clicking a link to an ID automatically moves keyboard focus there.
Tap to reveal reality
Reality:Browsers scroll to the section but do not move keyboard focus by default, which can confuse keyboard and screen reader users.
Why it matters:Ignoring focus management reduces accessibility, making navigation harder for users relying on keyboards or assistive tech.
Expert Zone
1
IDs should be short, descriptive, and use only letters, digits, hyphens, and underscores to avoid issues with CSS selectors and URLs.
2
Linking to IDs interacts with browser history: clicking back returns to the previous scroll position, which can be used for custom navigation flows.
3
Some browsers handle scrolling differently when fixed headers cover the target section; developers often add offset adjustments with JavaScript.
When NOT to use
Avoid using IDs for linking in single-page applications that manage navigation with JavaScript routing; instead, use framework-specific navigation methods. Also, do not rely on IDs for styling only; use classes for that purpose. For dynamic content, IDs must be managed carefully to avoid duplicates.
Production Patterns
In real websites, IDs are used for table of contents, FAQ jump links, and deep linking from external sites. Developers combine IDs with smooth scrolling, focus management, and URL state handling to create accessible, user-friendly navigation. Automated testing often checks that all IDs are unique and links work correctly.
Connections
URL Hash Fragments
Linking to IDs uses URL hash fragments to represent page position.
Understanding URL hashes helps grasp how browsers track and share specific page locations.
Accessibility (a11y) Practices
Section linking affects keyboard navigation and screen reader usability.
Knowing accessibility principles ensures section links serve all users effectively.
Bookmarks in Physical Books
Both are ways to mark and quickly find a specific place in a large document.
Recognizing this connection shows how digital navigation mimics familiar real-world tools.
Common Pitfalls
#1Using the same ID on multiple elements.
Wrong approach:
First intro
Second intro
Correct approach:
First intro
Second intro
Root cause:Misunderstanding that IDs must be unique leads to duplicate IDs causing unpredictable link behavior.
#2Linking to an ID that does not exist on the page.
Wrong approach:Go to missing
Correct approach:Go to existing
Content here
Root cause:Forgetting to add or mistyping the target ID means the link does nothing or scrolls to the top.
#3Expecting the page to scroll smoothly without enabling it.
Wrong approach:Jump
Content
Correct approach:html { scroll-behavior: smooth; } Jump
Content
Root cause:Assuming browsers smooth scroll by default leads to abrupt jumps that can confuse users.
Key Takeaways
IDs are unique labels on HTML elements that let links jump directly to specific page parts.
Using href="#id" in links scrolls the page to the element with that ID without reloading.
IDs can be added to any element, enabling flexible navigation and better user experience.
Accessibility requires managing keyboard focus when linking to sections to support all users.
Enhancing links with smooth scrolling and proper URL hashes creates polished, shareable navigation.