我目前正在为我的网站添加暗模式。 更改全部通过脚本处理,并考虑用户的偏好以及机器的当前外观。如果在黑暗模式下,我基本上最终会在<body>
上"黑暗"类,否则什么都没有。
一切都运行良好,但是在一个元素上,我需要基于此更改背景图像,但我遇到了一个问题,因为我的顶级选择器已经基于正文。
这是我的代码:
.page-home {
.home-intro__caricature {
@include ratio-box(100%);
width: 100%;
border-radius: 50%;
background-color: var(--main-background-color);
background-image: url('../src/svgs/caricature.svg'), url('../src/svgs/pattern-light.svg');
background-repeat: no-repeat, repeat;
background-position: center bottom, center center;
background-size: contain, auto;
transform: rotateY(0deg);
z-index: 2;
transition: box-shadow 0.2s ease-in-out;
&.dark {
background-image: url('../src/svgs/caricature.svg'), url('../src/svgs/pattern-dark.svg');
background-repeat: no-repeat, repeat;
background-position: center bottom, center center;
background-size: contain, auto;
}
}
}
问题是.page-home
是一门关于<body>
的课程,.dark
也需要如此。 如何构造选择器,以便将.dark
添加到选择器上以进行body
?
如果我正确理解了您的问题,以下内容应该有效。
也无需重申其他背景属性。
.page-home {
.home-intro__caricature {
@include ratio-box(100%);
width: 100%;
border-radius: 50%;
background-color: var(--main-background-color);
background-image: url('../src/svgs/caricature.svg'), url('../src/svgs/pattern-light.svg');
background-repeat: no-repeat, repeat;
background-position: center bottom, center center;
background-size: contain, auto;
transform: rotateY(0deg);
z-index: 2;
transition: box-shadow 0.2s ease-in-out;
}
&.dark {
.home-intro__caricature {
background-image: url('../src/svgs/caricature.svg'), url('../src/svgs/pattern-dark.svg');
}
}
}