Bird
Raised Fist0
HTMLmarkup~5 mins

Linking to sections using IDs in HTML

Choose your learning style10 modes available
Introduction
Linking to sections using IDs helps users jump directly to important parts of a webpage quickly and easily.
You want to create a table of contents that jumps to different parts of a long article.
You want users to quickly find a specific section on a page without scrolling.
You want to link from one part of a page to another part on the same page.
You want to improve navigation for users using keyboard or screen readers.
You want to share a link that opens a page scrolled to a specific section.
Syntax
HTML
<a href="#sectionID">Link text</a>
...
<section id="sectionID">Content here</section>
The id attribute must be unique on the page.
The href uses a hash (#) followed by the exact id value.
Examples
This link jumps to the 'About Us' section on the same page.
HTML
<a href="#about">About Us</a>
...
<section id="about">
  <h2>About Us</h2>
  <p>Information about our company.</p>
</section>
Multiple links in a navigation menu jump to different sections.
HTML
<nav>
  <a href="#contact">Contact</a>
  <a href="#services">Services</a>
</nav>

<section id="services">
  <h2>Our Services</h2>
</section>

<section id="contact">
  <h2>Contact Us</h2>
</section>
Sample Program
This page has a navigation menu with links that jump to different sections on the same page using IDs. The sections have tabindex="-1" to allow keyboard focus after jumping.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Linking to Sections Example</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      line-height: 1.5;
      padding: 1rem;
    }
    nav {
      background-color: #f0f0f0;
      padding: 1rem;
      margin-bottom: 2rem;
      border-radius: 0.5rem;
    }
    nav a {
      margin-right: 1.5rem;
      color: #0066cc;
      text-decoration: none;
      font-weight: 600;
    }
    nav a:hover, nav a:focus {
      text-decoration: underline;
      outline: none;
    }
    section {
      margin-bottom: 3rem;
    }
    h2 {
      color: #333;
    }
  </style>
</head>
<body>
  <nav aria-label="Main navigation">
    <a href="#intro">Introduction</a>
    <a href="#features">Features</a>
    <a href="#contact">Contact</a>
  </nav>

  <section id="intro" tabindex="-1">
    <h2>Introduction</h2>
    <p>Welcome to our website. Use the links above to jump to different sections.</p>
  </section>

  <section id="features" tabindex="-1">
    <h2>Features</h2>
    <ul>
      <li>Easy navigation</li>
      <li>Fast access to content</li>
      <li>Improved user experience</li>
    </ul>
  </section>

  <section id="contact" tabindex="-1">
    <h2>Contact</h2>
    <p>You can reach us at <a href="mailto:info@example.com">info@example.com</a>.</p>
  </section>
</body>
</html>
OutputSuccess
Important Notes
Use meaningful and unique IDs for each section to avoid confusion.
Adding tabindex="-1" to sections helps keyboard users focus on the section after jumping.
Test your links by clicking them and watching the page scroll smoothly to the right spot.
Summary
Use the id attribute to mark sections you want to link to.
Use <a href="#id"> to create links that jump to those sections.
This improves navigation and user experience on longer pages.

Practice

(1/5)
1. What is the purpose of the id attribute in HTML when linking to sections?
easy
A. It hides the section from the page.
B. It styles the section with a unique color automatically.
C. It marks a section so you can link directly to it using an anchor.
D. It creates a new page in the browser.

Solution

  1. Step 1: Understand the role of the id attribute

    The id attribute uniquely identifies an element on the page.
  2. Step 2: Linking to sections using id

    Links with href="#id" jump directly to the element with that id.
  3. Final Answer:

    It marks a section so you can link directly to it using an anchor. -> Option C
  4. Quick Check:

    id marks section for linking [OK]
Hint: Use id to name sections for direct links [OK]
Common Mistakes:
  • Thinking id changes style automatically
  • Confusing id with class
  • Believing id creates new pages
2. Which of the following is the correct syntax to link to a section with id="contact"?
easy
A. <a href="#contact">Contact</a>
B. <a link="#contact">Contact</a>
C. <a id="contact">Contact</a>
D. <a href="contact">Contact</a>

Solution

  1. Step 1: Identify how to link to an id

    Links to sections use href="#id" where id is the target's identifier.
  2. Step 2: Check each option's syntax

    <a href="#contact">Contact</a> uses href="#contact", which is correct. Others miss the # or use wrong attributes.
  3. Final Answer:

    <a href="#contact">Contact</a> -> Option A
  4. Quick Check:

    Link uses href="#id" [OK]
Hint: Use # before id in href to link sections [OK]
Common Mistakes:
  • Omitting the # in href
  • Using id attribute inside <a> instead of href
  • Using non-standard attributes like link
3. Given this HTML snippet, what happens when the user clicks the link?
<a href="#about">About Us</a>
...
<section id="about">
  <h2>About Our Company</h2>
  <p>We build websites.</p>
</section>
medium
A. Nothing happens because the link is broken.
B. The page scrolls to the section with heading 'About Our Company'.
C. The browser opens a new tab with the 'about' page.
D. The page reloads to the top.

Solution

  1. Step 1: Understand the link's href attribute

    The link uses href="#about", which targets the element with id="about".
  2. Step 2: Describe browser behavior on clicking such a link

    The browser scrolls smoothly to the element with that id, showing the 'About Our Company' section.
  3. Final Answer:

    The page scrolls to the section with heading 'About Our Company'. -> Option B
  4. Quick Check:

    href="#id" scrolls to that section [OK]
Hint: Clicking href="#id" scrolls to that section [OK]
Common Mistakes:
  • Thinking it opens a new page
  • Assuming the link does nothing
  • Believing it reloads the page
4. What is wrong with this code if the link does not jump to the section?
<a href="#services">Our Services</a>
...
<section id="service">
  <h2>Services Offered</h2>
</section>
medium
A. The href attribute should not have a #.
B. The link text must match the section heading exactly.
C. The section tag cannot have an id.
D. The id in the section does not match the href value.

Solution

  1. Step 1: Compare href and id values

    The link uses href="#services" but the section has id="service" (missing 's').
  2. Step 2: Understand matching requirement

    For the link to work, the href must exactly match the target element's id.
  3. Final Answer:

    The id in the section does not match the href value. -> Option D
  4. Quick Check:

    href and id must match exactly [OK]
Hint: Check href and id spellings match exactly [OK]
Common Mistakes:
  • Forgetting the # in href
  • Using wrong or mismatched id names
  • Thinking link text affects the jump
5. You want to create a navigation menu that links to three sections: Home, About, and Contact. Which HTML snippet correctly sets up the links and sections using IDs?
hard
A.
<nav>
  <a href="#home">Home</a>
  <a href="#about">About</a>
  <a href="#contact">Contact</a>
</nav>

<section id="home">Welcome!</section>
<section id="about">About us info</section>
<section id="contact">Contact details</section>
B.
<nav>
  <a href="home">Home</a>
  <a href="about">About</a>
  <a href="contact">Contact</a>
</nav>

<section id="home">Welcome!</section>
<section id="about">About us info</section>
<section id="contact">Contact details</section>
C.
<nav>
  <a href="#home">Home</a>
  <a href="#about">About</a>
  <a href="#contact">Contact</a>
</nav>

<section id="home-section">Welcome!</section>
<section id="about-section">About us info</section>
<section id="contact-section">Contact details</section>
D.
<nav>
  <a href="#home">Home</a>
  <a href="#about">About</a>
  <a href="#contact">Contact</a>
</nav>

<section id="home">Welcome!</section>
<section id="about">About us info</section>
<section>Contact details</section>

Solution

  1. Step 1: Check href values in navigation links

    Links use href="#home", href="#about", and href="#contact" which is correct syntax to link to IDs.
  2. Step 2: Verify matching section IDs

    Sections have matching id="home", id="about", and id="contact" exactly matching the href targets.
  3. Final Answer:

    Option A correctly links navigation anchors to matching section IDs. -> Option A
  4. Quick Check:

    Matching href="#id" and section id [OK]
Hint: Match href="#id" exactly with section id [OK]
Common Mistakes:
  • Omitting # in href
  • Mismatching href and id names
  • Missing id attribute on sections
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard