thumbnail

Somnia主题美化-首页背景图

很多博主为了站点整体美化,可能会配置主页背景图需求,我们也Somnia主题官方也提供了不影响整体性能的配置方法。

配置教程

CSS代码

/* 高优先级背景方案 - 纯CSS实现 */
body::before {
    content: "";
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    background: url("图片地址") center/cover no-repeat;
    background-color: #f0f0f0;
    pointer-events: none;
    opacity: 1;
    transition: opacity 0.3s ease;
}

/* 确保body占据全屏高度 */
body {
    min-height: 100vh;
    margin: 0;
    padding: 0;
    overflow-x: hidden;
    position: relative;
    z-index: 1;
}

/* 聊天组件样式 */
.smilie-tab {
    display: flex;
    overflow-x: auto;
    overflow-y: hidden;
    width: 300px;
}

a.but.btn-input-expand.input-smilie.mr6 {
    flex-shrink: 0;
    margin: 2px 0;
}

.dropdown-smilie {
    width: 300px;
    height: 240px;
    padding: 8px 10px;
    white-space: normal;
    text-align: center;
}

.dropdown-smilie .smilie-icon {
    display: inline-block;
    padding: 3px;
    width: 50px !important;
}

/* 滚动性能优化 */
html {
    scroll-behavior: smooth;
    scrollbar-width: thin;
}

body::-webkit-scrollbar {
    width: 6px;
}

body::-webkit-scrollbar-thumb {
    background-color: rgba(150, 150, 150, 0.5);
    border-radius: 3px;
}

/* 移动端优化 - 使用cover保持与PC端一致的全屏效果 */
@media (max-width: 768px) {
    body::before {
        background-attachment: fixed; /* 保持固定背景 */
        background-size: cover; /* 使用cover而非contain */
        
        /* 针对移动端的额外优化 */
        -webkit-background-size: cover; /* 兼容iOS Safari */
        background-position: center top; /* 调整定位,优先显示顶部内容 */
    }
}

设置方法

复制css代码>管理后台>外观>主题设置>其他设置># 自定义head>style自定义样式内

关键优化说明:

1.使用 body::before 伪元素:

  • 创建一个固定在视口的背景层,不影响内容布局
  • 通过 z-index 确保背景在内容下方
  • pointer-events: none 允许点击穿透背景层

2.优先级保障:

  • 使用伪元素选择器而非直接设置 body 背景
  • 避免使用!important,通过合理的 DOM 结构和 z-index 控制优先级

3.性能优化:

  • 背景层与内容层分离,滚动时减少样式重新计算
  • 细滚动条设计减少重绘区域
  • 平滑滚动提升用户体验

4.移动端适配:

  • 统一使用 cover 模式
  • 确保背景图在所有设备上都能覆盖整个视口
  • 添加 -webkit-background-size: cover 确保 iOS Safari 正确渲染
  • 使用 background-position: center top 优先显示背景图顶部内容(通常是设计重点)