00:00:00
侧边栏样式美化
✅侧边栏样式美化
2025年8月17日更新。
- 版权
环境:在Teek@1.4.2-2025.8.17
测试成功。
- 背景
默认的侧边栏不太容易区分到底是目录还是文件,我们可以进行美化。
- 配置
(1)在 .vitepress/theme/style
目录新建一个 sidebarIcon.css
文件
.
├─ docs
│ ├─ .vitepress
│ │ └─ config.mts
│ │ └─ theme
│ │ └─ style
│ │ └─ index.css
│ │ └─ sidebarIcon.css
│ └─ index.md
└─ node_modules
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
其内容是 :
css
/* .vitepress/theme/style/sidebarIcon.css */
/* 侧边栏缩放 */
.group:has([role='button']) .VPSidebarItem.level-0 .items {
padding-left: 15px !important;
border-left: 1px solid var(--vp-c-divider);
border-radius: 2px;
transition: background-color 0.25s;
}
/* 侧边栏图标 */
/* 选中所有 .VPSidebarItem 元素,排除带有 .is-link 类的 */
#VPSidebarNav .VPSidebarItem:not(.is-link).collapsed >.item {
display: inline-flex;
align-items: center; /* 垂直居中对齐图标和文本 */
}
/* 为所有不带 .is-link 的 .VPSidebarItem 折叠状态添加图标 */
#VPSidebarNav .VPSidebarItem:not(.is-link).collapsed >.item::before {
content: '';
background-image: url('/svg/document.svg'); /* 设置图标路径 */
width: 16px;
height: 16px;
display: inline-block;
vertical-align: middle; /* 确保图标与文本垂直居中 */
background-size: cover;
margin-right: 4px; /* 给图标和文本之间增加间距 */
}
#VPSidebarNav .VPSidebarItem:not(.is-link) >.item {
display: inline-flex;
align-items: center; /* 垂直居中对齐图标和文本 */
}
/* 为所有不带 .is-link 的 .VPSidebarItem 非折叠状态添加图标 */
#VPSidebarNav .VPSidebarItem:not(.is-link) >.item::before {
content: '';
background-image: url('/svg/document-open.svg'); /* 设置图标路径 */
width: 16px;
height: 16px;
display: inline-block;
vertical-align: middle; /* 确保图标与文本垂直居中 */
background-size: cover;
margin-right: 4px; /* 给图标和文本之间增加间距 */
}
/* 选中带有 .is-link 的 .VPSidebarItem 的直接子元素 .item */
#VPSidebarNav .VPSidebarItem.is-link > .item {
display: inline-flex;
align-items: center; /* 垂直居中图标和文字 */
}
/* 为选中的 .item 添加图标 */
#VPSidebarNav .VPSidebarItem.is-link > .item::before {
content: '';
background-image: url('/svg/file.svg'); /* 图标路径 */
width: 16px;
height: 16px;
display: inline-block;
vertical-align: middle;
background-size: cover;
margin-right: 4px; /* 图标与文字间距 */
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
(2)然后在 index.css
中引入生效
/* .vitepress/theme/style/index.css */
@import './sidebarIcon.css';
1
2
2
(3)验证效果(完美)
结束。