Skip to main content

CSS Vertical Navigation Bar

A vertical navigation bar stacks its links on top of each other, and it usually runs along the left or right side of a webpage.

Its structure is simple: an unordered list (<ul>) whose list items (<li>) each contain one link (<a>).

Example

A simple starting version: gray background color, with hover effects on the links.

Prompt
Create a basic vertical navigation bar using an unordered list with four links: Home, News, Contact, About. Style it with width 200px, light gray background (#f1f1f1), no list bullets, no margin/padding. Links should be block-level, black text, 8px 16px padding, no underline. On hover, change background to #555555 and text to white.
Output: complete HTML page

Active State

So users can tell which page they are on, highlight the matching link with an "active" class.

Example

Prompt
Create a vertical navigation bar with an active state. Use the same basic vertical navbar styling (width 200px, gray background, block links). Add an "active" class on the Home link with background-color #04AA6D and white text. Include hover effect on non-active links.
Output: complete HTML page

Center Navbar Links & Add Borders

Centering the links takes text-align: center on <li> or <a>. For a border around the whole navbar, put border on <ul>. Each <li> except the last one then gets a border-bottom.

Example

Prompt
Create a vertical navigation bar with centered links and borders. Add a 1px solid #555555 border around the ul, and a bottom border on each li except the last one (using li:last-child). Center the text with text-align: center. Use the same basic vertical navbar styling.
Output: complete HTML page

Full-height Vertical Navbar

This side navigation takes the full height and is "sticky": it does not move, even when the user scrolls.

Example

Prompt
Create a full-height fixed vertical navigation bar. The ul should have width 130px, light gray background, height 100%, position fixed, and overflow auto. Include links: Home, News, Contact, About. Add some body content to demonstrate the fixed sidebar.
Output: complete HTML page

Responsive Navbar

CSS media queries make the navbar responsive, turning it from vertical into horizontal once the screen is wider.

Example

Prompt
Create a responsive vertical navigation bar that becomes horizontal on wider screens using CSS media queries. At narrow widths the links stack vertically; at wider widths (e.g., 600px+) they display horizontally using float or flexbox.
Output: complete HTML page