惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
Martin Fowler
Martin Fowler
The GitHub Blog
The GitHub Blog
B
Blog RSS Feed
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
量子位
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
L
LangChain Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园_首页
IT之家
IT之家
V
Visual Studio Blog
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
I
InfoQ
D
Docker
V
V2EX

Donghai's Blog

使用DBeaver连接Dynamics 365 Dataverse | Donghai C# 技术备忘 - LINQ | Donghai 为Astro站点添加Google Analytics | Donghai C# 技术备忘 - LINQ - Donghai’s Blog 20260819 说些什么 | Donghai Dynamics 365 interview questions | Donghai C# 技术备忘 (1) | Donghai Dynamics 365 事件框架、事件执行管道 | Donghai 马拉松配速表 | Donghai 马拉松赛事分级 | Donghai 抖音创作日志(2026年) | Donghai 2026年跑步日志(5月开始) | Donghai 2FA | Donghai 初尝Github Actions | Donghai AstroPaper主题添加Waline评论 | Donghai Dynamics 365集中视图/聚焦视图/Focused View | Donghai AstroPaper主题添加GitHub风格的Markdown警告框 | Donghai AstroPaper主题自定义记录 | Donghai 我日常收藏的优质网站资源导航(持续更新) | Donghai 如何从归档页批量获取URL并提交到Bing Webmaster Tools | Donghai PaperMod 使用 Zeoseven 自定义网页字体 | Donghai 我常用的Prompt | Donghai Hugo默认时区导致本地预览文章不显示 | Donghai 通过手动提交工单解决Bing不收录网站问题 | Donghai 如何访问 Power BI 管理门户(Power BI Admin Portal) | Donghai 还在手敲时间戳?Win11输入法一个技巧,秒输当前时间戳 | Donghai 工作术语备忘录(持续更新) | Donghai 使用XrmToolBox导出安全角色表级权限到Excel | Donghai 24年的年终总结 | Donghai World笔记 | Donghai
自定义AstroPaper主题配色方案(译) | Donghai
Donghai · 2022-09-25 · via Donghai's Blog

原文章:Customizing AstroPaper theme color schemes

这篇文章将解释如何为网站启用或禁用明亮和黑暗模式。此外,你还将学习如何自定义整个网站的配色方案。

Table of contents

Open Table of contents
  • 启用/禁用明亮和黑暗模式
  • 选择主题配色方案
  • 自定义配色方案

启用/禁用明亮和黑暗模式

AstroPaper 主题默认会包含明亮和黑暗模式。换句话说,系统会有两种配色方案:一种是明亮模式,另一种是黑暗模式。你可以在 SITE 配置对象中禁用这种默认行为。

export const SITE = {
  website: "https://astro-paper.pages.dev/", // 把这个替换为你部署的域名
  author: "Sat Naing",
  profile: "https://satnaing.dev/",
  desc: "一个简约、响应式且对SEO友好的Astro博客主题。",
  title: "AstroPaper",
  ogImage: "astropaper-og.jpg",
  lightAndDarkMode: true,
  postPerIndex: 4,
  postPerPage: 4,
  scheduledPostMargin: 15 * 60 * 1000, // 15分钟
  showArchives: true,
  showBackButton: true, // 在文章详情中显示返回按钮
  editPost: {
    enabled: true,
    text: "Suggest Changes",
    url: "https://github.com/satnaing/astro-paper/edit/main/",
  },
  dynamicOgImage: true,
  lang: "en", // html语言代码。留空则默认是"en"
  timezone: "Asia/Bangkok",
} as const;
src/config.ts

要禁用明亮和黑暗模式,将 SITE.lightAndDarkMode 设置为 false

选择主题配色方案

默认情况下,如果我们禁用 SITE.lightAndDarkMode,系统将只根据用户的首选配色方案来设置。

因此,要选择主要的配色方案而不是使用首选配色方案,我们需要在 toggle-theme.js 文件中的 primaryColorScheme 变量中设置配色方案。

const primaryColorScheme = ""; // "light" | "dark"

// Get theme data from local storage
const currentTheme = localStorage.getItem("theme");

// ...public/toggle-theme.js

primaryColorScheme 变量可以有两个值:"light""dark"。如果你不想指定主要配色方案,可以将其留空(默认)。

  • "" - 系统的首选配色方案(默认)
  • "light" - 使用明亮模式作为主要配色方案
  • "dark" - 使用黑暗模式作为主要配色方案
为什么 'primaryColorScheme' 不在 config.ts 文件中? 为了避免页面重新加载时出现颜色闪烁,我们必须在页面加载时尽早放置切换开关的JavaScript代码。这解决了闪烁问题,但作为权衡,我们不能再使用ESM导入

自定义配色方案

AstroPaper 主题的明亮和黑暗配色方案可以在 global.css 文件中自定义

@import "tailwindcss";
@import "./typography.css";

@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));

:root,
html[data-theme="light"] {
  --background: #fdfdfd;
  --foreground: #282728;
  --accent: #006cac;
  --muted: #e6e6e6;
  --border: #ece9e9;
}

html[data-theme="dark"] {
  --background: #212737;
  --foreground: #eaedf3;
  --accent: #ff6b01;
  --muted: #343f60bf;
  --border: #ab4b08;
}
/* ... */src/styles/global.css

在AstroPaper主题中,:roothtml[data-theme="light"] 选择器定义了明亮的配色方案,而 html[data-theme="dark"] 定义了黑暗的配色方案。

要自定义自己的配色方案,请在 :root, html[data-theme="light"] 中指定明亮的颜色,在 html[data-theme="dark"] 中指定黑暗的颜色。

以下是颜色属性的详细说明。

颜色属性定义和用法
--background网站的主要颜色,通常是背景色
--foreground网站的次要颜色,通常是文本颜色
--accent网站的强调色,用于链接颜色、悬停颜色等
--muted卡片和滚动条的背景颜色,用于悬停状态等
--border边框颜色,用于边框工具和视觉分隔

以下是改变明亮配色方案的示例。

/* ... */
:root,
html[data-theme="light"] {
  --background: #f6eee1;
  --foreground: #012c56;
  --accent: #e14a39;
  --muted: #efd8b0;
  --border: #dc9891;
}
/* ... */src/styles/global.css

查看一些 预定义的配色方案 ,AstroPaper已经为你准备好了。

(完)

如果这篇文章刚好帮到了你,欢迎请我喝杯咖啡