Today, we will learn how to build beautiful toggle switch CSS buttons. In the last post, we discussed how to create a Hover effect using Html and CSS Let us see how to create a toggle button using Html and CSS.
Toggle switch CSS:
You can also create this type of button for your website using Html and CSS. If you really want to create it then follow the below steps to create it.
How to create Toggle switch CSS?
For the creation of toggle buttons, we have to write the code of HTML and CSS in a code editor and then we can write the code for it.
You might like it:
Download a code editor Toggle switch CSS:
Code editor provides an environment for developers to create any type of project. It helps us to write error-free content.
Source code of Html:
It is a markup language and we use it to create the content of the website pages. These buttons are part of the website and use for adding functionality to the website. The code for different buttons is available below and you can use it in your Html file.
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 |
<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="style.css"> <title>Toggle switch CSS | FantacyDesigns</title> </head> <body> <div class="toggle-wrapper"> <div class="toggle normal"> <input id="normal" type="checkbox"/> <label class="toggle-item" for="normal"></label> </div> <div class="name">Normal</div> </div> <div class="toggle-wrapper"> <div class="toggle transparent"> <input id="transparent" type="checkbox"/> <label class="toggle-item" for="transparent"></label> </div> <div class="name">Transparent</div> </div> <div class="toggle-wrapper"> <div class="toggle checkcross"> <input id="checkcross" type="checkbox"/> <label class="toggle-item" for="checkcross"> <div class="check"></div> </label> </div> <div class="name">Yes & No</div> </div> <div class="toggle-wrapper"> <div class="toggle"> <input id="gravity" type="checkbox"/> <label class="toggle-item" for="gravity"></label> </div> <div class="name">Gravity</div> </div> <div class="toggle-wrapper"> <div class="toggle pancake-stack"> <input id="pancake" type="checkbox"/> <label class="toggle-item" for="pancake"> <div class="pancakes"> <div class="pancake"></div> <div class="pancake"></div> <div class="pancake"></div> <div class="butter"></div> </div> </label> </div> <div class="name">Pancake Stacks</div> </div> <div class="toggle-wrapper"> <div class="toggle dog-rollover"> <input id="doggo" type="checkbox"/> <label class="toggle-item" for="doggo"> <div class="dog"> <div class="ear"></div> <div class="ear right"></div> <div class="face"> <div class="eyes"></div> <div class="mouth"></div> </div> </div> </label> </div> <div class="name">Doggo Wants a Treat</div> </div> </div> </body> </html> |
Source code of CSS:
It is also a markup language but we use it to design our website. Buttons are also part of the website and we have designed them using CSS. We have used different animations in the buttons after clicking by using CSS.
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 |
/* Please ❤ this if you like it! */ body { background: #2e394d; display: flex; flex-wrap: wrap; justify-content: center; align-items: center; } * { box-sizing: border-box; } *:before, *:after { content: ""; position: absolute; } input { height: 40px; left: 0; opacity: 0; position: absolute; top: 0; width: 40px; } .toggle-wrapper { flex: 1 1 calc(100% / 3); min-height: 50vh; display: flex; align-items: center; justify-content: center; overflow: hidden; position: relative; } @media (max-width: 960px) { .toggle-wrapper { flex: 1 1 calc(100% / 2); } } @media (max-width: 700px) { .toggle-wrapper { flex: 1 1 100%; } } .toggle-wrapper:nth-child(1) { background: #00BE94; } .toggle-wrapper:nth-child(1):hover { z-index:1; box-shadow: rgb(38, 57, 77) 0px 20px 30px -10px; transform: scale(1); transition: .5s; } .toggle-wrapper:nth-child(2) { background: #FFBE48; } .toggle-wrapper:nth-child(2):hover { z-index:1; box-shadow: rgb(38, 57, 77) 0px 20px 30px -10px; transform: scale(1); transition: .5s; } .toggle-wrapper:nth-child(3) { background: #008EFF; } .toggle-wrapper:nth-child(3):hover { z-index:1; box-shadow: rgb(38, 57, 77) 0px 20px 30px -10px; transform: scale(1); transition: .5s; } .toggle-wrapper:nth-child(4) { background: #2F4858; } .toggle-wrapper:nth-child(4):hover { z-index:1; box-shadow: rgb(38, 57, 77) 0px 20px 30px -10px; transform: scale(1); transition: .5s; } .toggle-wrapper:nth-child(5) { background: #FB8D75; } .toggle-wrapper:nth-child(5):hover { z-index:1; box-shadow: rgb(38, 57, 77) 0px 20px 30px -10px; transform: scale(1); transition: .5s; } .toggle-wrapper:nth-child(6) { background: #EE6F7B; } .toggle-wrapper:nth-child(6):hover { z-index:1; box-shadow: rgb(38, 57, 77) 0px 20px 30px -10px; transform: scale(1); transition: .5s; } .name { width: 80%; position: absolute; font: 18px "Rubik", sans-serif; letter-spacing: 0.5px; text-transform: uppercase; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.4); bottom: 15px; right: 15px; text-align: right; color:#fff; font-weight:bold } .toggle { position: relative; display: inline-block; } label.toggle-item { width: 7em; background: #2e394d; height: 3em; display: inline-block; border-radius: 50px; margin: 40px; position: relative; transition: all 0.3s ease; transform-origin: 20% center; cursor: pointer; } label.toggle-item:before { display: block; transition: all 0.2s ease; width: 2.3em; height: 2.3em; top: 0.25em; left: 0.25em; border-radius: 2em; border: 2px solid #88cf8f; transition: 0.3s ease; } .normal label { background: #af4c4c; border: 0.5px solid rgba(117, 117, 117, 0.31); box-shadow: inset 0px 0px 4px 0px rgba(0, 0, 0, 0.2), 0 -3px 4px rgba(0, 0, 0, 0.15); } .normal label:before { border: none; width: 2.5em; height: 2.5em; box-shadow: inset 0.5px -1px 1px rgba(0, 0, 0, 0.35); background: #fff; transform: rotate(-25deg); } .normal label:after { background: transparent; height: calc(100% + 8px); border-radius: 30px; top: -5px; width: calc(100% + 8px); left: -4px; z-index: 0; box-shadow: inset 0px 2px 4px -2px rgba(0, 0, 0, 0.2), 0px 1px 2px 0px rgba(151, 151, 151, 0.2); } #normal:checked + label { background: #4caf50; } #normal:checked + label:before { left: 67px; } .transparent label { background: transparent; border: 3px solid #fff; height: 3.35em; } .transparent label:before { border: 3px solid #fff; width: 2em; height: 2em; top: 0.3em; left: 0.3em; background: #fff; } #transparent:checked + label:before { transform: translateX(59px); } .checkcross label:before { content: none; } .checkcross .check { border-radius: 50%; width: 2.5em; height: 2.5em; position: absolute; background: #8BC34A; transition: 0.4s ease; top: 4.5px; left: 4.5px; } .checkcross .check:before, .checkcross .check:after { height: 4px; border-radius: 10px; background: #fff; transition: 0.4s ease; } .checkcross .check:before { width: 25px; transform: rotate(-45deg) translate(-6px, 20px); } .checkcross .check:after { width: 10px; transform: rotate(45deg) translate(20px, 11px); } #checkcross:checked + label .check { left: 68px; transform: rotate(360deg); background: #c34a4a; } #checkcross:checked + label .check:before { width: 27px; transform: rotate(-45deg) translate(-8px, 18px); } #checkcross:checked + label .check:after { width: 27px; transform: rotate(45deg) translate(18px, 8px); } #gravity:checked + label { transform: rotate(90deg); } #gravity:checked + label:before { transform: translateX(67px); transition-delay: 0.2s; transition: 0.6s cubic-bezier(0.895, 0.03, 0.685, 0.22) 0.2s; } .beer-pong label:before { background: #f9f9f9; box-shadow: inset 0 -3px 0 0 #c6c5c5; border: none; width: 2.5em; height: 2.5em; top: 0.25em; left: 0.25em; } .beer-pong .cup { top: -3%; right: -118px; border-top: 90px solid #f44336; border-left: 15px solid transparent; border-right: 15px solid transparent; height: 3px; position: absolute; width: 80px; transform-origin: bottom right; transition: 0.2s cubic-bezier(0.42, 0.5, 0.58, 1); } .beer-pong .cup:before { box-shadow: 0 -10px 0 0px rgba(39, 39, 39, 0.1), 0px -20px 0 0px rgba(39, 39, 39, 0.1); border-radius: 3px; overflow: hidden; background: rgba(39, 39, 39, 0.1); width: 120%; left: -5px; height: 4px; top: -40px; } .beer-pong .cup .lid { position: absolute; width: 95px; height: 8px; border-radius: 20px; background: #efefef; bottom: 86px; left: -23px; } .beer-pong .cup .lid:after { background: #efefef; width: 48px; height: 5px; left: 50%; margin-left: -24px; top: 94px; border-radius: 0 0 3px 3px; } #beer-pong:checked ~ .cup { animation: 0.2s linear cup 1s forwards; } #beer-pong:checked + label:before { animation: 2s linear bounce-off forwards; } @keyframes cup { 0% { transform: none; } 50% { transform: rotate(75deg) translate(10px, 15px); } 90% { transform: rotate(70deg) translate(10px, 15px); } 100% { transform: rotate(75deg) translate(10px, 15px); } } @keyframes bounce-off { 0% { transform: translateY(0); } 10%, 25% { transform: translate(-20px, -80px); } 50% { transform: rotate(163deg); transform-origin: 100px -12px; } 70% { transform: rotate(0) translate(-3px, -8px); transform-origin: 100px -12px; } 75% { transform: translate(20px, -8px); } 80% { transform: translate(30px, 0px); } 85% { transform: translate(40px, -3px); } 87% { transform: translate(46px, 0px); } 90% { transform: translate(52px, -1px); } 95% { transform: translate(60px, 0px); } 100% { transform: translate(64px, 0px); } } .dog-rollover label:before { content: none; } .dog-rollover label .dog { display: inline-block; position: absolute; width: 2.5em; height: 2.5em; top: 0.25em; left: 0.2em; transition: 0.6s ease; } .dog-rollover label .eyes { position: absolute; width: 8px; height: 8px; background: #222; border-radius: 50%; transform: translate(8px, 14px); box-shadow: 16px 0 0 #222, 22px -4px 0 12px #e4ac04; } .dog-rollover label .ear { width: 18px; height: 20px; position: absolute; left: -4px; bottom: 80%; background: #f9bb00; margin-bottom: -5px; border-radius: 50% 50% 0 0/100% 100% 0 0; box-shadow: inset 4px 0 0 0px #ffffff, inset -4px 0 0 0px #ffffff; transform: rotate(-40deg); } .dog-rollover label .ear.right { transform: rotate(60deg) scaleX(-1); left: auto; transform-origin: center bottom; transition: 0.4s ease-in-out; right: 0px; } .dog-rollover label .face { overflow: hidden; border-radius: 50%; width: 2.5em; height: 2.5em; position: absolute; background: #fff; z-index: 8; } .dog-rollover label .mouth { position: absolute; background: #222; width: 14px; height: 7px; left: 50%; margin-left: -7px; bottom: 12px; border-radius: 2px 2px 20px 20px; bottom: 8px; transform: scale(0); transition: 0.1s ease; } .dog-rollover label .mouth:before { width: 8px; height: 8px; background: #ec788d; border-radius: 0 0 50% 50%; transform: translate(3px, 5px); } .dog-rollover label:before { border-color: white; background: white; } #doggo:checked ~ .cup { animation: 0.2s linear cup 1s forwards; } #doggo:checked + label .dog { left: 68px; transform: rotate(360deg); } #doggo:checked + label .mouth { transform: scale(1); transition-delay: 0.7s; } #doggo:checked + label .ear.right { transform: scaleX(-1) rotate(-35deg); transition-delay: 0.6s; } .basketball-hoop label { background: #fdb827; } .basketball-hoop label:before { content: none; } .basketball-hoop .ball { border-radius: 50%; width: 2.5em; height: 2.5em; position: absolute; background: #FF9800; border: 2px solid black; transition: 0.4s ease; top: 4px; left: 4px; background-image: radial-gradient(circle at -5px 10px, transparent 15px, black 15px, black 17px, transparent 17px), radial-gradient(circle at 41px 25px, transparent 15px, black 15px, black 17px, transparent 17px), linear-gradient(110deg, transparent 22px, black 22px, black 24px, transparent 24px), linear-gradient(18deg, transparent 22px, black 22px, black 24px, transparent 24px); } .basketball-hoop .ball__wrapper { transition: 0.4s ease; width: 195%; height: 200%; transform-origin: 50% -2%; } .basketball-hoop .hoop { top: -50px; right: -84px; width: 50px; background: #f44336; height: 8px; position: absolute; } .basketball-hoop .hoop:before { position: absolute; right: -4px; width: 7px; height: 60px; background: #cd2e22; top: -40px; } .basketball-hoop .hoop:after { width: 40px; height: 35px; background: repeating-linear-gradient(45deg, transparent, transparent 13px, white 13px, white 15px), repeating-linear-gradient(-45deg, transparent, transparent 13px, white 13px, white 15px); top: 8px; border-radius: 0 0 20% 20%/40% 40% 60% 60%; border: 2px solid #fff; border-width: 0 2px; left: 2px; z-index: 20; } #hoop:checked + label { background: #542583; transition-delay: 1.35s; } #hoop:checked + label .ball__wrapper { animation: 1.5s linear ball-wrapper forwards; } #hoop:checked + label .ball { animation: 1.5s linear ball forwards; } @keyframes ball { 0% { transform: none; } 40% { transform: rotate(-150deg); } 48% { transform: rotate(-150deg) translateY(92px); } 52% { transform: rotate(-150deg) translate(-10px, 80px); } 56% { transform: rotate(-150deg) translate(-25px, 91px); } 60% { transform: rotate(-150deg) translate(-35px, 86px); } 65% { transform: rotate(-150deg) translate(-45px, 91px); } 70% { transform: rotate(-150deg) translate(-50px, 87px); } 75% { transform: rotate(-150deg) translate(-60px, 91px); } 80% { transform: rotate(-150deg) translate(-65px, 88px); } 85% { transform: rotate(-150deg) translate(-70px, 91px); } 90% { transform: rotate(-150deg) translate(-75px, 90px); } 95% { transform: rotate(-150deg) translate(-80px, 90px); } 100% { transform: rotate(-150deg) translate(-82px, 91px); } } @keyframes ball-wrapper { 0% { transform: none; } 40%, 100% { transform: rotate(150deg); } } .pancake-stack label:before { content: none; } .pancake-stack .pancakes { transition: 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275); } .pancake-stack .pancake { background: #e27c31; border-radius: 50%; width: 2.5em; height: 2.5em; position: absolute; transition: 0.4s ease; top: 2px; left: 4px; box-shadow: 0 2px 0 2px #fbbe7c; } .pancake-stack .pancake:nth-child(2) { left: 0; top: -3px; transform: scale(0); transition: 0.2s ease 0.2s; } .pancake-stack .pancake:nth-child(3) { top: -8px; transform: scale(0); transition: 0.2s ease 0.2s; } .pancake-stack .pancake:nth-child(3):before, .pancake-stack .pancake:nth-child(3):after { background: #ef8927; border-radius: 20px; width: 50%; height: 20%; } .pancake-stack .pancake:nth-child(3):before { top: 20px; left: 5px; } .pancake-stack .pancake:nth-child(3):after { top: 22px; right: 5px; } .pancake-stack .butter { width: 12px; height: 11px; background: #fbdb60; top: 6px; left: 20px; position: absolute; border-radius: 4px; box-shadow: 0 1px 0 1px #d67823; transform: scale(0); transition: 0.2s ease; } #pancake:checked + label .pancakes { transform: translateX(70px); } #pancake:checked + label .pancake:nth-child(2) { transform: scale(1); transition-delay: 0.2s; } #pancake:checked + label .pancake:nth-child(3) { transform: scale(1); transition-delay: 0.4s; } #pancake:checked + label .butter { transform: scale(1); transition-delay: 0.6s; } |
Download source code:
We have created a file and in this file, all the related code files are present for the development of these buttons in Html and CSS. The download link is given below and you can download the source code from here.
Task:
As you can see in the image, there are different types of toggle buttons available and they have custom and unique designs. Your job is to change the toggle style. We have used different and simple options for their development. Take a round image and then place that image in the place of buttons. Then switch the toggles button with the help of the image as a button.
I hope you like these toggle buttons very much. If so then you can like and share this post.
Thanks for reading this article.
Download Source CodeClick to Download