CSS Horizontal Navigation Bar


Horizontal Navigation Bar



There are two ways to create a horizontal navigation bar. Using inline or floating list items.

Inline List Items

One way to build a horizontal navigation bar is to specify the

  • elements as inline, in addition to the "standard" code from the previous page:

    Output :

  • Floating List Items

    Another way of creating a horizontal navigation bar is to float the <li> elements, and specify a layout for the navigation links.

    { float: left; } — This makes each list item float next to the other, creating a horizontal layout.

    { display: block; } — Setting list items to block display allows you to define padding, height, width, margins, and more.

    { padding: 8px; } — Adding padding to each <li> provides space around the content, improving readability and appearance.

    { background-color: #dddddd; } — This sets a light gray background color to each list item for better visual separation.

    Tip: To apply a full-width background color to the navigation bar, apply the background-color to the <ul> element instead of each individual <a> or <li>.

    Output :

    Horizontal Navigation Bar Examples

    Create a basic horizontal navigation bar with a dark background color and change the background color of the links when the user moves the mouse over them:

    Output 3:

    Active/Current Navigation Link

    An "active" class to the current link to let the user know which page he/she is on:

    Output :

    Right-Align Links

    Right-align links by floating the list items to the right (float:right;):

    Output:-:

    Border Dividers

    Add the border-right property to

  • to create link dividers:

    Output:-:

  • Fixed Navigation Bar

    Make the navigation bar stay at the top or the bottom of the page, even when the user scrolls the page:

    Output:-:

    Gray Horizontal Navbar

    An example of a gray horizontal navigation bar with a thin gray border:

    Output:-:

    Sticky Navbar

    Add position: sticky; to to create a sticky navbar.

    A sticky element toggles between relative and fixed, depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).

    Output:-:

    More Examples-Responsive Topnav

    How to use CSS media queries to create responsive top navigation.

    Output:-: