HTML <nav> Element

Publish in HTML Tutorial el 25/05/2025 13:51

The <nav> Element: Navigation for Your Website

The HTML <nav> element represents a section of a page whose purpose is to provide navigation links, either within the current document or to other documents. This is the semantic container for major navigation blocks like menus, tables of contents, or indexes.


Key characteristics of <nav>:

  • Semantic HTML5 element specifically for navigation
  • Typically contains a list of links (but can contain other content)
  • Not necessary for all links - use for major navigation blocks
  • Improves accessibility and SEO when used properly


Basic Syntax

Here's the basic structure of a <nav> element:


Examples of <nav> Usage

Example 1: Basic Horizontal Navigation

A simple horizontal navigation menu with inline styling:

Play Code


Example 2: Vertical Navigation Menu

A vertical navigation menu with list items:

Play Code


Example 3: Navigation with Dropdown (JavaScript)

A navigation menu with JavaScript-powered dropdown functionality:

Play Code


Example 4: Responsive Navigation with JavaScript Toggle

A responsive navigation that collapses on small screens with a toggle button:

Play Code


Example 5: Breadcrumb Navigation

A semantic breadcrumb navigation to show the user's location in the site hierarchy:

Play Code


Best Practices and Tips

  • Semantic Use: Only use <nav> for major navigation blocks, not for all groups of links.
  • Accessibility: Add ARIA labels when you have multiple navigation sections (e.g., aria-label="Main navigation").
  • Mobile First: Design your navigation to work well on mobile devices first.
  • Skip Links: Consider adding "skip to content" links for better accessibility.
  • Current Page: Visually indicate the current page in the navigation.


Common Mistakes to Avoid

  • Using <nav> for every group of links (footer links, social media icons, etc.)
  • Nesting <nav> elements unnecessarily
  • Forgetting to make navigation keyboard accessible
  • Creating navigation that doesn't work without JavaScript
  • Using non-semantic elements like <div> when <nav> would be more appropriate

También te puede interesar

HTML <style> Element
HTML <style> Element

The <style> HTML element contains style information for a document, or part of a document. ...

HTML <strong> Element
HTML <strong> Element

The <strong> element is used to indicate that its content has strong importance, seriousnes...

HTML <span> Element
HTML <span> Element

The <span> element is an inline container used to mark up a part of a text or document. Unl...

HTML <source> Element
HTML <source> Element

The <source> element is used to specify multiple media resources for media elements like &l...

HTML <small> Element
HTML <small> Element

The <small> element is used to represent side-comments and small print, typically for discl...

HTML <section> Element
HTML <section> Element

HTML <section> Element: The Complete Guide The <section> element is a semantic HTML ...