📖 Scenario: You are creating a simple webpage for a small business. The page has multiple sections like Home, About, and Contact. You want visitors to jump quickly to these sections by clicking links at the top.
🎯 Goal: Build a webpage with a navigation menu at the top. Each menu item links to a specific section on the same page using IDs. When a visitor clicks a menu link, the page scrolls to that section.
📋 What You'll Learn
Create three sections with headings: Home, About, Contact
Assign unique IDs to each section
Create a navigation menu with links that use the IDs to jump to each section
Use semantic HTML elements for structure
💡 Why This Matters
🌍 Real World
Websites often have navigation menus that let users jump to different parts of the page quickly. Using IDs and links is a simple way to do this.
💼 Career
Knowing how to link to sections using IDs is a basic but essential skill for front-end web developers and content creators to improve user experience and accessibility.
Progress0 / 4 steps
1
Create the basic HTML structure with sections
Write the basic HTML5 skeleton with a <main> element containing three <section> elements. Each section should have a heading: Home, About, and Contact. Do not add IDs yet.
HTML
Hint
Use <main> to wrap the main content. Inside it, add three <section> elements each with an <h2> heading for Home, About, and Contact.
2
Add unique IDs to each section
Add the IDs home, about, and contact to the <section> elements for Home, About, and Contact respectively.
HTML
Hint
Use the id attribute inside each <section> tag. For example, <section id="home">.
3
Create a navigation menu with links to sections
Add a <nav> element above the <main>. Inside it, create an unordered list <ul> with three list items <li>. Each list item should have a link <a> that uses the href attribute to link to the IDs #home, #about, and #contact with link text Home, About, and Contact respectively.
HTML
Hint
Use <nav> for navigation. Inside it, add <ul> with <li> items. Each <a> tag's href should start with # followed by the section ID.
4
Add accessibility features and finalize structure
Add an aria-label="Primary navigation" attribute to the <nav> element for accessibility. Also, add a <header> element wrapping the <nav> to improve semantic structure.
HTML
Hint
Wrap the <nav> inside a <header> tag. Add aria-label="Primary navigation" to the <nav> for screen readers.
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
Step 1: Understand the role of the id attribute
The id attribute uniquely identifies an element on the page.
Step 2: Linking to sections using id
Links with href="#id" jump directly to the element with that id.
Final Answer:
It marks a section so you can link directly to it using an anchor. -> Option C
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
Step 1: Identify how to link to an id
Links to sections use href="#id" where id is the target's identifier.
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.
Final Answer:
<a href="#contact">Contact</a> -> Option A
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?
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
Step 1: Compare href and id values
The link uses href="#services" but the section has id="service" (missing 's').
Step 2: Understand matching requirement
For the link to work, the href must exactly match the target element's id.
Final Answer:
The id in the section does not match the href value. -> Option D
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?