HOW-TO

How to make a navigation bar in HTML

The <nav> markup, CSS that lays the links out in a row, a sticky top bar, highlighting the current page, a phone-friendly version, and why nav links break once the site is online.

4 min read·

Have the file already? Upload it and get a link anyone can open. Free plan, no card.

Get a link free

A navigation bar is a <nav> element holding a list of links, laid out in a row with CSS. Put the markup at the top of the <body>, add the style block to the <head>, and copy the same <nav> into every page of the site.

The HTML

<nav class="topnav"><a class="brand" href="index.html">My Site</a><ul><li><a href="index.html">Home</a></li><li><a href="about.html">About</a></li><li><a href="services.html">Services</a></li><li><a href="contact.html">Contact</a></li></ul></nav>

The <nav> tag tells screen readers and search engines that these are the main links. The <ul> list keeps the links grouped; without CSS it shows as a bulleted list, which is expected.

The CSS that makes it a bar

<style>.topnav{display:flex;align-items:center;justify-content:space-between;gap:16px;padding:12px 20px;background:#1f2937}.topnav a{color:#fff;text-decoration:none}.topnav .brand{font-weight:700}.topnav ul{display:flex;flex-wrap:wrap;gap:20px;list-style:none;margin:0;padding:0}.topnav ul a:hover{text-decoration:underline}.topnav a:focus-visible{outline:2px solid #93c5fd;outline-offset:3px}</style>

display:flex on the <ul> puts the links side by side; list-style:none removes the bullets; justify-content:space-between pushes the site name left and the links right. To center all the links instead, use justify-content:center on .topnav.

Keep it at the top while scrolling

Add position:sticky;top:0;z-index:10 to .topnav. The bar scrolls with the page until it reaches the top, then stays there. Sticky does nothing if a parent element has overflow:hidden, so check that first when it will not stick.

Highlight the current page

On each page, mark its own link: <a href="about.html" aria-current="page">About</a>. Then style it once: .topnav a[aria-current="page"]{border-bottom:2px solid #fff}. aria-current also tells screen readers which page is open.

On phones

  • ✓flex-wrap:wrap on the <ul> (already in the CSS above) lets the links drop to a second line instead of running off the screen. For four or five links that is often enough.
  • ✓Make sure the page has <meta name="viewport" content="width=device-width, initial-scale=1"> in the <head>, or phones shrink the whole page and the links become too small to tap.
  • ✓For a menu button that opens and closes the links, use <details><summary>Menu</summary><ul>…</ul></details>. It opens on tap with no JavaScript. Hide the <summary> on wide screens with a @media (min-width: 700px) rule if you only want it on phones.

Why nav links break once the site is online

  • ✓The links point to files on your computer, such as href="C:/Users/…/about.html" or file:///. Use relative paths like about.html so they work anywhere.
  • ✓The file names do not match. Web servers treat About.html and about.html as different files; keep every file name lowercase.
  • ✓A link starts with /, such as /about.html. That points to the root of the domain, which is not your folder on some hosts. Drop the leading slash.
  • ✓The other pages were never uploaded. A nav bar needs every page it links to in the same upload, see a multi-page site without a build step.

Pages made with AI tools

Claude, ChatGPT and Gemini often build the nav with a framework or a router, so the links work inside the chat preview and break in a downloaded file. Ask for “plain HTML pages with a <nav> of relative links and the CSS in a <style> tag”. More in AI page works in chat but breaks when hosted.

Share the site as a link

With FileShare Pro you upload the pages from the dashboard, copy the link and send it; the nav links work in any browser and the person opening it needs no account. When you change a page, upload it to the same link. Related: how to add a link in HTML and how to add a button in HTML. Sign up free to share your first site.

Frequently asked

How do I make a navigation bar in HTML?

Put a <nav> with a <ul> of links at the top of the <body>, then add CSS: display:flex and list-style:none on the <ul>, a background color on the <nav>, and text-decoration:none on the links.

How do I make a horizontal navigation bar?

Set display:flex on the list that holds the links and add a gap between them. Older tutorials use float:left or display:inline on each <li>; flex does the same with less code.

How do I make the navigation bar stay at the top?

Add position:sticky; top:0; z-index:10 to the <nav>. If it still scrolls away, a parent element probably has overflow:hidden.

Why do my nav links work on my computer but not online?

Usually the links use full file paths from your computer, the file names differ in capital letters, or the other pages were not uploaded. Use lowercase relative links like about.html and upload every page.

Got a page to share?

Drop the file, copy the link — live in seconds, free plan included.

Start sharing free