前言

默认的 Butterfly 主题布局宽度为 1200px,在现代的大屏显示器上会显得比较局促,两侧留白过多。本教程将教你如何优化 Butterfly 主题的宽屏布局,让博客在大屏幕上显示更加舒适美观。

优化内容

  • ✅ 页面最大宽度:1200px → 1400px
  • ✅ 侧边栏宽度:26%(动态) → 312px(固定)
  • ✅ 主内容区宽度:74% → 自动计算(总宽度 - 侧边栏)
  • ✅ 文章头图高度:400px → 460px
  • ✅ 完美支持响应式(手机/平板不受影响)

效果对比

修改前

  • 页面宽度:1200px(两侧留白多)
  • 侧边栏:26% 动态宽度(不同屏幕下宽度不一致)
  • 文章头图:400px(稍显矮小)

修改后

  • 页面宽度:1400px(充分利用屏幕空间)
  • 侧边栏:312px 固定宽度(视觉更稳定)
  • 主内容区:自动适配剩余宽度
  • 文章头图:460px(更大气)

视觉提升: 页面宽度提升 16.7%,阅读体验更加舒适!


修改步骤

第一步:调整页面布局宽度

找到文件:themes/butterfly/source/css/_page/common.styl

修改内容

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
.layout
display: flex
flex: 1 auto
margin: 0 auto
padding: 40px 15px
max-width: 1400px // 原值:1200px → 改为 1400px
width: 100%

+maxWidth900()
flex-direction: column

+maxWidth768()
padding: 20px 5px

+minWidth2000()
max-width: 60%

& > div:first-child:not(.nc)
@extend .cardHover
align-self: flex-start
padding: 50px 40px

+maxWidth768()
padding: 36px 14px

& > div:first-child
width: calc(100% - 312px) // 原值:74% → 改为自动计算
transition: all .3s

+maxWidth900()
width: 100% !important

if hexo-config('aside.position') == 'left'
+minWidth900()
order: 2

// 隱藏aside
&.hide-aside
max-width: 1400px // 原值:1000px → 改为 1400px

+minWidth2000()
max-width: 1300px

& > div
width: 100% !important

关键修改点

  1. .layoutmax-width1200px1400px
  2. 主内容区宽度计算width: 74%width: calc(100% - 312px)
  3. 无侧边栏时宽度max-width: 1000pxmax-width: 1400px

第二步:固定侧边栏宽度

找到文件:themes/butterfly/source/css/_layout/aside.styl

修改内容

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
#aside-content
width: 312px // 原值:26% → 改为固定 312px
max-width: 312px // 新增:最大宽度限制
min-width: 300px // 新增:最小宽度限制

+minWidth900()
if hexo-config('aside.position') == 'right'
padding-left: 15px
else
padding-right: 15px

+maxWidth900()
margin-top: 20px
width: 100%
max-width: none // 新增:平板/手机取消宽度限制
padding: 0 5px // 新增:添加内边距

.card-widget
@extend .cardHover
position: relative
overflow: hidden
margin-bottom: 20px
padding: 20px 24px

// ... 其他代码保持不变

关键修改点

  1. 固定侧边栏宽度26%312px
  2. 添加宽度范围max-width: 312pxmin-width: 300px
  3. 响应式适配:小屏幕下 max-width: none,恢复自适应

第三步:增加文章头图高度

找到文件:themes/butterfly/source/css/_layout/head.styl

修改内容

找到 &.post-bg 部分,修改如下:

1
2
3
4
5
6
// post
&.post-bg
height: 460px // 原值:400px → 改为 460px

+maxWidth768()
height: 360px // 手机端保持不变

关键修改点

  • 文章页头图高度400px460px(提升 15%)
  • 手机端不变:保持 360px,确保移动端体验

完整修改总结

文件 位置 原始值 修改后 说明
common.styl .layout max-width 1200px 1400px 页面最大宽度
common.styl > div:first-child width 74% calc(100% - 312px) 主内容区宽度
common.styl .hide-aside max-width 1000px 1400px 无侧边栏时宽度
aside.styl #aside-content width 26% 312px 侧边栏固定宽度
aside.styl #aside-content max-width 312px 侧边栏最大宽度
head.styl .post-bg height 400px 460px 文章头图高度

响应式适配说明

桌面端(屏幕宽度 > 900px)

  • ✅ 页面宽度:1400px
  • ✅ 侧边栏:固定 312px
  • ✅ 主内容区:自动计算(1400px - 312px - 间距)

平板端(屏幕宽度 ≤ 900px)

  • ✅ 侧边栏移到主内容下方
  • ✅ 宽度限制取消,全宽显示
  • ✅ 添加适当内边距(0 5px)

手机端(屏幕宽度 ≤ 768px)

  • ✅ 单列布局
  • ✅ 文章头图高度保持 360px
  • ✅ 所有元素全宽显示

注意事项

1. 备份文件

修改前请先备份原始文件,避免出错后无法恢复:

1
2
3
cp themes/butterfly/source/css/_page/common.styl themes/butterfly/source/css/_page/common.styl.bak
cp themes/butterfly/source/css/_layout/aside.styl themes/butterfly/source/css/_layout/aside.styl.bak
cp themes/butterfly/source/css/_layout/head.styl themes/butterfly/source/css/_layout/head.styl.bak

2. 清除缓存

修改完成后必须清除缓存:

1
2
3
hexo clean
hexo g
hexo s

3. 侧边栏宽度微调

如果你觉得 312px 不合适,可以自行调整:

  • 更宽:改为 350px(同时修改 calc(100% - 350px)
  • 更窄:改为 280px(同时修改 calc(100% - 280px)

4. 宽度数值对应关系

修改时请确保:

1
主内容区宽度 = calc(100% - 侧边栏宽度)

例如侧边栏是 312px,主内容区就是 calc(100% - 312px)


常见问题

Q1: 修改后页面显示错乱?

A: 请确保执行了 hexo clean 清除缓存,然后重新生成。

Q2: 手机端显示异常?

A: 检查 aside.styl 中的响应式代码是否正确添加了 max-width: none

Q3: 想要更宽的布局怎么办?

A: 可以将 1400px 改为 1600px 或更大,但注意不要超过主流屏幕宽度。

Q4: 只想改宽度,不想改侧边栏?

A: 可以只修改 common.stylmax-width,保持侧边栏为 26%


总结

通过本教程的三步修改,你的 Butterfly 主题博客将获得:

  • 🎨 更宽敞的视觉体验:1400px 布局充分利用屏幕空间
  • 📐 更稳定的侧边栏:固定宽度不随窗口变化而跳动
  • 📱 完美的响应式:手机/平板端不受影响
  • 🖼️ 更大气的头图:460px 高度让首屏更震撼

享受宽屏带来的舒适阅读体验吧! 🚀