HTML5, CSS3 & JS Experiments

By Martin Ivanov

iPhone Toggle Switch

Demo

Are you interested in HTML5 Web Components? Check X-Switch - a similar toggle switch experiment, wrapped as a custom tag.

Checkboxes Example

Radiobuttons Example

Usage and Code Insight

                
        <p>Are you interested in <a href="html5-web-components.html" target="_blank">HTML5 Web Components</a>? 
            Check <a href="x-switch-html5-web-component.html" target="_blank">X-Switch</a> - a 
            similar toggle switch experiment, wrapped as a custom tag.</p>
        
<h4>Checkboxes Example</h4>
<div class="iphone-toggle-buttons">
    <ul>
        <li><label for="checkbox-0"><input type="checkbox" name="checkbox-0" id="checkbox-0" /><span>checkbox 0</span></label></li>
        <li><label for="checkbox-1"><input type="checkbox" name="checkbox-1" id="checkbox-1" checked="checked" /><span>checkbox 1</span></label></li>
    </ul>
</div>

<h4>Radiobuttons Example</h4>

<div class="iphone-toggle-buttons">
    <ul>
        <li><label for="radio-0"><input type="radio" name="radio-button-group-0" id="radio-0" checked="checked" /><span>radio 0</span></label></li>
        <li><label for="radio-1"><input type="radio" name="radio-button-group-0" id="radio-1" /><span>radio 1</span></label></li>
        <li><label for="radio-2"><input type="radio" name="radio-button-group-0" id="radio-2" /><span>radio 2</span></label></li>
    </ul>
</div>
    
.iphone-toggle-buttons,
.iphone-toggle-buttons *,
.iphone-toggle-buttons *::before,
.iphone-toggle-buttons *::after
{
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    box-sizing: content-box;
}

.iphone-toggle-buttons input[type="checkbox"] + span::after,
.iphone-toggle-buttons input[type="radio"] + span::after
{
    content: "off";
}

.iphone-toggle-buttons input[type="checkbox"]:checked + span::after,
.iphone-toggle-buttons input[type="radio"]:checked + span::after
{
    content: "on";
}

.iphone-toggle-buttons
{
    -moz-user-select: none;
    -webkit-user-select: none;
    user-select: none;
}

.iphone-toggle-buttons
{
    margin: 4px 0;
}

.iphone-toggle-buttons label,
.iphone-toggle-buttons input[type="checkbox"] + span,
.iphone-toggle-buttons input[type="radio"] + span,
.iphone-toggle-buttons input[type="checkbox"] + span::before,
.iphone-toggle-buttons input[type="radio"] + span::before,
.iphone-toggle-buttons input[type="checkbox"] + span::after,
.iphone-toggle-buttons input[type="radio"] + span::after
{
    display: inline-block;
}

.iphone-toggle-buttons label,
.iphone-toggle-buttons input[type="checkbox"] + span,
.iphone-toggle-buttons input[type="radio"] + span
{
    vertical-align: middle;
}

.iphone-toggle-buttons input[type="checkbox"] + span,
.iphone-toggle-buttons input[type="radio"] + span,
.iphone-toggle-buttons input[type="checkbox"] + span::before,
.iphone-toggle-buttons input[type="radio"] + span::before,
.iphone-toggle-buttons input[type="checkbox"] + span::after,
.iphone-toggle-buttons input[type="radio"] + span::after
{
    -moz-border-radius: 4px;
    -webkit-border-radius: 4px;
    border-radius: 4px;
}

.iphone-toggle-buttons input[type="checkbox"] + span::before,
.iphone-toggle-buttons input[type="radio"] + span::before,
.iphone-toggle-buttons input[type="checkbox"] + span::after,
.iphone-toggle-buttons input[type="radio"] + span::after,
.iphone-toggle-buttons input[type="checkbox"]:checked + span::before,
.iphone-toggle-buttons input[type="radio"]:checked + span::before,
.iphone-toggle-buttons input[type="checkbox"]:checked + span::after,
.iphone-toggle-buttons input[type="radio"]:checked + span::after
{
    top: 0;
}

.iphone-toggle-buttons input[type="checkbox"] + span::after,
.iphone-toggle-buttons input[type="radio"] + span::after,
.iphone-toggle-buttons input[type="checkbox"]:checked + span::before,
.iphone-toggle-buttons input[type="radio"]:checked + span::before
{
    right: 0;
}

.iphone-toggle-buttons input[type="checkbox"] + span::before,
.iphone-toggle-buttons input[type="radio"] + span::before,
.iphone-toggle-buttons input[type="checkbox"]:checked + span::after,
.iphone-toggle-buttons input[type="radio"]:checked + span::after
{
    left: 0;
}

.iphone-toggle-buttons input[type="checkbox"],
.iphone-toggle-buttons input[type="radio"],
.iphone-toggle-buttons input[type="checkbox"] + span::before,
.iphone-toggle-buttons input[type="radio"] + span::before,
.iphone-toggle-buttons input[type="checkbox"] + span::after,
.iphone-toggle-buttons input[type="radio"] + span::after
{
    position: absolute;
}

.iphone-toggle-buttons label
{
    margin: 4px;
    cursor: pointer;
}

.iphone-toggle-buttons input[type="checkbox"],
.iphone-toggle-buttons input[type="radio"]
{
    filter: alpha(opacity=0);
    -moz-opacity: 0;
    -webkit-opacity: 0;
    opacity: 0;
}

.iphone-toggle-buttons input[type="checkbox"] + span,
.iphone-toggle-buttons input[type="radio"] + span
{
    width: 100px;
    height: 30px;
    font: bold 14px/30px Arial, Sans-serif;
    color: #8c8c8c;
    text-transform: uppercase;
    border: solid 1px #bcbbbb;
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#c8c8c8", endColorstr="#f3f3f3");
    background: -moz-linear-gradient(top, #c8c8c8, #f3f3f3);
    background: -webkit-linear-gradient(top, #c8c8c8, #f3f3f3);
    background: -o-linear-gradient(top, #c8c8c8, #f3f3f3);
    background: -ms-linear-gradient(top, #c8c8c8, #f3f3f3);
    background: linear-gradient(top, #c8c8c8, #f3f3f3);
    position: relative;
    text-indent: -9999px;
}

.iphone-toggle-buttons input[type="checkbox"] + span::before,
.iphone-toggle-buttons input[type="radio"] + span::before
{
    content: "";
    width: 40%;
    height: 29px;
    border-top: solid 1px #fff;
    border-right: solid 1px #bebebe;
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#cfcfcf", endColorstr="#f9f9f9");
    background: -moz-linear-gradient(top, #cfcfcf, #f9f9f9);
    background: -webkit-linear-gradient(top, #cfcfcf, #f9f9f9);
    background: -o-linear-gradient(top, #cfcfcf, #f9f9f9);
    background: -ms-linear-gradient(top, #cfcfcf, #f9f9f9);
    background: linear-gradient(top, #cfcfcf, #f9f9f9);
    -moz-box-shadow: 1px 0 1px #bebebe;
    -webkit-box-shadow: 1px 0 1px #bebebe;
    box-shadow: 1px 0 1px #bebebe;
}

.iphone-toggle-buttons input[type="checkbox"] + span::after,
.iphone-toggle-buttons input[type="radio"] + span::after
{
    text-indent: 0;
    width: 62%;
    height: 32px;
    text-align: center;
}

.iphone-toggle-buttons input[type="checkbox"]:checked + span,
.iphone-toggle-buttons input[type="radio"]:checked + span
{
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#1b45bd", endColorstr="#5d96ea");
    background: -moz-linear-gradient(top, #1b45bd, #5d96ea);
    background: -webkit-linear-gradient(top, #1b45bd, #5d96ea);
    background: -o-linear-gradient(top, #1b45bd, #5d96ea);
    background: -ms-linear-gradient(top, #1b45bd, #5d96ea);
    background: linear-gradient(top, #1b45bd, #5d96ea);
    color: #fff;
    text-shadow: -1px -1px #0d2046;
}

.iphone-toggle-buttons input[type="checkbox"]:checked + span::before,
.iphone-toggle-buttons input[type="radio"]:checked + span::before
{
    left: auto;
    -moz-box-shadow: -2px 0 1px #3a5e91;
    -webkit-box-shadow: -2px 0 1px #3a5e91;
    box-shadow: -2px 0 1px #3a5e91;
}

.iphone-toggle-buttons input[type="checkbox"]:checked + span::after,
.iphone-toggle-buttons input[type="radio"]:checked + span::after
{
    border-top: solid 1px #0f2a4f;
    border-bottom: solid 1px #0f2a4f;
    border-left: solid 1px #2c5496;
    height: 30px;
    top: -1px;
    left: -1px;
    -moz-border-radius: 4px 0 0 4px;
    -webkit-border-radius: 4px 0 0 4px;
    border-radius: 4px 0 0 4px;
}

.browser-support li.ie6,
.browser-support li.ie7,
.browser-support li.ie8
{
    display: none;
}