You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

移动端Brave/Opera浏览器深色模式下网页显示异常求助

问题解决:移动端Brave/Opera深色模式颜色反转异常

问题原因

移动端Brave和Opera浏览器自带的自动深色模式颜色反转功能会忽略自定义CSS变量,强制对页面颜色进行反转,导致主题样式失效,出现背景色、文字颜色错乱的问题。

解决方案

1. 添加HTML元标签

在<head>中加入以下元标签,明确告诉浏览器使用自定义颜色方案,禁止自动反转:

<meta name="color-scheme" content="light dark">

2. 补充CSS属性

在主题变量定义中添加color-scheme属性,强化浏览器对自定义主题的识别:

:root {
  /* 原有变量 */
  color-scheme: light;
}

[data-theme="dark"] {
  /* 原有变量 */
  color-scheme: dark;
}

3. 可选:禁用强制颜色调整

如果上述方法仍无效,在body中添加属性完全禁止浏览器的强制颜色修改:

body {
  /* 原有样式 */
  forced-color-adjust: none;
}

修改后的完整代码示例

HTML(新增元标签)

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="color-scheme" content="light dark">
  <title>Porfolio</title>
  <link rel="stylesheet" href="styles.css">
</head>

<body>
  <div class="container">
    <div class="theme-switch-wrapper">
      <label class="theme-switch">
                <input type="checkbox">
                <span class="slider"></span>
            </label>
    </div>
    <h2>Projects</h2>
    <div class="project-list">
      <a href="projects/project-1.html" target="_self" class="project-link">
                <span class="project-link-text">Project 1</span>
                <span class="project-domain">Web</span>
            </a>
      <a href="projects/project-2.html" target="_self" class="project-link">
                <span class="project-link-text">Project 2</span>
                <span class="project-domain">Mobile</span>
            </a>
    </div>
    <br />
    <div class="footer">
      <div><a href="https://www.google.com">Google Link</a></div>
      <div><a href="https://www.facebook.com">Facebook Link</a></div>
    </div>
  </div>
  <script src="script.js"></script>
</body>

</html>

CSS(新增颜色方案属性)

/* Import fonts */
@import url('https://fonts.googleapis.com/css2?family=Special+Elite&display=swap');

/* Theme Variables */
:root {
  /* Light theme */
  --bg-overlay: none;
  --text-color: #271B08;
  --list-bg: white;
  --link-color: #2c3e50;
  --link-hover-bg: #f8f9fa;
  --link-hover-color: #1a252f;
  --footer-disclaimer-link-hover-color: #1a252f;
  --border-color: #eee;
  --muted-color: gray;
  color-scheme: light;
}

[data-theme="dark"] {
  /* Dark theme */
  --bg-overlay: linear-gradient(rgba(237, 221, 195, 0.4), rgba(237, 221, 195, 0.4));
  --text-color: #271B08;
  --list-bg: #EDDDC3;
  --link-color: #271B08;
  --link-hover-bg: #746556;
  --link-hover-color: #EDDDC3;
  --footer-disclaimer-link-hover-color: #746556;
  --border-color: #746556;
  --muted-color: #746556;
  --footer-link-hover: #271B08;
  color-scheme: dark;
}

/* Base styles */
body {
  background-size: cover;
  color: var(--text-color);
  font-family: 'Special Elite', monospace;
  margin: 0;
  padding: 20px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  forced-color-adjust: none;
}

.container {
  max-width: 800px;
  width: 100%;
  padding: 20px;
  border-radius: 8px;
  box-sizing: border-box;
  margin: 0 auto;
}

h1,
h2,
h3 {
  text-align: center;
  color: var(--text-color);
}

h3 {
  margin-top: 2em;
  margin-bottom: 1em;
  font-size: 1.2em;
}

.project-list {
  display: flex;
  flex-direction: column;
  background-color: var(--list-bg);
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.project-link {
  position: relative;
  display: block;
  padding: 10px;
  margin: 5px 0;
  color: var(--link-color);
  text-decoration: none;
  border-bottom: 1px solid var(--border-color);
  transition: background-color 0.2s ease;
  padding-right: 100px;
}

.project-link:hover {
  background-color: var(--link-hover-bg);
  color: var(--link-hover-color);
}

.project-link-text {
  display: block;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.project-domain {
  position: absolute;
  font-size: small;
  right: 10px;
  top: 10px;
  color: var(--muted-color);
  white-space: nowrap;
}

.project-link:hover .project-domain {
  color: var(--link-hover-color);
}

.footer,
.disclaimer {
  text-align: center;
  color: var(--text-color);
  margin-top: 20px;
  font-size: 14px;
}

.footer {
  padding: 10px;
}

.disclaimer p {
  margin: 10px 0;
}

.footer a,
.disclaimer a {
  text-decoration: none;
  color: var(--link-color);
  margin: 5px;
  font-size: 14px;
}

.footer a:hover,
.disclaimer a:hover {
  color: var(--footer-disclaimer-link-hover-color);
  text-decoration: underline;
}

.theme-switch-wrapper {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin: 20px 0;
  width: 100%;
}

.theme-switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.theme-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #746556;
  transition: 0.4s;
  border-radius: 34px;
}

.slider:before {
  position: absolute;
  content: "☀️";
  display: flex;
  align-items: center;
  justify-content: center;
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  transition: 0.4s;
  border-radius: 50%;
}

input:checked+.slider {
  background-color: #271B08;
}

input:checked+.slider:before {
  transform: translateX(26px);
  content: "🌙";
  background-color: #EDDDC3;
}

JS(保持原有逻辑不变)

function initThemeSwitch() {
  const themeSwitch = document.querySelector('.theme-switch input[type="checkbox"]');
  if (!themeSwitch) return;

  const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;

  function getThemePreference() {
    const savedTheme = localStorage.getItem('theme');
    return savedTheme || (prefersDark ? 'dark' : 'light');
  }

  function updateTheme(theme) {
    document.documentElement.setAttribute('data-theme', theme);
    if (themeSwitch) {
      themeSwitch.checked = theme === 'dark';
    }
  }

  updateTheme(getThemePreference());

  window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
    if (!localStorage.getItem('theme')) {
      const newTheme = e.matches ? 'dark' : 'light';
      updateTheme(newTheme);
    }
  });

  themeSwitch.addEventListener('change', function(e) {
    const theme = e.target.checked ? 'dark' : 'light';
    localStorage.setItem('theme', theme);
    updateTheme(theme);
  });

  window.addEventListener('storage', function(e) {
    if (e.key === 'theme') {
      updateTheme(e.newValue);
    }
  });

  document.addEventListener('visibilitychange', function() {
    if (document.visibilityState === 'visible') {
      updateTheme(getThemePreference());
    }
  });
}

if (document.readyState === 'loading') {
  document.addEventListener('DOMContentLoaded', initThemeSwitch);
} else {
  initThemeSwitch();
}

内容的提问来源于stack exchange,提问作者batman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 11:32:00