Show DevBest On/off toggle buttons

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Not coded in a while and tried to do something similar to the toggle buttons on iOS, got far enough then got bored.

There's no parent element selector in CSS and I can't be bothered to use JavaScript to make the background colour of the onofftoggle element change to green when checked.

Anyway, you can view the preview here:

And code is below:
HTML:
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
            body { padding: 60px; }

            .onofftoggle {
                width: 80px;
                border: 1px solid #ccc;
                border-radius: 25px;
                height: 25px;
                position: relative;
                background-color: #f7f7f7;
            }
           
                .onofftoggle input[type="checkbox"] {
                    display: none;
                }
           
                .onofftoggle input[type="checkbox"] + .onoffbutton {
                    display: block;
                    background-color: #fff;
                    border: 1px solid #ccc;
                    border-radius: 25px;
                    width: 25px;
                    height: 90%;
                    max-height: 23px;
                    box-shadow: 0 2px 4px rgba(0, 0, 0, .3);
                    cursor: pointer;
                    position: absolute;
                    left: 0;
                    -webkit-transition: left .4s ease-in-out;
                            transition: left .4s ease-in-out;
                }
           
                .onofftoggle input[type="checkbox"]:checked + .onoffbutton {
                    left: calc(100% - 25px);
                }
        </style>
    </head>

    <body>
        <div class="onofftoggle">
            <label>
                <input type="checkbox" name="check1" />
                <span class="onoffbutton"></span>
            </label>
        </div>
       
        <div class="onofftoggle">
            <label>
                <input type="checkbox" name="check2" />
                <span class="onoffbutton"></span>
            </label>
        </div>
       
        <div class="onofftoggle">
            <label>
                <input type="checkbox" name="check3" />
                <span class="onoffbutton"></span>
            </label>
        </div>
    </body>
</html>

Hope it helps if you go with it,
Mark.
 

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
Nice one bud, haven't seen a demo or whatever but its from you so it'll be working well. I also took a similar way of doing this to get a similar result - it's one of them things that is fiddily to achieve.
 

griimnak

You're a slave to the money then you die
Jul 20, 2013
956
797
i'll probably use this if i plan on making a mobile skin for my site, cheers dude
 

xhilaration

New Member
Mar 14, 2015
30
5
When you turn the toggle switch to 'On', can you put a color to indicate it showing being on?

Like can you put a hex color code in the toggle switch code??

---------------------------
Example:
On-Pink (#D90C57)
Off-Dark Gray (#4E4A4B)
 

D4ll32

New Member
Nov 1, 2014
6
0
This is super good for a website where you have to do lots of turn on/off! 10/10 because it's not with javascript!
 

Users who are viewing this thread

Top