Skip to content

Instantly share code, notes, and snippets.

@Seppala
Created April 8, 2012 17:09
Show Gist options
  • Save Seppala/2338539 to your computer and use it in GitHub Desktop.
Save Seppala/2338539 to your computer and use it in GitHub Desktop.
iOS like Html, CSS only Toggle button
/*
* CSS for creating the toggle button
*/
/* Micro clearfix (http://nicolasgallagher.com/micro-clearfix-hack/) */
.cf:before,.cf:after{content:"";display:table}.cf:after{clear:both}.cf{zoom:1}
/* = Function = */
.mx-button {
float: left;
}
.mx-button input {
display: none;
}
.mx-button label {
background: #ccc;
border: 1px solid #888;
color: #666;
padding: 5px 10px;
}
.mx-button label:hover {
background-color: #ddd;
}
.mx-button label:active,
.mx-button input:focus + label {
background-color: #aaa;
}
.mx-button input:checked + label {
background-color: #b4b4b4;
}
/* = Fancy (Twitter Bootstrap) = */
form {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: 80px;
}
.mx-button label {
background-color: #f5f5f5;
background-image: -webkit-linear-gradient(top, #ffffff, #e6e6e6);
background-image: -moz-linear-gradient(top, #ffffff, #e6e6e6);
background-image: -o-linear-gradient(top, #ffffff, #e6e6e6);
background-image: -ms-linear-gradient(top, #ffffff, #e6e6e6);
background-image: linear-gradient(top, #ffffff, #e6e6e6);
background-repeat: repeat-x;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#e6e6e6', GradientType=0);
border-color: #e6e6e6 #e6e6e6 #bfbfbf;
border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
border: 1px solid #cccccc;
border-bottom-color: #b3b3b3;
-webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
-moz-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
color: #333333;
cursor: pointer;
display: inline-block;
margin-bottom: 0;
margin-left: -1px;
padding: 4px 10px;
font-size: 13px;
line-height: 18px;
text-align: center;
text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
vertical-align: middle;
}
.mx-button label:hover {
background-color: #e6e6e6;
background-position: 0 -15px;
color: #333333;
text-decoration: none;
-webkit-transition: background-position 0.1s linear;
-moz-transition: background-position 0.1s linear;
-ms-transition: background-position 0.1s linear;
-o-transition: background-position 0.1s linear;
transition: background-position 0.1s linear;
}
.mx-button label:active,
.mx-button input:focus + label {
background-color: #d9d9d9;
background-image: none;
-webkit-box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.05);
-moz-box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.05);
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.05);
outline: 0;
}
.mx-button input:checked + label {
background: #006dcc;
text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
color: #ffffff;
/*background: #f5f5f5;*/
-webkit-box-shadow: inset 0 1px 6px rgba(0, 109, 204, 0.15), 0 1px 2px rgba(0, 109, 204, 0.05);
-moz-box-shadow: inset 0 1px 6px rgba(0, 109, 204, 0.15), 0 1px 2px rgba(0, 109, 204, 0.05);
box-shadow: inset 0 1px 6px rgba(0, 109, 204, 0.15), 0 1px 2px rgba(0, 109, 204, 0.05);
border-left-color: rgba(0, 109, 204, 0.15);
border-top-color: rgba(0, 109, 204, 0.15);
border-right-color: rgba(0, 109, 204, 0.15);
border-bottom-color: rgba(0, 109, 204, 0.15);
}
.mx-button:first-child label {
-webkit-border-top-left-radius: 4px;
-moz-border-radius-topleft: 4px;
border-top-left-radius: 4px;
-webkit-border-bottom-left-radius: 4px;
-moz-border-radius-bottomleft: 4px;
border-bottom-left-radius: 4px;
margin-left: 0;
}
.mx-button:last-child label {
-webkit-border-top-right-radius: 4px;
-moz-border-radius-topright: 4px;
border-top-right-radius: 4px;
-webkit-border-bottom-right-radius: 4px;
-moz-border-radius-bottomright: 4px;
border-bottom-right-radius: 4px;
}​
<!-- Adapted from http://complexcompulsions.com/toggle-buttons-without-javascript/ -->
<form>
<div class="mx-button">
<input type="radio" name="mx" id="button1" value="yes" checked>
<label for="button1" unselectable>Do</label>
</div>
<div class="mx-button">
<input type="radio" name="mx" id="button2" value="no">
<label for="button2" unselectable>Don't</label>
</div>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment