I have the following snippet where the display of the paragraphs are determined by whether the container has the class .dark-mode
or not.
function toggleDarkMode() {
const container = document.getElementById("container");
container.classList.toggle("dark-mode");
}
:not(.dark-mode) .dark-mode-only {
display: none;
}
.dark-mode .light-mode-only {
display: none;
}
<div id="container">
<p class="light-mode-only">
Only for light mode.
</p>
<p class="dark-mode-only">
Only for dark mode.
</p>
<button onclick="toggleDarkMode()">Toggle dark mode</button>
</div>
As you can see, clicking the button adds the .dark-mode
class to the container, but the :not(.dark-mode) .dark-mode-only {...}
selector does not actually work. However, if I simply add div
in-front of this statement, it works again:
function toggleDarkMode() {
const container = document.getElementById("container");
container.classList.toggle("dark-mode");
}
div:not(.dark-mode) .dark-mode-only {
display: none;
}
.dark-mode .light-mode-only {
display: none;
}
<div id="container">
<p class="light-mode-only">
Only for light mode.
</p>
<p class="dark-mode-only">
Only for dark mode.
</p>
<button onclick="toggleDarkMode()">Toggle dark mode</button>
</div>
So what exactly is the issue here? Does the :not
selector work only when the element is specified before it? Thanks for any help.
EDIT Even using something like *:not(.dark-mode)
does not work.