样式自定义实战
概述
本文档记录了本博客样式自定义的实战过程,包括上一篇/下一篇导航、页脚、顶部区域等组件的样式调整方法。
Butterfly 主题样式自定义方式
方式一:主题配置文件(推荐)
在 _config.butterfly.yml 中修改主题颜色:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| theme_color: enable: true main: "#49B1F5" paginator: "#00c4b6" button_hover: "#FF7242" text_selection: "#00c4b6" link_color: "#99a9bf" meta_color: "#858585" hr_color: "#A4D8FA" code_foreground: "#F47466" code_background: "rgba(27, 31, 35, .05)" toc_color: "#00c4b6" blockquote_padding_color: "#49b1f5" blockquote_background_color: "#49b1f5" scrollbar_color: "#49b1f5"
|
方式二:自定义 CSS 文件
- 在
_config.butterfly.yml 中配置注入:
1 2 3
| inject: head: - <link rel="stylesheet" href="/css/custom.css">
|
- 创建
source/css/custom.css 文件编写自定义样式
实战案例:上一篇/下一篇导航
问题描述
Butterfly 主题默认的上一篇/下一篇导航使用纯色背景,视觉效果单调。
解决方案
通过自定义 CSS 实现背景图 + 磨砂玻璃效果。
最终代码
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 64 65 66 67 68 69 70 71
|
.pagination-related { position: relative !important; overflow: hidden !important; }
html:not([data-theme="dark"]) .pagination-related { background: url('https://picsum.photos/800/200?random=1') center/cover no-repeat !important; }
html:not([data-theme="dark"]) .pagination-related::before { content: '' !important; position: absolute !important; top: 0 !important; left: 0 !important; right: 0 !important; bottom: 0 !important; background: rgba(255, 255, 255, 0.2) !important; backdrop-filter: blur(3px) !important; -webkit-backdrop-filter: blur(3px) !important; z-index: 1 !important; }
html[data-theme="dark"] .pagination-related { background: url('https://picsum.photos/800/200?random=2') center/cover no-repeat !important; }
html[data-theme="dark"] .pagination-related::before { content: '' !important; position: absolute !important; top: 0 !important; left: 0 !important; right: 0 !important; bottom: 0 !important; background: rgba(0, 0, 0, 0.3) !important; backdrop-filter: blur(3px) !important; -webkit-backdrop-filter: blur(3px) !important; z-index: 1 !important; }
.pagination-related > * { position: relative !important; z-index: 2 !important; }
.pagination-related .info .info-item-1 { color: rgba(255, 255, 255, 0.9) !important; }
.pagination-related .info .info-item-2 { color: #fff !important; font-weight: 500 !important; text-shadow: 0 1px 3px rgba(0, 0, 0, 0.2) !important; }
.pagination-related .cover { display: none !important; }
|
效果说明
| 模式 | 背景 | 遮罩 | 磨砂 |
|---|
| 白天 | picsum.photos 随机图 | rgba(255,255,255,0.2) | blur(3px) |
| 晚上 | picsum.photos 随机图 | rgba(0,0,0,0.3) | blur(3px) |
CSS 属性解释
backdrop-filter
1 2
| backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
|
blur(3px):轻微模糊,数值越大越模糊- 建议范围:
2px - 5px(轻微效果)
遮罩透明度
1 2
| background: rgba(255, 255, 255, 0.2); background: rgba(0, 0, 0, 0.3);
|
0.1:几乎透明0.2:轻微遮罩(推荐)0.3:中等遮罩0.5:较重遮罩
z-index 层级
1 2
| .pagination-related::before { z-index: 1; } .pagination-related > * { z-index: 2; }
|
确保内容在遮罩层之上显示。
其他样式自定义案例
页脚样式
1 2 3 4 5 6 7 8 9 10
| #footer { background: transparent !important; padding: 10px 0 !important; min-height: auto !important; }
#footer-bar { padding: 10px 0 !important; }
|
顶部区域透明效果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #page-header { background-color: transparent !important; }
html:not([data-theme="dark"]) #page-header::before { background-color: rgba(255, 255, 255, 0.25) !important; backdrop-filter: blur(2px); }
html[data-theme="dark"] #page-header::before { background-color: rgba(0, 0, 0, 0.5) !important; backdrop-filter: blur(2px); }
|
调试技巧
1. 浏览器开发者工具
按 F12 打开开发者工具:
- Elements 面板:查看 HTML 结构和 CSS 样式
- Styles 面板:查看生效的 CSS 规则
- Computed 面板:查看最终计算的样式值
2. 实时修改样式
在 Styles 面板中可以直接修改 CSS 值,实时预览效果。
3. 查找选择器
右键点击元素 → 检查(Inspect),找到正确的 CSS 选择器。
4. 清除缓存
修改 CSS 后需要清除浏览器缓存:
- Windows/Linux:
Ctrl + Shift + R - Mac:
Cmd + Shift + R
5. 检查 CSS 是否加载
在 Network 面板中搜索 custom.css,确认文件已加载。
选择器优先级
当样式不生效时,可能是优先级问题。解决方法:
1 2 3 4 5 6 7 8 9
| .pagination-related { background: red !important; }
html:not([data-theme="dark"]) .pagination-related { background: red; }
|
常用资源
注意事项
- ** !important 谨慎使用**:只在必要时使用,避免覆盖其他样式
- 浏览器兼容性:
backdrop-filter 需要 -webkit- 前缀支持 Safari - 性能影响:过多的
backdrop-filter 可能影响页面性能 - 日夜模式:使用
html:not([data-theme="dark"]) 和 html[data-theme="dark"] 区分模式 - 缓存问题:修改 CSS 后务必清除浏览器缓存