Open In App

How to create a Fixed Navigation Bar in CSS ?

Last Updated : 31 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Creating a fixed navigation bar in CSS involves using the position property to fix the navigation bar at the top of the viewport. This is commonly done for a better user experience, where the navigation remains visible as the user scrolls down the page.

Syntax:

/* Style for the navigation bar */
.navbar {
background-color: #333;
padding: 10px;
color: white;
position: fixed;
top: 0;
width: 100%;
z-index: 1000;
/* Optional: Adjust z-index to control the stacking order */
}

Features:

  • position: fixed; Fixes the position of the navigation bar relative to the viewport.
  • top: 0;: Positions the fixed element at the top of the viewport.
  • width: 100%; Makes the navigation bar span the entire width of the viewport.
  • z-index: 1000; (Optional) Adjusts the stacking order to control visibility, especially when overlapping with other elements.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads