--- url: "https://web.dev/articles/website-navigation" titulek: "Building the main navigation for a website | Articles | web.dev" h1: "Building the main navigation for a website Stay organized with collections Save and categorize content based on your preferences." popis: "This tutorial describes how to build an accessible main navigation of a website. You learn about semantic HTML, accessibility, and how using ARIA attributes can sometimes do more harm than good." jazyk: "en" kanonicka: "https://web.dev/articles/website-navigation" drobecky: ["Articles", "Building the main navigation for a website"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 02:41:03" znaku: 27633 slov: 4340 kodovani: "iso-8859-2" sha256: "49dd604144c20e22c17f62272a6f9ff16db94d7811adf09b2ccf665067b3d45f" --- Building the main navigation for a website Stay organized with collections Save and categorize content based on your preferences. This tutorial describes how to build an accessible main navigation of a website. You learn about semantic HTML, accessibility, and how using ARIA attributes can sometimes do more harm than good. Manuel Matuzović There are many different ways of building the main navigation of a website, in terms of styling, functionality, and the underlying markup and semantic information. If the implementation is too minimalist, it works for most people, but the user experience (UX) might not be great. If it's over-engineered, it might confuse users or even hinder them from being able to access it at all. For most websites, you want to build something that's neither too simple, nor too complicated. Building layer by layer In this tutorial you start with a basic setup and add features layer by layer up to a point where you provide just enough information, styling, and functionality to please most users. To achieve that you make use of the progressive enhancement principle, which states that you start with the most fundamental and robust solution and progressively add layers of functionality. If one layer fails to work for some reason, the navigation will still work because it gracefully falls back to the underlying layer. Basic structure For a basic navigation you need two things: `` elements and a few lines of CSS to improve the default styling and layout of your links. HTML `Home About us Pricing Contact` CSS `/* Define variables for your colors */ :root { --color-shades-dark: rgb(25, 25, 25); } /* Use the alternative box model Details: */ *{ box-sizing: border-box; } /* Basic font styling */ body { font-family: Segoe UI, system-ui, -apple-system, sans-serif; font-size: 1.6rem; } /* Link styling */ a { --text-color: var(--color-shades-dark); border-block-end: 3px solid var(--border-color, transparent); color: var(--text-color); display: inline-block; margin-block-end: 0.5rem; /* See note at the bottom of this chapter */ margin-inline-end: 0.5rem; padding: 0.1rem; text-decoration: none; } /* Change the border-color on :hover and :focus */ a:where(:hover, :focus) { --border-color: var(--text-color); }` View [Step 1: Basic HTML and CSS" on CodePen](https://codepen.io/web-dot-dev/pen/wvjvJrw). This works well for most users, no matter how they're accessing the site. The navigation is accessible with a mouse, a keyboard, a touch device, or a screen reader, but there's room for improvement. You can enhance the experience by extending this basic pattern with additional functionality and information. Here's what you can do: Highlight the active page. Announce the number of items to screen reader users. Add a landmark and allow screen reader users to access the navigation directly using a shortcut. Hide the navigation on narrow viewports. Improve focus styling. Highlight the active page To highlight the active page, you can add a class to the corresponding link. `About us` The issue with this approach is that it conveys the information which link is active purely visually. A blind screen reader user couldn't tell the difference between the active page and other pages. Fortunately, the [Accessible Rich Internet Applications (ARIA)](https://www.w3.org/TR/wai-aria/) standard offers a way to communicate this information semantically, as well. Use the [aria-current="page"](https://developer.mozilla.org/docs/Web/Accessibility/ARIA/Attributes/aria-current) attribute and value instead of a class. `aria-current` (state) indicates the element that represents the current item within a container or set of related elements. A page token used to indicate a link within a set of pagination links, where the link is visually styled to represent the currently-displayed page. [Accessible Rich Internet Applications (WAI-ARIA) 1.1](https://www.w3.org/TR/wai-aria/#aria-current) With the additional attribute, a screen reader now announces something like "current page, link, About Us" instead of just "link, About Us". `About us` A convenient side effect is that you can use the attribute to select the active link in CSS, making the `active-page` class obsolete. `Home About us Pricing Contact``/* Change border-color and color for the active page */ [aria-current="page"] { --border-color: var(--color-highlight); --text-color: var(--color-highlight); }` View [Step 2: Highlight the active page on CodePen](https://codepen.io/web-dot-dev/pen/OJZJpOe). Announce the number of items By looking at the navigation, sighted users can tell that it only contains four links. A blind screen reader user can't obtain this information as quickly. They may have to work their way through the whole list of links. This might not be an issue if the list is short like in this example, but if it contains 40 links this task can be cumbersome. If a screen reader user knows upfront that the navigation contains a lot of links, they might decide to use a different, more efficient way of navigation, like the site search. A nice way of communicating the number of items upfront, is wrapping each link in a list item (`
  • `), nested in an unordered list (`