Revision 834 of :checked

  • Revision slug: CSS/:checked
  • Revision title: :checked
  • Revision id: 834
  • Created:
  • Creator: fusionchess
  • Is current revision? No
  • Comment Added special example; 1 words added

Revision Content

{{ CSSRef() }}

Summary

The :checked pseudo-class selector represents any radio (<input type="radio">), checkbox (<input type="checkbox">) or option ({{ HTMLElement("option") }} in a {{ HTMLElement("select") }}) element that is checked or toggled to an on state. The user can change this state by clicking on the element, or selecting a different value, in which case the :checked pseudo-class no longer applies to this element, but will to the relevant one.

Syntax 

element:checked { style properties }

Examples

Example selectors

input[type="radio"]:checked
Represents all radio buttons on the page that are checked
input[type="checkbox"]:checked
Represents all checkboxes on the page that are checked
option:checked
Represents all select's options on the page that are selected

Using an hidden checkbox in order to store some CSS boolean values

The :checked pseudo-class applied to hidden checkboxes appended at the begin of your page could be used in order to store some dynamic booleans to be used by a CSS rule. The following example shows how to hide/show some expandable elements simply clicking on a button (download this demo).

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Expandable elements</title>
<style type="text/css">
#expand-btn {
    margin: 0 3px;
    display: inline-block;
    font: 12px / 13px "Lucida Grande", sans-serif;
    text-shadow: rgba(255, 255, 255, 0.4) 0 1px;
    padding: 3px 6px;
    border: 1px solid rgba(0, 0, 0, 0.6);
    background-color: #969696;
    cursor: default;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;
    -moz-box-shadow: rgba(255, 255, 255, 0.4) 0 1px, inset 0 20px 20px -10px white;
    -webkit-box-shadow: rgba(255, 255, 255, 0.4) 0 1px, inset 0 20px 20px -10px white;
    box-shadow: rgba(255, 255, 255, 0.4) 0 1px, inset 0 20px 20px -10px white;
}

#showrows:checked ~ #expand-btn, #showrows:checked ~ * #expand-btn {
    background: #B5B5B5;
    -moz-box-shadow: inset rgba(0, 0, 0, 0.4) 0 -5px 12px, inset rgba(0, 0, 0, 1) 0 1px 3px, rgba(255, 255, 255, 0.4) 0 1px;
    -webkit-box-shadow: inset rgba(0, 0, 0, 0.4) 0 -5px 12px, inset rgba(0, 0, 0, 1) 0 1px 3px, rgba(255, 255, 255, 0.4) 0 1px;
    box-shadow: inset rgba(0, 0, 0, 0.4) 0 -5px 12px, inset rgba(0, 0, 0, 1) 0 1px 3px, rgba(255, 255, 255, 0.4) 0 1px;
}

#showrows, .expandable {
    display: none;
}

#showrows:checked ~ table tr.expandable, #showrows:checked ~ * table tr.expandable {
    display: table-row;
    background: #cccccc;
}

#showrows:checked ~ p.expandable, #showrows:checked ~ * p.expandable {
    display: block;
    background: #cccccc;
}
</style>
</head>
<body>

<input type="checkbox" name="rows-expanded" id="showrows" />

<h1>Expandable elements</h1>
<table>
    <thead>
        <tr><th>Column #1</th><th>Column #2</th><th>Column #3</th></tr>
    </thead>
    <tbody>
        <tr class="expandable"><td>[cell text]</td><td>[cell text]</td><td>[cell text]</td></tr>
        <tr><td>[cell text]</td><td>[cell text]</td><td>[cell text]</td></tr>
        <tr><td>[cell text]</td><td>[cell text]</td><td>[cell text]</td></tr>
        <tr class="expandable"><td>[cell text]</td><td>[cell text]</td><td>[cell text]</td></tr>
        <tr class="expandable"><td>[cell text]</td><td>[cell text]</td><td>[cell text]</td></tr>
    </tbody>
</table>

<p>[some sample text]</p>
<p><label for="showrows" id="expand-btn">Show hidden elements</label></p>
<p class="expandable">[another sample text]</p>
<p>[some sample text]</p>
</body>
</html>

Specifications

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 {{ CompatGeckoDesktop("1") }} 9.0 9.0 3.1
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.1 {{ CompatGeckoMobile("1") }} 9.0 9.5 3.1

Revision Source

<p>{{ CSSRef() }}</p>
<h2>Summary</h2>
<p>The <code>:checked</code> <a href="/en/CSS/Pseudo-classes" title="Pseudo-classes">pseudo-class</a> selector represents any <strong>radio </strong>(<code>&lt;input type="radio"&gt;</code>), <strong>checkbox </strong>(<code>&lt;input type="checkbox"&gt;</code>) or <strong>option </strong>({{ HTMLElement("option") }} in a {{ HTMLElement("select") }}) element that is checked or toggled to an <code>on</code> state. The user can change this state by clicking on the element, or selecting a different value, in which case the <code>:checked</code> pseudo-class no longer applies to this element, but will to the relevant one.</p>
<h2>Syntax </h2>
<pre>element:checked { style properties }</pre>
<h2 class="editable">Examples</h2>
<h4>Example selectors</h4>
<dl> <dt><code>input[type="radio"]:checked</code><br> <code> </code></dt> <dd>Represents all radio buttons on the page that are checked</dd> <dt><code>input[type="checkbox"]:checked</code></dt> <dd>Represents all checkboxes on the page that are checked</dd> <dt><code>option:checked</code></dt> <dd>Represents all select's options on the page that are selected</dd>
</dl>
<h4>Using an hidden checkbox in order to store some CSS boolean values</h4>
<p>The <code>:checked</code> pseudo-class applied to hidden checkboxes appended at the begin of your page could be used in order to store some dynamic booleans to be used by a CSS rule. The following example shows how to hide/show some expandable elements simply clicking on a button (<a class="internal" href="/@api/deki/files/6246/=expandable-elements.html" title="Expandable elements through the :checked pseudoclass">download this demo</a>).</p>
<pre class="brush: html">&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Expandable elements&lt;/title&gt;
&lt;style type="text/css"&gt;
#expand-btn {
    margin: 0 3px;
    display: inline-block;
    font: 12px / 13px "Lucida Grande", sans-serif;
    text-shadow: rgba(255, 255, 255, 0.4) 0 1px;
    padding: 3px 6px;
    border: 1px solid rgba(0, 0, 0, 0.6);
    background-color: #969696;
    cursor: default;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;
    -moz-box-shadow: rgba(255, 255, 255, 0.4) 0 1px, inset 0 20px 20px -10px white;
    -webkit-box-shadow: rgba(255, 255, 255, 0.4) 0 1px, inset 0 20px 20px -10px white;
    box-shadow: rgba(255, 255, 255, 0.4) 0 1px, inset 0 20px 20px -10px white;
}

#showrows:checked ~ #expand-btn, #showrows:checked ~ * #expand-btn {
    background: #B5B5B5;
    -moz-box-shadow: inset rgba(0, 0, 0, 0.4) 0 -5px 12px, inset rgba(0, 0, 0, 1) 0 1px 3px, rgba(255, 255, 255, 0.4) 0 1px;
    -webkit-box-shadow: inset rgba(0, 0, 0, 0.4) 0 -5px 12px, inset rgba(0, 0, 0, 1) 0 1px 3px, rgba(255, 255, 255, 0.4) 0 1px;
    box-shadow: inset rgba(0, 0, 0, 0.4) 0 -5px 12px, inset rgba(0, 0, 0, 1) 0 1px 3px, rgba(255, 255, 255, 0.4) 0 1px;
}

#showrows, .expandable {
    display: none;
}

#showrows:checked ~ table tr.expandable, #showrows:checked ~ * table tr.expandable {
    display: table-row;
    background: #cccccc;
}

#showrows:checked ~ p.expandable, #showrows:checked ~ * p.expandable {
    display: block;
    background: #cccccc;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;input type="checkbox" name="rows-expanded" id="showrows" /&gt;

&lt;h1&gt;Expandable elements&lt;/h1&gt;
&lt;table&gt;
    &lt;thead&gt;
        &lt;tr&gt;&lt;th&gt;Column #1&lt;/th&gt;&lt;th&gt;Column #2&lt;/th&gt;&lt;th&gt;Column #3&lt;/th&gt;&lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr class="expandable"&gt;&lt;td&gt;[cell text]&lt;/td&gt;&lt;td&gt;[cell text]&lt;/td&gt;&lt;td&gt;[cell text]&lt;/td&gt;&lt;/tr&gt;
        &lt;tr&gt;&lt;td&gt;[cell text]&lt;/td&gt;&lt;td&gt;[cell text]&lt;/td&gt;&lt;td&gt;[cell text]&lt;/td&gt;&lt;/tr&gt;
        &lt;tr&gt;&lt;td&gt;[cell text]&lt;/td&gt;&lt;td&gt;[cell text]&lt;/td&gt;&lt;td&gt;[cell text]&lt;/td&gt;&lt;/tr&gt;
        &lt;tr class="expandable"&gt;&lt;td&gt;[cell text]&lt;/td&gt;&lt;td&gt;[cell text]&lt;/td&gt;&lt;td&gt;[cell text]&lt;/td&gt;&lt;/tr&gt;
        &lt;tr class="expandable"&gt;&lt;td&gt;[cell text]&lt;/td&gt;&lt;td&gt;[cell text]&lt;/td&gt;&lt;td&gt;[cell text]&lt;/td&gt;&lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;[some sample text]&lt;/p&gt;
&lt;p&gt;&lt;label for="showrows" id="expand-btn"&gt;Show hidden elements&lt;/label&gt;&lt;/p&gt;
&lt;p class="expandable"&gt;[another sample text]&lt;/p&gt;
&lt;p&gt;[some sample text]&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<h2><span>Specifications</span></h2>
<ul> <li><a class="external" href="http://www.w3.org/TR/css3-selectors/#checked" rel="external nofollow" target="_blank" title="http://www.w3.org/TR/css3-selectors/#enableddisabled">CSS3 Selectors :checked</a> Proposed Recommendation</li> <li><a class="external" href="http://www.whatwg.org/specs/web-forms/current-work/#relation" rel="external nofollow" target="_blank" title="http://www.whatwg.org/specs/web-forms/current-work/#relation">WHATWG web-forms</a></li>
</ul>
<h2>Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Chrome</th> <th>Firefox (Gecko)</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari</th> </tr> <tr> <td>Basic support</td> <td>1.0</td> <td>{{ CompatGeckoDesktop("1") }}</td> <td>9.0</td> <td>9.0</td> <td>3.1</td> </tr> </tbody> </table>
</div>
<div id="compat-mobile"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Android</th> <th>Firefox Mobile (Gecko)</th> <th>IE Mobile</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>2.1</td> <td>{{ CompatGeckoMobile("1") }}</td> <td>9.0</td> <td>9.5</td> <td>3.1</td> </tr> </tbody> </table>
</div>
Revert to this revision