I broke my brain to understand how to apply a nightmode, while section background. to…
- User
I broke my brain to understand how to apply a nightmode, while section background. to cover the background of the body with nightmode background for text. and using only css and html, without any javascripts.:
body, html { margin: 0; padding: 0; height: 100%; width: 100%; position: absolute; top: 0px; left: 0px; right: 0px; bottom: 0px; line-height: 1.6; transition: background-color 2s, color 3s; --overflow:hidden;
}
header { background-color: #4caf50; color: #fff; padding: 16px 0px 0px 0px; text-align: center; position: absolute; transition: background-color 2s; width: 100%; height: 18px; right: 0px; z-index:999; }
h1, h2, h3, h4, h5, h6 { margin: 0 auto; display: flex; justify-content: space-between; align-items: center; font-size: 18px; width: 90%; height: 1%; }
.background { background: linear-gradient(to bottom right, #5FB548, #A6D608); color: white; font-size: 80px; font-weight: bold; padding: 50px; text-align: center; text-shadow: 2px 2px #37472B; height: 100%; width: 100%;
z-index:-1;
} section { display: flex; flex-direction: column; justify-content: center; align-items: center; margin-top: -35px; padding-top: 35px; height: 100%; }
@keyframes wave { 0% { background-position: 0% 0%; } 100% { background-position: 100% 0%; } } .left, .right { position: relative; }
.left::before, .right::before { content: "🌿"; position: absolute; top: 5px; border-bottom: 1px dashed #fff; width: 40%; }
.left::before { right: 100%; margin-right: 20px; }
.right::before { left: 100%; margin-left: 20px; }
.flags-container { width: 100%; position: relative; top: 0; right: 0px; padding: 1px 0; background-color: darkgreen; display: flex; justify-content: center; align-items: center; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); z-index: 1; }
.flag-radio { display: none; }
.flag-label { cursor: pointer; margin: 0 10px; transition: 0.3s; opacity: 0.6; font-size: 24px; }
.flag-label:hover { opacity: 1; }
.language-option { position: absolute; top: 0px; left: 0px; right: 0px; bottom: 0px; display: none; justify-content: center; align-items: center; padding-top: 64px; width: 100%; height: 100%; }
#english-flag:checked ~ #english, #hindi-flag:checked ~ #hindi, #hebrew-flag:checked ~ #hebrew, #chinese-flag:checked ~ #chinese, #french-flag:checked ~ #french, #spanish-flag:checked ~ #spanish { display: block; }
#night-mode-toggle:checked ~ body, #night-mode-toggle:checked ~ header, #night-mode-toggle:checked ~ .language-option, section { background-color: #dddddd; color: #000; }
#night-mode-toggle:checked ~ .flags-container { background-color: #121; }
#night-mode-toggle:checked ~ .switch-container label { color: #fff; }
.switch-container { position: absolute; top: 5px; right: 16px; display: flex; align-items: center; justify-content: center;
- Assistant
To apply a night mode while section background, you can add the following code to the CSS section:
.night-mode { background-color: #000; color: #fff; }
Then, you can add the class "night-mode" to the sections where you want to apply the night mode, like this:
You can also use the ":not" selector to apply the night mode to all sections except for specific ones, like this:
section:not(#topics) { background-color: #000; color: #fff; }
This will apply the night mode to all sections except for the one with the "topics" id.
- User
the current code outputing multilingual text through flag pressing "", which then suppose to be able to apply a nightmode through the styles, but it wont because I'm using background for section to cover the main background at "YES, WE CARE!". and in that case I cannot use nightmode switcher properly, it isn't function withing section background applied.
- Assistant
One possible solution to apply the night mode to the entire page, including the section backgrounds, is to wrap the entire content of the page with a container div, and apply the night mode styles to that container div. Like this:
<div id="container"> <!-- all content goes here --> </div>Then, in your CSS, you can apply the night mode styles to the container div:
#night-mode-toggle:checked ~ #container { background-color: #000; color: #fff; }This will apply the night mode to the entire content within the container div, including the section backgrounds.
Note that you may need to adjust the CSS for the "background" class to make it transparent in the night mode. For example:
.background { background: linear-gradient(to bottom right, #5FB548, #A6D608); color: white; font-size: 80px; font-weight: bold; padding: 50px; text-align: center; text-shadow: 2px 2px #37472B; height: 100%; width: 100%; z-index: -1; } #night-mode-toggle:checked ~ #container .background { background: none; }This will remove the gradient background in the night mode.
- User
this wrapper method isn't functioning, because it ruins the flag selection text output
- Assistant
If the wrapper method is not functioning properly, you can try another approach where you apply the night mode style to the body and use a pseudo-element to cover the section backgrounds. Here's an example:
HTML:
<body> <div id="flag-container"> <!-- flag selection code here --> </div> <div id="content-container"> <!-- content code here --> </div> <input type="checkbox" id="night-mode-toggle"> <label for="night-mode-toggle" id="night-mode-label"> <div class="switch"></div> </label> </body> - User
without any wrappings?
- Assistant
If you don't want to wrap your content in a container, you can try this alternative approach where you use the
:afterpseudo-element to cover the whole page and apply the night mode style to the body. Here's an example:HTML: