LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » CSS Codes / HTML-CSS-PHP-JavaScript » Android Style Toggle Switch Checkbox For Web In Pure CSS

Android Style Toggle Switch Checkbox For Web In Pure CSS

Android-Style-Toggle-Switch-Checkbox-For-Web-In-Pure-CSS
If you are into web development you will for sure know what a “toggle switch” is. There’s almost no website or web application that is not using them nowadays.

It’s a design element that provides the users with a way to choose between two different states. Commonly used in websites, mobile apps, and other software. When used in web development, a toggle switch is not a native element so developers have come up with ways to mimic this element by using only HTML and CSS. Usually, a hidden checkbox is used under the hood to keep track of the state of the toggle switch.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure CSSCode.
  3. Cross Browser.
  4. No External Files.
  5. Fully Customizable.
  6. Responsive.
Recommended For You:
15 Stylish And Attractive Custom CSS For Scroll Bar Using CSS3

How To Add Android Style Toggle Switch Checkbox For Web In Pure CSS?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

CSS:

input[type="checkbox"].togglebtn {
    display: none;
}
label.togglebtn {
    display: inline-block;
    width: 40px;
    height: 20px;
    border-radius: 20px;
    vertical-align: middle;
    margin: 0px 10px;
    text-align: center;
    background-color: #ec7a7a;
}
label.togglebtn > span.indicator {
    border-radius: 50%;
    display: inline-block;
    position: relative;
    height: 25px;
    width: 25px;
    top: -2.5px;
    left: -10px;
    background-color: #da3e3e;
    box-shadow: 2px 2px 4px #444;
    transition: left 200ms ease-out;
}
input.togglebtn:checked + label.togglebtn > span.indicator {
    left: 10px;
    background-color: #63cf71;
}
input.togglebtn:checked + label.togglebtn:not(.disabled).conditional ~ label.conditional.off,
input.togglebtn:not(:checked) + label.togglebtn.conditional ~ label.conditional.on {
    display: none;
}
input.togglebtn:checked + label.togglebtn:not(.disabled) {
    background-color: #a2dfaa;
}
label.togglebtn.disabled {
    background-color: #a3a3a3;
}
label.togglebtn.disabled > span.indicator {
    background-color: #7c7c7c !important;
}

HTML:

<p class="lead">A CSS library for creating Android-style toggle switches using pure CSS.</p>

        <h2>Demo #1: <span class="explanation">Toggle only</span></h2>
        <input type="checkbox" id="demo1" class="togglebtn" autocomplete="off"/>
        <label for="demo1" class="togglebtn"><span class="indicator"></span></label>

        <hr/>

        <h2>Demo #2: <span class="explanation">Toggle with label after</span></h2>
        <input type="checkbox" id="demo2" class="togglebtn" autocomplete="off"/>
        <label for="demo2" class="togglebtn"><span class="indicator"></span></label>
        <label for="demo2">Leat Fingies</label>

        <hr/>

        <h2>Demo #3: <span class="explanation">Toggle with label before</span></h2>
        <label for="demo3">Bimgus</label>
        <input type="checkbox" id="demo3" class="togglebtn" autocomplete="off"/>
        <label for="demo3" class="togglebtn"><span class="indicator"></span></label>

        <hr/>

        <h2>Demo #4: <span class="explanation">Toggle with conditional label (only works if label is after toggle)</span></h2>
        <input type="checkbox" id="demo4" class="togglebtn" autocomplete="off"/>
        <label for="demo4" class="togglebtn conditional"><span class="indicator"></span></label>
        <label for="demo4" class="conditional on">Leat Fingies</label>
        <label for="demo4" class="conditional off">Bimgus</label>

        <hr/>

        <h2>Demo #5: <span class="explanation">Disabled Toggle</span></h2>
        <label for="demo5-disabler">Disable next toggle</label>
        <input type="checkbox" id="demo5-disabler" class="togglebtn" autocomplete="off" onchange="toggleDisableCheckbox('demo5-disabled')"/>
        <label for="demo5-disabler" class="togglebtn"><span class="indicator"></span></label>
        <input type="checkbox" id="demo5-disabled" class="togglebtn" autocomplete="off"/>
        <label for="demo5-disabled" class="togglebtn"><span class="indicator"></span></label>

JavaScript:

<script>
/*
* Adds and removes disabled class from given togglebtn
*/
            function toggleDisableCheckbox(id) {
                let elem = document.getElementById(id);
                elem.disabled = !elem.disabled;
                document.querySelector("label[for=" + id + "].togglebtn").classList.toggle("disabled");
            } 
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need. Remember to add JavaScript after HTML code.

Recommended For You:
Stop Multiple Keyboard Keys Functioning Using Pure JavaScript

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps and again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *