Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>see if <a href="http://jsfiddle.net/centerwow/2kkHp/7/" rel="nofollow">this JSFiddle code</a> helps you.</p> <pre><code>&lt;p&gt;&lt;b&gt;Disclaimer:&lt;/b&gt; Due to a webkit bug involving pseudo classes and sibling selectors this may not work in some webkit-based browsers.&lt;/p&gt; &lt;p&gt;Here's our finished toggle button.&lt;/p&gt; &lt;label id="label"&gt; &lt;input type="checkbox" id="checkbox" /&gt; &lt;span id="off"&gt;Off&lt;/span&gt; &lt;span id="on"&gt;On&lt;/span&gt; &lt;/label&gt; &lt;p&gt;Here's how we build it. First you start off with a label containing a checkbox and two spans.&lt;/p&gt; &lt;label id="label"&gt; &lt;input type="checkbox" /&gt; &lt;span&gt;Off&lt;/span&gt; &lt;span&gt;On&lt;/span&gt; &lt;/label&gt; &lt;p&gt;Simple and not much to look at. Let's style those two spans to look like buttons and hide the checkbox.&lt;/p&gt; &lt;label id="label"&gt; &lt;input type="checkbox" id="checkbox" /&gt; &lt;span id="off"&gt;Off&lt;/span&gt; &lt;span id="on" style="display:block;"&gt;On&lt;/span&gt; &lt;/label&gt; &lt;p&gt;Now, the key is to hide the "On" span initially with "display: none;". Then we show it when the checkbox is checked while hiding the "Off" span at the same time. This is done with this CSS:&lt;/p&gt; &lt;pre&gt; #checkbox:checked + #off { display: none; } #checkbox:checked + #off + #on { display: block; } &lt;/pre&gt; &lt;p&gt;So, when the checkbox is checked we are targeting its sibling "off" and we are also targeting the sibling of the checkbox's sibling which is "on".&lt;/p&gt; &lt;p&gt;At this point you could use server-side code or javascript to react to the hidden checkbox being checked just like you would with a normal checkbox.&lt;/p&gt; ​ p { margin: 10px; } pre { margin: 10px; } #label { cursor: pointer; display: block; margin: 20px auto; width: 200px; } #checkbox { display: none; } #checkbox:checked + #off { display: none; } #checkbox:checked + #off + #on { display: block; } #off { background: #ff3019; background: -moz-linear-gradient(top, #ff3019 0%, #cf0404 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ff3019), color-stop(100%,#cf0404)); background: -webkit-linear-gradient(top, #ff3019 0%,#cf0404 100%); background: -o-linear-gradient(top, #ff3019 0%,#cf0404 100%); background: -ms-linear-gradient(top, #ff3019 0%,#cf0404 100%); background: linear-gradient(top, #ff3019 0%,#cf0404 100%); border: 2px solid #cf0404; -moz-border-radius: 10px; -webkit-border-radius: 10px; -o-border-radius: 10px; border-radius: 10px; color: white; display: block; font: 1em "Amaranth", sans-serif; padding: 4px 10px; text-align: center; -moz-user-select: none; -webkit-user-select: none; -o-user-select: none; user-select: none; } #on { background: #b4ddb4; background: -moz-linear-gradient(top, #b4ddb4 0%, #83c783 17%, #52b152 33%, #008a00 67%, #005700 83%, #002400 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#b4ddb4), color-stop(17%,#83c783), color-stop(33%,#52b152), color-stop(67%,#008a00), color-stop(83%,#005700), color-stop(100%,#002400)); background: -webkit-linear-gradient(top, #b4ddb4 0%,#83c783 17%,#52b152 33%,#008a00 67%,#005700 83%,#002400 100%); background: -o-linear-gradient(top, #b4ddb4 0%,#83c783 17%,#52b152 33%,#008a00 67%,#005700 83%,#002400 100%); background: -ms-linear-gradient(top, #b4ddb4 0%,#83c783 17%,#52b152 33%,#008a00 67%,#005700 83%,#002400 100%); background: linear-gradient(top, #b4ddb4 0%,#83c783 17%,#52b152 33%,#008a00 67%,#005700 83%,#002400 100%); border: 2px solid #002400; -moz-border-radius: 10px; -webkit-border-radius: 10px; -o-border-radius: 10px; border-radius: 10px; color: white; display: none; font: 1em "Amaranth", sans-serif; padding: 4px 10px; text-align: center; -moz-user-select: none; -webkit-user-select: none; -o-user-select: none; user-select: none; } ​ </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload