Heart Toggle Switch with HTML and CSS
Creating a Heart Toggle Switch with HTML and CSS
In this tutorial, we’ll guide you through the process of creating an eye-catching heart-shaped toggle switch using HTML and CSS. The toggle switch will not only change its appearance but also become active when turned on and off. It’s a fun and creative way to add interactivity to your web projects. Here we have also given you HTML and CSS source code. So, let’s get started!
Heart Toggle Switch Preview;
Before we get into the code, let’s take a look at what we’ll be building:
The HTML Structure;
This is HTML Structure for creating the structure of Heart Toggle Switch.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Heart Toggle</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="love"> Â <input id="switch" type="checkbox"> Â <label class="love-heart" for="switch"> Â Â <i class="left"></i> Â Â <i class="right"></i> Â Â <i class="bottom"></i> <div class="round"></div> Â </label> </div> </body> </html>
The CSS Styles
Create a file named styles.css in the same directory as your HTML file and add the following CSS code:
/* Reset some default styles */ body, html { Â margin: 0; Â padding: 0; } /* Center the toggle switch on the page */ body { Â display: flex; Â align-items: center; Â justify-content: center; Â height: 100vh; Â background-color: rgb(60, 2, 104); } /* Hide the default checkbox and heart */ .love-heart:before, #switch { Â display: none; } /* Heart-shaped toggle switch styles */ .love-heart, .love-heart::after { Â /* Add border and dimensions */ Â border-color: hsl(231deg 28% 86%); Â border: 1px solid; Â border-top-left-radius: 100px; Â border-top-right-radius: 100px; Â width: 10px; Â height: 8px; Â border-bottom: 0; } /* Positioning for the animated round indicator */ .round { Â position: absolute; Â z-index: 1; Â width: 8px; Â height: 8px; Â background: hsl(0deg 0% 100%); Â box-shadow: rgb(0 0 0 / 24%) 0px 0px 4px 0px; Â border-radius: 100%; Â left: 0px; Â bottom: -1px; Â transition: all .5s ease; Â animation: check-animation2 .5s forwards; } /* Styles for when the toggle switch is checked */ input:checked + label .round { Â transform: translate(0px, 0px); Â animation: check-animation .5s forwards; Â background-color: hsl(0deg 0% 100%); } /* Keyframe animations for the round indicator */ @keyframes check-animation { Â 0% { Â Â Â transform: translate(0px, 0px); } Â 50% { Â Â Â transform: translate(0px, 7px); } Â 100% { Â Â Â transform: translate(7px, 7px); Â } } @keyframes check-animation2 { Â 0% { Â Â Â transform: translate(7px, 7px); } Â 50% { Â Â Â transform: translate(0px, 7px); } Â 100% { Â Â Â transform: translate(0px, 0px); } } /* Additional heart and checked styles */ .love-heart { Â Â /* Position and scaling for the heart shape */ Â box-sizing: border-box; Â position: relative; Â transform: rotate(-45deg) translate(-50%, -33px) scale(4); Â display: block; Â border-color: hsl(231deg 28% 86%); Â cursor: pointer; Â top: 0; } /* Styles for when the toggle switch is checked */ input:checked +.love-heart, input:checked +.love-heart::after, input:checked +.love-heart.bottom { Â border-color: hsl(347, 86%, 44%); Â box-shadow: inset 6px -5px 0px 2px hsl(347, 63%, 40%); } /* Styles for the bottom part of the heart */ .love-heart::after, .love-heart .bottom { Â content: ""; Â display: block; Â box-sizing: border-box; Â position: absolute; Â border-color: hsl(231deg 28% 86%); } /* Positioning and dimensions for the bottom part */ .love-heart::after { Â right: -9px; Â transform: rotate(90deg); Â top: 7px; } .love-heart .bottom { Â width: 11px; Â height: 11px; Â border-left: 1px solid; Â border-bottom: 1px solid; Â border-color: hsl(231deg 28% 86%); Â left: -1px; Â top: 5px; Â border-radius: 0px 0px 0px 5px; }
So, here is the HTML and CSS style code, the source code for creating the heart toggle switch. And if you want to learn HTML and CSS then our free Tutorial is available in website from where you can learn.
In short, the provided CSS code creates an interactive heart-shaped toggle switch with customized styling, animations, and responsive behavior. It combines various CSS techniques, including flexbox for layout, keyframe animations for dynamic movement, and pseudo-elements for creating heart shapes and animations. This code can be easily integrated into your web projects to add an entertaining and engaging element for users to interact with.
Conclusion;
Congratulations! You’ve successfully created a delightful heart-shaped toggle switch using HTML and CSS. This creative element can add a touch of interactivity and charm to your web projects. Feel free to customize the colors, animations, and dimensions to match your project’s design. You can integrate this toggle switch into various parts of your website, such as settings panels, forms, or interactive widgets. Happy coding!
Look Also;
0 Comments