Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST

HTML and CSS Links & Navigation


HTML & CSS: Links & Navigation

Links connect your site together.

Combine semantic navigation markup with CSS to create clear, accessible menus.


Basic links

Create hyperlinks with the <a> tag and the href attribute.

Example

<a href="about.html">About us</a>
Try it Yourself »

This anchor uses the href attribute to define the destination.

For external links, include target="_blank" and rel="noopener noreferrer".

If you want to read more about HTML Links or get an in-depth understanding, go to HTML Links in the HTML tutorial.

Note: target="_blank" opens the link in a new tab. Use rel="noopener noreferrer" to prevent the new page from controlling your site and to avoid sending the referrer.


Link attributes

  • href - destination URL.
  • target - where to open (_self, _blank).
  • title - optional tooltip.
  • download - download the linked file.

In other words: An absolute URL starts with https:// and points anywhere on the web. A relative URL points to another file in the same project (like about.html).

Download attribute

<a href="files/guide.pdf" download>Download the guide</a>

If you want to read more or get an in-depth understanding, see Bookmarks and Link Colors in the HTML tutorial.



Navigation menus

Wrap site navigation in a <nav> element.

Use lists for grouping links.

Note: The aria-label attribute gives screen readers a short description of the menu, such as "Primary navigation".

Navigation markup

<nav class="site-nav" aria-label="Primary">
  <ul>
    <li><a href="index.html" class="active">Home</a></li>
    <li><a href="services.html">Services</a></li>
    <li><a href="contact.html">Contact</a></li>
  </ul>
</nav>
Try it Yourself »

This markup wraps the list of links in a semantic <nav> with an aria-label to describe its purpose to assistive technologies.

Learn more: Links, Semantic Elements.


Styling navigation with CSS

Horizontal menu

.site-nav ul {
  display:flex; 
  gap:16px; 
  list-style:none; 
  padding:0; 
  margin:0;
}
.site-nav a {
  display:block; 
  padding:12px 20px; 
  border-radius:999px; 
  text-decoration:none; 
  color:#0f172a; 
  font-weight:600;
}
.site-nav a:hover, .site-nav a.active {
  background:#04AA6D; 
  color:#fff;
}
<nav class="site-nav" aria-label="Primary">...</nav>
Try it Yourself »

The CSS uses flexbox to space items and applies hover/active styles for feedback and clarity.

Explore more designs: CSS Navbar, Horizontal Navbar, Vertical Navbar.


Button-style links

Apply display: inline-block, padding, and background colors to make links look like buttons.


Skip links for accessibility

Give keyboard users a quick path to main content.

Skip link

<a class="skip-link" href="#main">Skip to content</a>
.skip-link {
  position:absolute; 
  left:-9999px; 
  top:auto; 
  width:1px; 
  height:1px; 
  overflow:hidden;
}
.skip-link:focus {
  position:static; 
  width:auto; 
  height:auto; 
  padding:12px 20px; 
  background:#000; 
  color:#fff;
}
Try it Yourself »

The skip link is visually hidden until focused; then it becomes visible so keyboard users can jump directly to #main.

If you want to read more about CSS Accessibility or get an in-depth understanding, go to CSS Accessibility in the CSS tutorial.


Active states and transitions

  • Add .active class to represent the current page.
  • Use :hover, :focus, :focus-visible for better keyboard accessibility.
  • Apply transition for smooth color changes.

Responsive menus

On mobile, convert horizontal menus into vertical stacks or hamburger navigation.

Note: A hamburger menu is the three-line button that expands to show links on small screens.

  • Use media queries to switch flex-direction to column.
  • Hide/show navigation with a menu button and JavaScript for more complex patterns.
  • See Responsive Navbar examples.

Accessible navigation demo

This demo presents a simple site navigation and content area using semantic tags and accessible focus styles.

Syntax highlights: flex-based menu layout, hover/focus feedback, and an active link state.

Example

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body>
  <nav aria-label="Primary navigation">
    <ul>
      <li><a href="#" class="active">Home</a></li>
      <li><a href="#">Docs</a></li>
      <li><a href="#">Blog</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
  <main>
    <h1>Accessible Navigation</h1>
    <p>This menu uses semantic tags, keyboard focus states, and flexbox for layout.</p>
  </main>
</body>
</html>
body {
  font-family:"Source Sans Pro",sans-serif; 
  margin:0; 
  background:#f1f5f9;
}
nav {
  background:#0f172a; 
  padding:12px 24px;
}
nav ul {
  display:flex; 
  gap:12px; 
  list-style:none; 
  margin:0; 
  padding:0;
}
nav a {
  color:#cbd5f5; 
  text-decoration:none; 
  padding:10px 18px; 
  border-radius:8px;
}
nav a:hover, nav a:focus {
  background:#1d4ed8; 
  color:#fff;
}
main {
  max-width:720px; 
  margin:48px auto; 
  padding:0 24px;
}
Try it Yourself »

The nav bar uses spacing and color contrast for readability, while the content region centers text and provides breathing room.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.