CodeGym /Courses /Frontend SELF EN /Navigation Tags

Navigation Tags

Frontend SELF EN
Level 10 , Lesson 2
Available

2.1 Anchor Links

Navigation within an HTML page helps users quickly jump to different sections of a document. It's super handy for long pages with lots of content. In HTML, there are several ways to implement such navigation, including using anchor links, the id attribute, and JavaScript navigation.

Anchor links are the most common way to navigate within a page. They allow users to jump to specific parts of the document using anchor tags.

Usage example:

Creating anchors with the id attribute:

HTML
    
      <h2 id="section1">Section 1</h2>
      <p>Content of section 1...</p>
      <h2 id="section2">Section 2</h2>
      <p>Content of section 2...</p>
      <h2 id="section3">Section 3</h2>
      <p>Content of section 3...</p>
    
  

Creating links to anchors:

HTML
    
      <nav>
        <ul>
          <li><a href="#section1">Go to Section 1</a></li>
          <li><a href="#section2">Go to Section 2</a></li>
          <li><a href="#section3">Go to Section 3</a></li>
        </ul>
      </nav>
    
  
Important!

When a user clicks on a link, the browser automatically scrolls to the element with the specified id. The page doesn't reload.

Navigation using the name attribute

The name attribute can also be used to create anchors, although it's less common and considered outdated.

Example

HTML
    
      <a name="top"></a>
      <p>Top of the page...</p>
      <a name="bottom"></a>
      <p>Bottom of the page...</p>
      <nav>
        <ul>
          <li><a href="#top">Go to Top</a></li>
          <li><a href="#bottom">Go to Bottom</a></li>
        </ul>
      </nav>
    
  

2.2 Scrolling with JavaScript

JavaScript offers more flexible ways to navigate within a page. With methods like scrollIntoView, scrollTo, and others, you can implement smooth scrolling to different elements on a page.

Usage example:

This code smoothly scrolls the page to the specified element when a button is clicked.

HTML
    
      <button onclick="scrollToSection('section1')">Go to Section 1</button>
      <button onclick="scrollToSection('section2')">Go to Section 2</button>
      <button onclick="scrollToSection('section3')">Go to Section 3</button>

      <h2 id="section1">Section 1</h2>
      <p>Content of section 1...</p>
      <h2 id="section2">Section 2</h2>
      <p>Content of section 2...</p>
      <h2 id="section3">Section 3</h2>
      <p>Content of section 3...</p>
    
  
JavaScript
    
     // Function to smoothly scroll to a specific section on the page
      function scrollToSection(sectionId) {
        // Locate the element by its ID
        document.getElementById(sectionId).scrollIntoView({ behavior: 'smooth' });
      }
    
  

2.3 The <nav> Tag

The <nav> tag in HTML5 is used for defining a section of a page that contains navigation links. This tag helps semantically mark navigation blocks, improving the web page's structure and making it more accessible to both users and search engines.

The <nav> tag is used to group links intended for navigating a site or within the current document. These can be main menus, secondary menus, breadcrumbs, page lists, and other navigation elements.

Syntax

HTML
    
      <nav>
        <!-- Navigation elements -->
      </nav>
    
  

Usage example

HTML
    
      <header>
        <h1>My Website</h1>
        <nav>
          <ul>
            <li><a href="#home">Home</a></li>
            <li><a href="#about">About Us</a></li>
            <li><a href="#services">Services</a></li>
            <li><a href="#contact">Contact</a></li>
          </ul>
        </nav>
      </header>
    
  

Explanation

  • The <header> tag: used to create a page header that includes the navigation menu
  • The <nav> tag: contains a list of links that lead to different sections of the page
  • The <ul> and <li> tags: used to create a list of navigation links
  • The <a> tag: defines the actual links that users can click to move to other parts of the page or site

2.4 Using the <nav> Tag

The <nav> tag is used for grouping navigation links, such as:

  • Main site menu
  • Secondary menus
  • Breadcrumbs
  • Links to pagination pages
  • Other navigation links

Main Site Menu

HTML
    
      <nav>
        <ul>
          <li><a href="#home">Home</a></li>
          <li><a href="#about">About Us</a></li>
          <li><a href="#services">Services</a></li>
          <li><a href="#contact">Contact</a></li>
        </ul>
      </nav>
    
  

Secondary Menu

HTML
    
      <nav>
        <ul>
          <li><a href="#faq">FAQ</a></li>
          <li><a href="#support">Support</a></li>
          <li><a href="#privacy">Privacy Policy</a></li>
        </ul>
      </nav>
    
  

Breadcrumbs

HTML
    
      <nav aria-label="breadcrumb">
        <ol>
          <li><a href="/home">Home</a></li>
          <li><a href="/category">Category</a></li>
          <li>Current Page</li>
        </ol>
      </nav>
    
  

Benefits of Using the <nav> Tag

Semantic Markup

Using the <nav> tag helps search engines and assistive technologies better understand the page structure. It makes the site more accessible and improves SEO.

Improved Page Structure

The <nav> tag clearly separates navigation elements from the main content, making the page markup more logical and structured.

Easy Styling

Navigation elements wrapped in the <nav> tag are easy to style with CSS to enhance appearance and usability.

1
Task
Frontend SELF EN, level 10, lesson 2
Locked
Anchor Links
Anchor Links
1
Task
Frontend SELF EN, level 10, lesson 2
Locked
Website Menu
Website Menu
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION