bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/CSS/CSS Foundations
CSS•CSS Foundations

CSS Horizontal Navigation Bar

In a horizontal navigation bar, the navigation links are stacked horizontally (next to each other), and is typically aligned on the top of a webpage.

The basics of a horizontal navigation bar is an unordered list (<ul>), with list items (<li>), each holding a link (<a>), as shown in the Navbar Intro page.

It is also common to add a <nav> element around the <ul> element, that will serve as a container for your navigation bar.

Horizontal Navbar Using Float

One way of creating a horizontal navigation bar is to add the float property to the <li> elements.

Here we 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:

  • Home
  • News
  • Contact
  • About

Example

Formatted code
ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #333333;
}
ul li {
  float: left;
}
ul li a {
  display: block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}
ul li a:hover {
  background-color: #111111;
}

Live preview

Example explained

  • overflow: hidden; - Prevents list items from going outside of the list
  • background-color: #333333; - Adds a black background-color to the <ul> element
  • float: left; - Makes <li> elements float next to each other
  • display: block; - Allows us to specify padding, height, width, and margins to <a>
  • padding: 14px 16px; - Add some padding between each <a> element
  • text-decoration: none; - Removes underline from the links

Horizontal Navbar Using Flex

A more modern way of creating a horizontal navigation bar, is to use CSS flexbox .

The following example creates an equal-looking horizontal navbar as above, but with flexbox:

Example

Formatted code
ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  background-color: #333333;
  display: flex;
}
ul li a {
  display: block;
  color: white;
  padding: 14px
  16px;
  text-decoration: none;
}
ul li a:hover {
  background-color: #111111;
}

Live preview

The solution here are display: flex; . This creates a flex context and by default the items are shown from left to right.

And just by adding one line to the ul block: justify-content: center; , you will have a horizontally centered navbar:

Example

Formatted code
ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  background-color: #333333;
  display: flex;
  justify-content: center;
}

Live preview

Active State

Here, we add an "active" class to highlight the link corresponding to the current page to let the user know which page/section he/she is on:

  • Home
  • News
  • Contact
  • About

Example

Formatted code
ul li a.active {
  background-color: #04AA6D;
}

Live preview

Gray Horizontal Navbar

Here is an example of a gray horizontal navigation bar with a thin, gray border:

  • Home
  • News
  • Contact
  • About

Example

Formatted code
ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  display: flex;
  border: 1px solid #e7e7e7;
  background-color: #f3f3f3;
}

Live preview

Right-align Last Link

You can right-align one or more links by floating the list items to the right ( float: right; ):

  • Home
  • News
  • Contact
  • About
<ul> <li><a href="#home" class="active">Home</a></li>
<li><a href="#news">News</a></li> <li><a href="#contact">Contact</a></li>
<li style="float:right"><a href="#about">About</a></li> </ul>

Border Dividers

Add the border-right property to <li> elements (to all <li>s, except the last), to create link dividers:

  • Home
  • News
  • Contact
  • About

Example

Formatted code
/* Add a lightgray right border to all list items, except the last */
ul li {
  float: left;
  border-right: 1px solid #bbbbbb;
}
ul
li:last-child {
  border-right: none;
}

Live preview

Fixed Navigation Bar

You can make the navigation bar to stay fixed at the top or at the bottom of a page, by using the position property along with top or bottom :

Fixed Top

Formatted code
ul {
  position: fixed;
  top: 0;
  width: 100%;
}

Live preview

Fixed Bottom

Formatted code
ul {
  position: fixed;
  bottom: 0;
  width: 100%;
}

Live preview

Note

Fixed position might not work properly on mobile devices.

Sticky Navigation Bar

Add position: sticky; to <ul> to create a sticky navbar.

An element with position: sticky; toggles between a relative and fixed position, depending on the scroll position.

A sticky element is positioned relative until a certain scroll position is reached - then it "sticks" in that place (like position: fixed).

Example

Formatted code
ul {
  position: sticky;
  top: 0;
}

Live preview

Note

You must specify at least one of the top , right , bottom or left properties, for sticky positioning to work.

Dropdown Menu in Navigation Bar

Here we create a dropdown menu for one of the menu links:

Responsive Navbar

Example

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

  • no credit card required

Previous

CSS Vertical Navigation Bar

Next

CSS Dropdowns

This chapter

Overview
108

Lessons

263m

Read time

1. CSS Tutorial2. CSS Introduction3. CSS Syntax4. CSS Selectors5. CSS Grouping Selectors6. How To Add CSS7. How To Add Internal CSS8. How To Add Inline CSS9. Multiple Style Sheets & Cascading Order10. CSS Comments11. CSS Errors12. CSS Colors13. CSS RGB Colors14. CSS HEX Colors15. CSS HSL Colors16. CSS Backgrounds17. CSS Background Image18. CSS Background Image Repeat19. CSS Background Attachment20. CSS Background Shorthand21. CSS Borders22. CSS Border Width23. CSS Border Color24. CSS Shorthand Border Property25. CSS Rounded Borders26. CSS Margins27. CSS Margin Collapse28. CSS Padding29. CSS Padding and box-sizing30. CSS Height and Width31. CSS Min/Max Height and Width32. CSS Box Model33. CSS Outline34. CSS Outline Width35. CSS Outline Color36. CSS Outline Shorthand37. CSS Outline Offset38. CSS Text Color39. CSS Text Alignment40. CSS Text Decoration41. CSS Text Decoration Styles42. CSS Text Transformation43. CSS Text Spacing44. CSS Text Shadow45. CSS Web Safe Fonts46. CSS Font Fallbacks47. CSS Font Style48. CSS Font Size49. CSS Google Fonts50. CSS Great Font Pairings51. CSS Font Property52. CSS Icons - Font Awesome53. CSS Icons - Bootstrap54. CSS Icons - Google55. CSS Links56. CSS Link Buttons57. CSS Lists58. CSS Tables59. CSS Table Size (Width and Height)60. CSS Table Alignment61. CSS Table Styling62. CSS Responsive Tables63. CSS The display Property64. CSS Hide Elements65. CSS The max-width Property66. CSS The position Property67. CSS Fixed and Absolute Positioning68. CSS Sticky Positioning69. CSS Position Offsets70. CSS The z-index Property71. CSS The overflow Property72. CSS overflow-x and overflow-y73. CSS Float74. CSS clear and Clearfix Hack75. CSS display: inline-block76. CSS Center Align77. CSS Horizontal Align78. CSS Vertical Align79. CSS Combinators80. CSS Pseudo-classes81. CSS Interactive Pseudo-classes82. CSS Structural Pseudo-classes83. CSS Pseudo-elements84. CSS Pseudo-elements for Text85. CSS Pseudo-elements for Content86. CSS Opacity87. CSS Navigation Bars88. CSS Vertical Navigation Bar89. CSS Horizontal Navigation Bar90. CSS Dropdowns91. CSS Advanced Dropdowns92. CSS Image Gallery93. CSS Image Sprites94. CSS Attribute Selectors95. CSS Advanced Attribute Selectors96. CSS Forms97. CSS Form Inputs98. CSS Form Focus and Icons99. CSS Form Elements100. CSS Counters101. CSS Units102. CSS Absolute Units103. CSS Relative Units104. CSS Inheritance105. CSS Specificity106. CSS Specificity Hierarchy107. CSS The !important Rule108. CSS Math Functions

On this page

Horizontal Navbar Using FloatHorizontal Navbar Using FlexActive StateGray Horizontal NavbarRight-align Last LinkBorder DividersFixed Navigation BarSticky Navigation BarDropdown Menu in Navigation BarResponsive Navbar