Animated Toggle Switch by codepeek
Zoom
Saved
1 line⌘Z Undo
1<!--2 Author: @codepeek3 License: MIT4 Tags: #animated #dark #playful #switch #weather5-->6<label class="dnh">7 <input class="dnh__in" type="checkbox" role="switch" aria-label="Night mode">8 <span class="dnh__sky" aria-hidden="true">9 <span class="dnh__star">10 </span>11 <span class="dnh__star">12 </span>13 <span class="dnh__star">14 </span>15 <span class="dnh__star">16 </span>17 <span class="dnh__orbit">18 <span class="dnh__sun">19 </span>20 <span class="dnh__moon">21 </span>22 </span>23 <span class="dnh__hill">24 </span>25 </span>26</label>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!--
Author: @codepeek
License: MIT
Tags: #animated #dark #playful #switch #weather
-->
<label class="dnh">
<input class="dnh__in" type="checkbox" role="switch" aria-label="Night mode">
<span class="dnh__sky" aria-hidden="true">
<span class="dnh__star">
</span>
<span class="dnh__star">
</span>
<span class="dnh__star">
</span>
<span class="dnh__star">
</span>
<span class="dnh__orbit">
<span class="dnh__sun">
</span>
<span class="dnh__moon">
</span>
</span>
<span class="dnh__hill">
</span>
</span>
</label>1/* * Author: @codepeek * License: MIT * Tags: #animated #dark #playful #switch #weather */2.dnh {3 position: relative;4 display: inline-block;5 width: 168px;6 height: 76px;7 cursor: pointer;8}9.dnh__in {10 position: absolute;11 top: 0;12 left: 0;13 width: 1px;14 height: 1px;15 margin: 0;16 opacity: 0;17}18.dnh__sky {19 position: absolute;20 inset: 0;21 overflow: hidden;22 border-radius: 38px;23 background: linear-gradient(#7fb7c9,#f2d3a0);24 box-shadow: inset 0 3px 8px rgba(0,0,0,.35),0 0 0 3px #161616,0 0 0 4px #3c3c3c;25}26.dnh__sky::before {27 content: "";28 position: absolute;29 inset: 0;30 background: linear-gradient(#08111f,#1e3a48);31 opacity: 0;32 transition: opacity .7s ease;33}34.dnh__in:checked + .dnh__sky::before {35 opacity: 1;36}37.dnh__orbit {38 position: absolute;39 left: 50%;40 top: 84px;41 width: 0;42 height: 0;43 transform: rotate(-52deg);44 transition: transform .8s cubic-bezier(.45,1.3,.4,1);45}46.dnh__sun {47 position: absolute;48 left: -14px;49 top: -76px;50 width: 28px;51 height: 28px;52 border-radius: 50%;53 background: radial-gradient(circle at 40% 40%,#ffe08a,#f2a531);54 box-shadow: 0 0 0 6px rgba(255,214,120,.25),0 0 24px 6px rgba(255,190,80,.45);55}56.dnh__moon {57 position: absolute;58 left: -12px;59 top: 50px;60 width: 24px;61 height: 24px;62 border-radius: 50%;63 background: #ece6d2;64 box-shadow: inset -6px -3px 0 #c4bb9f,0 0 14px 2px rgba(236,230,210,.35);65 transform: rotate(128deg);66}67.dnh__in:checked + .dnh__sky .dnh__orbit {68 transform: rotate(-128deg);69}70.dnh__star {71 position: absolute;72 width: 3px;73 height: 3px;74 border-radius: 50%;75 background: #f4efe0;76 opacity: 0;77 transition: opacity .3s;78}79.dnh__star:nth-child(1) {80 left: 96px;81 top: 14px;82 --dnh-i: 1;83}84.dnh__star:nth-child(2) {85 left: 122px;86 top: 30px;87 --dnh-i: 2;88}89.dnh__star:nth-child(3) {90 left: 70px;91 top: 22px;92 --dnh-i: 3;93 width: 2px;94 height: 2px;95}96.dnh__star:nth-child(4) {97 left: 140px;98 top: 16px;99 --dnh-i: 4;100 width: 2px;101 height: 2px;102}103.dnh__in:checked + .dnh__sky .dnh__star {104 opacity: 1;105 transition: opacity .5s calc(.35s + var(--dnh-i) * 110ms);106 animation: dnh-tw 2.4s calc(var(--dnh-i) * -.6s) ease-in-out infinite alternate;107}108.dnh__hill {109 position: absolute;110 left: -20px;111 right: -20px;112 bottom: -34px;113 height: 56px;114 border-radius: 50%;115 background: #365640;116 box-shadow: 70px -6px 0 -4px #2a4634;117 transition: background-color .7s,box-shadow .7s;118}119.dnh__in:checked + .dnh__sky .dnh__hill {120 background-color: #12241c;121 box-shadow: 70px -6px 0 -4px #0c1913;122}123.dnh__in:focus-visible + .dnh__sky {124 outline: 2px solid #f2d3a0;125 outline-offset: 7px;126}127@keyframes dnh-tw {128 to {129 transform: scale(.4);130 opacity: .5;131 }132}133@media (prefers-reduced-motion:reduce) {134 .dnh__orbit {135 transition: none;136 }137 .dnh__in:checked + .dnh__sky .dnh__star {138 animation: none;139 }140}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* * Author: @codepeek * License: MIT * Tags: #animated #dark #playful #switch #weather */
.dnh {
position: relative;
display: inline-block;
width: 168px;
height: 76px;
cursor: pointer;
}
.dnh__in {
position: absolute;
top: 0;
left: 0;
width: 1px;
height: 1px;
margin: 0;
opacity: 0;
}
.dnh__sky {
position: absolute;
inset: 0;
overflow: hidden;
border-radius: 38px;
background: linear-gradient(#7fb7c9,#f2d3a0);
box-shadow: inset 0 3px 8px rgba(0,0,0,.35),0 0 0 3px #161616,0 0 0 4px #3c3c3c;
}
.dnh__sky::before {
content: "";
position: absolute;
inset: 0;
background: linear-gradient(#08111f,#1e3a48);
opacity: 0;
transition: opacity .7s ease;
}
.dnh__in:checked + .dnh__sky::before {
opacity: 1;
}
.dnh__orbit {
position: absolute;
left: 50%;
top: 84px;
width: 0;
height: 0;
transform: rotate(-52deg);
transition: transform .8s cubic-bezier(.45,1.3,.4,1);
}
.dnh__sun {
position: absolute;
left: -14px;
top: -76px;
width: 28px;
height: 28px;
border-radius: 50%;
background: radial-gradient(circle at 40% 40%,#ffe08a,#f2a531);
box-shadow: 0 0 0 6px rgba(255,214,120,.25),0 0 24px 6px rgba(255,190,80,.45);
}
.dnh__moon {
position: absolute;
left: -12px;
top: 50px;
width: 24px;
height: 24px;
border-radius: 50%;
background: #ece6d2;
box-shadow: inset -6px -3px 0 #c4bb9f,0 0 14px 2px rgba(236,230,210,.35);
transform: rotate(128deg);
}
.dnh__in:checked + .dnh__sky .dnh__orbit {
transform: rotate(-128deg);
}
.dnh__star {
position: absolute;
width: 3px;
height: 3px;
border-radius: 50%;
background: #f4efe0;
opacity: 0;
transition: opacity .3s;
}
.dnh__star:nth-child(1) {
left: 96px;
top: 14px;
--dnh-i: 1;
}
.dnh__star:nth-child(2) {
left: 122px;
top: 30px;
--dnh-i: 2;
}
.dnh__star:nth-child(3) {
left: 70px;
top: 22px;
--dnh-i: 3;
width: 2px;
height: 2px;
}
.dnh__star:nth-child(4) {
left: 140px;
top: 16px;
--dnh-i: 4;
width: 2px;
height: 2px;
}
.dnh__in:checked + .dnh__sky .dnh__star {
opacity: 1;
transition: opacity .5s calc(.35s + var(--dnh-i) * 110ms);
animation: dnh-tw 2.4s calc(var(--dnh-i) * -.6s) ease-in-out infinite alternate;
}
.dnh__hill {
position: absolute;
left: -20px;
right: -20px;
bottom: -34px;
height: 56px;
border-radius: 50%;
background: #365640;
box-shadow: 70px -6px 0 -4px #2a4634;
transition: background-color .7s,box-shadow .7s;
}
.dnh__in:checked + .dnh__sky .dnh__hill {
background-color: #12241c;
box-shadow: 70px -6px 0 -4px #0c1913;
}
.dnh__in:focus-visible + .dnh__sky {
outline: 2px solid #f2d3a0;
outline-offset: 7px;
}
@keyframes dnh-tw {
to {
transform: scale(.4);
opacity: .5;
}
}
@media (prefers-reduced-motion:reduce) {
.dnh__orbit {
transition: none;
}
.dnh__in:checked + .dnh__sky .dnh__star {
animation: none;
}
}Changes made here are only a live preview and will not be saved.
Save to GitHub
GitHub opens a new file in your repository with this element filled in. Review it there and press Commit changes. Nothing is sent from codepeek and no GitHub access is needed.
◉ 1 views
Build it with AIA ready prompt with the code, the credit and how to adapt it to your project.
A pill of sky where the sun swings down behind the hills on an arc and the moon rises on the other side, with stars fading in one by one.
Choose where to donate
Select a platform to continue.
Community
Comments 0
Code copied
SUPPORT THE CREATOR
Would you like to donate to the creator to help them keep creating content?
You need to sign in before you can write a comment.