css制作右侧导航条
固定定位法
使用 position: fixed 将导航条固定在视口右侧,适合长页面滚动时保持可见。
.right-nav {
position: fixed;
top: 50%;
right: 20px;
transform: translateY(-50%);
width: 200px;
background: #f5f5f5;
border-radius: 8px;
padding: 15px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
浮动布局法
通过 float: right 实现导航条与内容并排,需确保父容器有足够宽度。

.right-nav {
float: right;
width: 25%;
margin-left: 20px;
background: #fff;
border: 1px solid #ddd;
padding: 10px;
}
Flexbox 对齐法
在 Flex 容器中通过 margin-left: auto 将导航条推到右侧。

.container {
display: flex;
}
.content {
flex: 1;
}
.right-nav {
margin-left: auto;
width: 250px;
background: #e9e9e9;
padding: 15px;
}
响应式设计适配
添加媒体查询控制小屏幕下的导航条显示方式。
@media (max-width: 768px) {
.right-nav {
position: static;
width: 100%;
margin-top: 20px;
}
}
滚动监听高亮
结合 JavaScript 实现滚动时高亮当前章节链接。
.right-nav a.active {
color: #ff5722;
font-weight: bold;
border-left: 3px solid #ff5722;
padding-left: 10px;
}






