CSS Toggle example

HTML
<div id="main"> <div class="container"> <div class="settings"> <div class="row"> <div class="question"> Do you like racoons? </div> <div class="switch"> <input id="cmn-toggle-1" class="cmn-toggle cmn-toggle-round" type="checkbox"> <label for="cmn-toggle-1"></label> </div> </div><!-- /row --> <div class="row"> <div class="question"> Do you believe in love after life? </div> <div class="switch"> <input id="cmn-toggle-2" class="cmn-toggle cmn-toggle-round" type="checkbox"> <label for="cmn-toggle-2"></label> </div> </div><!-- /row --> <div class="row"> <div class="question"> Surely you must like tomatoes? </div> <div class="switch"> <input id="cmn-toggle-3" class="cmn-toggle cmn-toggle-round" type="checkbox"> <label for="cmn-toggle-3"></label> </div> </div><!-- /row --> <div class="row"> <div class="question"> Do you like tomatoes? </div> <div class="switch"> <input id="cmn-toggle-4" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"> <label for="cmn-toggle-4"></label> </div> </div><!-- /row --> <div class="row"> <div class="question"> Do you like surfing? </div> <div class="switch"> <input id="cmn-toggle-5" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"> <label for="cmn-toggle-5"></label> </div> </div><!-- /row --> <div class="row"> <div class="question"> What about traveling? </div> <div class="switch"> <input id="cmn-toggle-6" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"> <label for="cmn-toggle-6"></label> </div> </div><!-- /row --> <div class="row"> <div class="question"> Are you any good at math? </div> <div class="switch"> <input id="cmn-toggle-7" class="cmn-toggle cmn-toggle-yes-no" type="checkbox"> <label for="cmn-toggle-7" data-on="Yes" data-off="No"></label> </div> </div><!-- /row --> <div class="row"> <div class="question"> What about science and stuff? </div> <div class="switch"> <input id="cmn-toggle-8" class="cmn-toggle cmn-toggle-yes-no" type="checkbox"> <label for="cmn-toggle-8" data-on="Yup!" data-off="Um..."></label> </div> </div><!-- /row --> <div class="row"> <div class="question"> Are you happy? </div> <div class="switch"> <input id="cmn-toggle-9" class="cmn-toggle cmn-toggle-yes-no" type="checkbox"> <label for="cmn-toggle-9" data-on="Hell yeah!" data-off="Not really?"></label> </div> </div><!-- /row --> </div> </div> </div><!-- #main -->
CSS
/* ============================================================ RESET ============================================================ */ html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td { background: transparent; border: 0; margin: 0; padding: 0; vertical-align: baseline; } body { line-height: 1; } h1, h2, h3, h4, h5, h6 { clear: both; font-weight: normal; } ol, ul { list-style: none; } blockquote { quotes: none; } blockquote:before, blockquote:after { content: ''; content: none; } del { text-decoration: line-through; } /* tables still need 'cellspacing="0"' in the markup */ table { border-collapse: collapse; border-spacing: 0; } a img { border: none; } /* ============================================================ GLOBALS ============================================================ */ *, *:before, *:after { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } body { background-color: #dc8c96; color: #505050; font-family: "Ubuntu", sans-serif; font-weight: 300; font-size: 16px; line-height: 1.8; } /* Headings */ h1, h2, h3, h4, h5, h6 { line-height: 1; font-weight: 400; font-family: "Roboto Slab", serif; } a { text-decoration: none; color: #dc8c96; } a:hover { color: #ca5160; } /* clear floated divs */ .clearfix:after { content: ""; display: table; clear: both; } /* ============================================================ TEMPLATE ============================================================ */ #wrapper { width: 100%; margin: 0 auto; } #main { background-color: #fff; padding: 30px 0; } .container { width: 100%; max-width: 1000px; margin: 0 auto; padding: 0 30px; } /* ============================================================ INFO BAR ============================================================ */ .info-bar { background-color: #ca5160; text-align: center; } .info-bar a.icon { margin: 0 5px; padding: 10px 0; color: #fff; font-size: 20px; position: relative; } .info-bar a.icon[data-title]:hover:after { position: absolute; top: 100%; left: 0; z-index: 20px; content: attr(data-title); padding: 5px 10px; background-color: #ca5160; font-family: "Ubuntu", sans-serif; color: #fff; font-size: 12px; white-space: nowrap; } /* ============================================================ HEADER ============================================================ */ header .branding { padding: 30px 0; background-color: #dc8c96; } header .branding .container { padding: 0 30px 0 90px; } header .social { float: left; width: 100%; height: 60px; text-align: right; line-height: 60px; } header .social a { display: inline-block; text-indent: -9999px; font-size: 0; margin: 15px 0; width: 30px; height: 30px; background-size: 30px 30px; margin-left: 5px; background-image: url("../img/core/social-light.png"); background-image: url("../img/core/social-light.svg"), none; background-size: 180px 30px; } header .social a.fb { background-position: 0 0; } header .social a.twitter { background-position: -30px 0; } header .social a.googleplus { background-position: -60px 0; } header .social a.rss { background-position: -90px 0; } header .social a.email { background-position: -120px 0; } header .social a.search { background-position: -150px 0; } header .social a:hover { opacity: 0.4; } header .logo { float: left; width: 60px; height: 60px; margin-left: -60px; } header .site-title { background-color: #ca5160; padding: 10px 30px; text-align: center; } header .site-title h1 { margin: 0; padding: 0; color: #fff; font-size: 12px; } /* ============================================================ FOOTER ============================================================ */ footer { padding: 30px 0; } footer .container { padding: 0 30px; } footer aside { width: 33.3333%; float: left; } footer aside.logo { text-align: right; } footer ul { list-style-type: none; margin: 0; padding: 0; } footer ul a { color: #fff; } footer ul a:hover { color: #f4dadd; } footer .copyright { margin-top: 30px; text-align: center; } footer .copyright span { color: #fff; } footer .copyright a { color: #f4dadd; } /* ============================================================ FUSION ADS ============================================================ */ #fusionads { position: fixed; top: 10px; right: 10px; z-index: 20; padding: 20px; background: rgba(255, 255, 255, 0.95); font-size: 12px; line-height: 1.2; -webkit-box-shadow: -2px 2px 4px rgba(0, 0, 0, 0.1); -moz-box-shadow: -2px 2px 4px rgba(0, 0, 0, 0.1); box-shadow: -2px 2px 4px rgba(0, 0, 0, 0.1); } #fusionads .fusion-wrap { display: block; margin: 0 auto 10px auto; width: 130px; } #fusionads a.fusion-text { display: block; color: #646464; } #fusionads a.fusion-img { display: block; margin-bottom: 10px; width: 130px; height: 100px; background-color: #fff; } #fusionads a.fusion-img img { display: block; margin: 0 0 10px 0; } #fusionads a.fusion-poweredby { color: #ff3c50; } #close-fusionad { position: fixed; top: 10px; right: 10px; z-index: 30; width: 20px; height: 20px; background-color: #505050; text-align: center; line-height: 20px; color: #fff; font-size: 12px; font-weight: 700; } /* ============================================================ MEDIA QUERIES ============================================================ */ @media all and (max-width: 960px) { /* footer */ footer aside { width: 100%; float: none; margin-bottom: 15px; } footer aside:last-child { margin-bottom: 0; } footer aside.logo { text-align: center; } footer ul { text-align: center; } footer ul li { display: inline-block; } footer ul li:after { content: "\2022"; } footer ul li:last-child:after { content: none; } footer ul li a { margin: 0 10px; } } @media all and (max-width: 400px) { header .branding { padding: 15px 0; } header .branding .container { padding: 0 30px; } header .social { float: none; width: 100%; text-align: center; line-height: 30px; height: 30px; } header .social a { margin: 0 5px; } header .logo { float: none; margin: 0 auto; margin-bottom: 15px; } } /* ============================================================ COMMON ============================================================ */ #wrapper { min-width: 600px; } .settings { display: table; width: 100%; } .settings .row { display: table-row; } .settings .question, .settings .switch { display: table-cell; vertical-align: middle; padding: 10px; } .settings .question { width: 600px; font-family: "Roboto Slab", serif; font-size: 20px; } /* ============================================================ COMMON ============================================================ */ .cmn-toggle { position: absolute; margin-left: -9999px; visibility: hidden; } .cmn-toggle + label { display: block; position: relative; cursor: pointer; outline: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } /* ============================================================ SWITCH 1 - ROUND ============================================================ */ input.cmn-toggle-round + label { padding: 2px; width: 120px; height: 60px; background-color: #dddddd; -webkit-border-radius: 60px; -moz-border-radius: 60px; -ms-border-radius: 60px; -o-border-radius: 60px; border-radius: 60px; } input.cmn-toggle-round + label:before, input.cmn-toggle-round + label:after { display: block; position: absolute; top: 1px; left: 1px; bottom: 1px; content: ""; } input.cmn-toggle-round + label:before { right: 1px; background-color: #f1f1f1; -webkit-border-radius: 60px; -moz-border-radius: 60px; -ms-border-radius: 60px; -o-border-radius: 60px; border-radius: 60px; -webkit-transition: background 0.4s; -moz-transition: background 0.4s; -o-transition: background 0.4s; transition: background 0.4s; } input.cmn-toggle-round + label:after { width: 58px; background-color: #fff; -webkit-border-radius: 100%; -moz-border-radius: 100%; -ms-border-radius: 100%; -o-border-radius: 100%; border-radius: 100%; -webkit-box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3); -moz-box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3); box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3); -webkit-transition: margin 0.4s; -moz-transition: margin 0.4s; -o-transition: margin 0.4s; transition: margin 0.4s; } input.cmn-toggle-round:checked + label:before { background-color: #8ce196; } input.cmn-toggle-round:checked + label:after { margin-left: 60px; } /* ============================================================ SWITCH 2 - ROUND FLAT ============================================================ */ input.cmn-toggle-round-flat + label { padding: 2px; width: 120px; height: 60px; background-color: #dddddd; -webkit-border-radius: 60px; -moz-border-radius: 60px; -ms-border-radius: 60px; -o-border-radius: 60px; border-radius: 60px; -webkit-transition: background 0.4s; -moz-transition: background 0.4s; -o-transition: background 0.4s; transition: background 0.4s; } input.cmn-toggle-round-flat + label:before, input.cmn-toggle-round-flat + label:after { display: block; position: absolute; content: ""; } input.cmn-toggle-round-flat + label:before { top: 2px; left: 2px; bottom: 2px; right: 2px; background-color: #fff; -webkit-border-radius: 60px; -moz-border-radius: 60px; -ms-border-radius: 60px; -o-border-radius: 60px; border-radius: 60px; -webkit-transition: background 0.4s; -moz-transition: background 0.4s; -o-transition: background 0.4s; transition: background 0.4s; } input.cmn-toggle-round-flat + label:after { top: 4px; left: 4px; bottom: 4px; width: 52px; background-color: #dddddd; -webkit-border-radius: 52px; -moz-border-radius: 52px; -ms-border-radius: 52px; -o-border-radius: 52px; border-radius: 52px; -webkit-transition: margin 0.4s, background 0.4s; -moz-transition: margin 0.4s, background 0.4s; -o-transition: margin 0.4s, background 0.4s; transition: margin 0.4s, background 0.4s; } input.cmn-toggle-round-flat:checked + label { background-color: #8ce196; } input.cmn-toggle-round-flat:checked + label:after { margin-left: 60px; background-color: #8ce196; } /* ============================================================ SWITCH 3 - YES NO ============================================================ */ input.cmn-toggle-yes-no + label { padding: 2px; width: 120px; height: 60px; } input.cmn-toggle-yes-no + label:before, input.cmn-toggle-yes-no + label:after { display: block; position: absolute; top: 0; left: 0; bottom: 0; right: 0; color: #fff; font-family: "Roboto Slab", serif; font-size: 20px; text-align: center; line-height: 60px; } input.cmn-toggle-yes-no + label:before { background-color: #dddddd; content: attr(data-off); -webkit-transition: -webkit-transform 0.5s; -moz-transition: -moz-transform 0.5s; -o-transition: -o-transform 0.5s; transition: transform 0.5s; -webkit-backface-visibility: hidden; -moz-backface-visibility: hidden; -ms-backface-visibility: hidden; -o-backface-visibility: hidden; backface-visibility: hidden; } input.cmn-toggle-yes-no + label:after { background-color: #8ce196; content: attr(data-on); -webkit-transition: -webkit-transform 0.5s; -moz-transition: -moz-transform 0.5s; -o-transition: -o-transform 0.5s; transition: transform 0.5s; -webkit-transform: rotateY(180deg); -moz-transform: rotateY(180deg); -ms-transform: rotateY(180deg); -o-transform: rotateY(180deg); transform: rotateY(180deg); -webkit-backface-visibility: hidden; -moz-backface-visibility: hidden; -ms-backface-visibility: hidden; -o-backface-visibility: hidden; backface-visibility: hidden; } input.cmn-toggle-yes-no:checked + label:before { -webkit-transform: rotateY(180deg); -moz-transform: rotateY(180deg); -ms-transform: rotateY(180deg); -o-transform: rotateY(180deg); transform: rotateY(180deg); } input.cmn-toggle-yes-no:checked + label:after { -webkit-transform: rotateY(0); -moz-transform: rotateY(0); -ms-transform: rotateY(0); -o-transform: rotateY(0); transform: rotateY(0); }
JAVASCRIPT
Expand for more options Login