CSS Navigation Bar (Vertical Menus)
Vertical Navigation Bar
- Home
- News
- Contact
- About
To build a vertical navigation bar, you can style the <a> elements inside the list.
Example
display: block;
width: 60px;
}
Example explained:
display: block;
- Displaying the links as block elements makes the whole link area clickable (not just the text), and it allows us to specify the width (and padding, margin, height, etc. if you want)width: 60px;
- Block elements take up the full width available by default. We want to specify a 60 pixels width
You can also set the width of <ul>, and remove the width of <a>, as they will take up the full width available when displayed as block elements. This will produce the same result as our previous example:
Example
list-style-type: none;
margin: 0;
padding: 0;
width: 60px;
}
li a {
display: block;
}
Vertical Navigation Bar Examples
Create a basic vertical navigation bar with a gray background color and change the background color of the links when the user moves the mouse over them:
Example
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
/* Change the link color on hover */
li a:hover {
background-color: #888;
color: white;
}
Active/Current Navigation Link
Add an "active" class to the current link to let the user know which page he/she is on:
Example
background-color: #0054ff;
color: white;
}
Center Links & Add Borders
Add text-align:center
to <li> or <a> to center the links.
Add the border
property to <ul> add a border around the navbar. If you also want borders inside the navbar, add a border-bottom
to all <li> elements, except for the last one:
Example
border: 1px solid #e6e6e6;
}
li {
text-align: center;
border-bottom: 1px solid #e6e6e6;
}
li:last-child {
border-bottom: none;
}
Full-height Fixed Vertical Navbar
Create a full-height, "sticky" side navigation:
Fixed Full-height Side Nav
Try to scroll this area, and see how the sidenav sticks to the page
Notice that this div element has a left margin of 25%. This is because the side navigation is set to 25% width. If you remove the margin, the sidenav will overlay/sit on top of this div.
Also notice that we have set overflow:auto to sidenav. This will add a scrollbar when the sidenav is too long (for example if it has over 50 links inside of it).
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Example
list-style-type: none;
margin: 0;
padding: 0;
width: 25%;
background-color: #f1f1f1;
height: 100%; /* Full height */
position: fixed; /* Make it stick, even on scroll */
overflow: auto; /* Enable scrolling if the sidenav has too much content */
}
Full Code
<style>
body {
margin: 0;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 25%;
background-color: #f1f1f1;
position: fixed;
height: 100%;
overflow: auto;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
li a.active {
background-color: #0054ff;
color: white;
}
li a:hover:not(.active) {
background-color: #555;
color: white;
}
</style>
</head>
<body>
<ul>
<li><a class="active" href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<div style="margin-left:25%;padding:1px 16px;height:1000px;">
<h2>Fixed Full-height Side Nav</h2>
<h3>Try to scroll this area, and see how the sidenav sticks to the page</h3>
<p>Notice that this div element has a left margin of 25%. This is because the side navigation is set to 25% width. If you remove the margin, the sidenav will overlay/sit on top of this div.</p>
<p>Also notice that we have set overflow:auto to sidenav. This will add a scrollbar when the sidenav is too long (for example if it has over 50 links inside of it).</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
</div>
Note: This example might not work properly on mobile devices.
Comments
Post a Comment