26
loading...
This website collects cookies to deliver better user experience
:root {
/** sunny side **/
--light-background: #b87e54;
--light-olive: #4d4828;
--light-blue: #99c1c3;
--light-purple: #67597a;
--light-yellow: #e0cd7e;
/** dark side **/
--dark-background: #283618;
--dark-darkblue: #001d3d;
--dark-blue: #003566;
--dark-darkestblue: #000814;
--dark-mustard: #664e00;
}
background-color: var(--dark-background);
. input
HTML element. This input and its associated label will be turned into something that looks like a toggle with css, but will continue to function the same as a checked, or unchecked, input element. We will be leaning heavily on the ::before
and ::after
pseudo-elements to get it done.<input type="checkbox" id="toggle" class="mostHigh toggle--checkbox" />
<label for="toggle" class="toggle--label mostHigh">
</label>
.toggle--checkbox
with display: none
so that the original checkbox isn't visible and then create the toggle outline with .toggle--label
..toggle--label {
width: 80px;
height: 40px;
background: var(--blue-color);
border-radius: 100px;
border: 5px solid var(--blue-border);
display: flex;
}
::before
to create the switch portion that will toggle back and forth..toggle--label:before {
animation-name: reverse;
animation-duration: 350ms;
animation-fill-mode: forwards;
transition: all 350ms ease-in;
content: "";
width: 30px;
height: 30px;
border: 5px solid var(--yellow-border);
top: 0px;
left: 4px;
position: absolute;
border-radius: 82px;
background: var(--yellow-background);
}
+
to select the sibling of the class .toggle--checkbox
(the input) which is .toggle--label
(the label). Other combinators for simple selectors are descendant selector (space), a child selector >
and general sibling selector ~
.:hover
does, that when that class is checked then the css will update the sibling selectors code. The sibling qualifier is based on the HTML elements to which the classes are assigned..toggle--checkbox:checked + .toggle--label {
background: var(--indigo-color);
border-color: var(--indigo-border);
}
.toggle--checkbox:checked + .toggle--label:before {
background: var(--white);
border-color: var(--gray-border);
animation-name: switch;
animation-duration: 350ms;
animation-fill-mode: forwards;
}
.toggle--label:before
. We are using the keyframes to create visual motion and make the circle toggle move from the left side of the toggle switch to the right - and then back.@keyframes switch {
0% {
left: 4px;
}
60% {
left: 20px;
width: 50px;
}
100% {
left: 40px;
width: 30px;
}
}
@keyframes reverse {
0% {
left: 24px;
width: 42px;
}
60% {
left: 20px;
width: 50px;
}
100% {
left: 4px;
}
}
.toggle--label
. We want to make sure that the toggle itself is near the top level so that the header and the main part of the site can be sibling elements to grab on to.<Header />
<Toggle />
<Main />
.header {
background-color: var(--light-background);
color: var(--light-olive);
}
.toggle--checkbox:checked ~ .head {
background-color: var(--dark-background);
color: var(--white);
}