简体   繁体   English

CSS 变量在实现深色/浅色主题时不适用于 body 标签

[英]CSS variables are not working on body tag while implementing a dark/light theme

I am developing two themes for my site (light and dark), the site is developed in React.我正在为我的网站开发两个主题(浅色和深色),该网站是用 React 开发的。 In the main CSS file I have inserted the colors that should be used in the light theme and in the dark theme, like this:在主要的 CSS 文件中,我插入了应该在浅色主题和深色主题中使用的 colors,如下所示:

#light{
--color-bg: #4e4f50; /* #1f1f38 */
--color-bg-variant: #746c70; /* #2c2c6c */
--color-primary: #e2ded0; /* #4db5ff */
--color-primary-variant: #647c90; /* rgba(77, 181, 255, 0.4) */
--color-white: white;
--color-light: rgb(255 255 255 / 60%);
}

#dark{
--color-bg: #1A1A2E; /* #1f1f38 */
--color-bg-variant: #16213E; /* #2c2c6c */
--color-primary: #E94560; /* #4db5ff */
--color-primary-variant: #0F3460; /* rgba(77, 181, 255, 0.4) */
--color-white: white;
--color-light: rgb(255 255 255 / 60%);
}

These variables are read throughout the project apparently except in the CSS of the body tag which is like this:这些变量显然在整个项目中被读取,除了body标签的 CSS 是这样的:

body {
font-family: Poppins, sans-serif;
background: var(--color-bg);
color: var(--color-white);
line-height: 1.7; /* distanza tra le macroaree */
background-image: none;
}

Rightly, not working, there are all colors, except the background one, so the problem is not ignorable.对了,不行,colors全都有,除了后台一个,问题不容忽视。 I don't understand why in all the other CSS the variables are read but not from the body tag.我不明白为什么在所有其他 CSS 中读取变量而不是从body标签中读取。

Here is how I use and change the theme from light to dark and vice versa ( App.jsx ):以下是我如何使用主题并将主题从浅色更改为深色,反之亦然 ( App.jsx ):

import { createContext } from "react"
import { useState } from "react"

export const ThemeContext = createContext(null)

const App = () => {
const [theme, setTheme] = useState("dark")

const toggleTheme = () => {

setTheme((curr) => (curr === "light" ? "dark" : "light"))
}
return (
<ThemeContext.Provider value={{theme, setTheme}}>
  <Router>
      <Routes>
        <Route path="/" element={
          <div id={theme}>
            <Header/>
            <Nav/>
            <About/>
            <Experience/>
            <Services/>
            <BlogPreview/>
            <Contact/>
            <Footer/>
          </div>}
        />
        <Route path="/Blog" element={
          <div id={theme}>
            <Blog/>
            <NavG/>
          </div>}  
        />
        <Route path="*" element={
          <div id={theme}>
            <Error/>
            <NavGG/>
          </div>
        }/>
        <Route path="/Blog/buymeanr6please" element={
          <div id={theme}>
            <Post1/>
          </div>
        }/>
      </Routes>
  </Router>
</ThemeContext.Provider>
)
}

export default App

I hope I have explained myself well.我希望我已经很好地解释了自己。 Could someone give me a hand?有人可以帮我吗?

This is a cascading and inheritance issue.这是一个级联和 inheritance问题。 Those CSS variables of yours are defined in the context of that <div id={theme}> , which is a child of body , so body can't see them.您的那些 CSS 变量是在<div id={theme}>的上下文中定义的,它是body的子级,因此body看不到它们。 Remember, CSS is read top to bottom.请记住,从上到下读取 CSS。

Either move the property from body to <div id={theme}> , like so:将属性从body移动到<div id={theme}> ,如下所示:

#dark, #dark{
 font-family: Poppins, sans-serif;
 background: var(--color-bg);
 color: var(--color-white);
 line-height: 1.7; /* distanza tra le macroaree */
 background-image: none;
}

Or sets your variables on html so every child including body can see them.或者在html上设置您的变量,这样每个孩子(包括body )都可以看到它们。 If you decide to do it this way, add the below code in App and remove the id from <div id={theme}> .如果您决定这样做,请在App中添加以下代码并从<div id={theme}>中删除id

useEffect(() => {
   document.documentElement.setAttribute("id", theme);
}, [theme]);

First of all, use class names (start with dot) instead of Id's (start with #) cause element must have only unique ID's, and you need to change those based on user preference or clicked a toggle button.首先,使用 class 名称(以点开头)而不是 ID(以 # 开头),因为元素必须只有唯一 ID,您需要根据用户偏好更改这些 ID 或单击切换按钮。

.light{
  ...// your light colors
}

.dark{
  ...// your dark colors
}

Then use one of these classes on html element so body would inherit those variables.然后在html元素上使用这些类之一,以便body继承这些变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM