To build a vertical navigation bar, you can style the <a> elements inside the list, in addition to the code from the previous page:
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 widthYou 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:
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 60px;
}
li a {
display: block;
}
Try it Yourself »
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:
ul {
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: #555;
color: white;
}
Try it Yourself »
Add an "active" class to the current link to let the user know which page he/she is on:
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:
ul {
border: 1px solid #555;
}
li {
text-align: center;
border-bottom: 1px solid #555;
}
li:last-child {
border-bottom: none;
}
Try it Yourself »
Create a full-height, "sticky" side navigation:
ul {
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 */
}
Try it Yourself »
Note: This example might not work properly on mobile devices.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!