mozilla

Revision 294113 of :checked

  • Revision slug: CSS/:checked
  • Revision title: :checked
  • Revision id: 294113
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment

Revision Content

{{ CSSRef() }}

Summary

The :checked CSS 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

/* any "checkable" element */
:checked {
  width: 50px;
  height: 50px;
}

/* only radio elements */
input[type="radio"]:checked {
  margin-left: 25px;
}

/* only checkbox elements */
input[type="checkbox"]:checked {
  display: none;  
}

/* only option elements */
option:checked {
  color: red;
}
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 hidden checkboxes 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 employed 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;
}

#isexpanded:checked ~ #expand-btn, #isexpanded: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;
}

#isexpanded, .expandable {
    display: none;
}

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

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

<input type="checkbox" id="isexpanded" />

<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="isexpanded" id="expand-btn">Show hidden elements</label></p>
<p class="expandable">[another sample text]</p>
<p>[some sample text]</p>
</body>
</html>

Using hidden radioboxes in order to store some CSS boolean values

You can also use the :checked pseudo-class applied to hidden radioboxes in order to build, for example, an image gallery with full-size images shown only when mouse clicks on previews. See this demo for a possible cue.

Note: For an analogous effect, but based on :hover pseudo-class and without hidden radioboxes, see this demo, taken from the En/CSS/:hover page.

Specifications

Specification Status Comment
{{ SpecName('HTML WHATWG', '#selector-checked', ':checked') }} {{ Spec2('HTML WHATWG') }} No change.
{{ SpecName('HTML5 W3C', '#selector-checked', ':checked') }} {{ Spec2('HTML5 W3C') }} Defines the semantic regarding HTML.
{{ SpecName('CSS4 Selectors', '#checked-pseudo', ':checked') }} {{ Spec2('CSS4 Selectors') }} No change.
{{ SpecName('CSS3 UI', '#pseudo-checked', ':checked') }} {{ Spec2('CSS3 UI') }} Link to Selectors Level 3.
{{ SpecName('CSS3 Selectors', '#checked', ':checked') }} {{ Spec2('CSS3 Selectors') }} Defines the pseudo-class, but not the associated semantic

Browser compatibility

{{ CompatibilityTable() }}

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

 

Revision Source

<p>{{ CSSRef() }}</p>
<h2 id="Summary">Summary</h2>
<p>The <code>:checked</code> CSS <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&nbsp;</strong>(<code>&lt;input type="checkbox"&gt;</code>) or <strong>option&nbsp;</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 id="Syntax">Syntax</h2>
<pre>
element:checked { style properties }</pre>
<p>Examples</p>
<h4 id="Example_selectors">Example selectors</h4>
<pre class="brush: css">
/* any "checkable" element */
:checked {
  width: 50px;
  height: 50px;
}

/* only radio elements */
<code>input[type="radio"]:checked {
  margin-left: 25px;
}
</code>
/* only checkbox elements */
input[type="checkbox"]:checked {
  display: none;  
}

/* only option elements */
option:checked {
&nbsp; color: red;
}
</pre>
<dl>
  <dt>
    <code>input[type="radio"]:checked</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 id="Using_hidden_checkboxes_in_order_to_store_some_CSS_boolean_values">Using hidden checkboxes 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 employed 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 {
&nbsp;&nbsp;&nbsp; margin: 0 3px;
&nbsp;&nbsp;&nbsp; display: inline-block;
&nbsp;&nbsp;&nbsp; font: 12px / 13px "Lucida Grande", sans-serif;
&nbsp;&nbsp;&nbsp; text-shadow: rgba(255, 255, 255, 0.4) 0 1px;
&nbsp;&nbsp;&nbsp; padding: 3px 6px;
&nbsp;&nbsp;&nbsp; border: 1px solid rgba(0, 0, 0, 0.6);
&nbsp;&nbsp;&nbsp; background-color: #969696;
&nbsp;&nbsp;&nbsp; cursor: default;
&nbsp;&nbsp;&nbsp; -moz-border-radius: 3px;
&nbsp;&nbsp;&nbsp; -webkit-border-radius: 3px;
&nbsp;&nbsp;&nbsp; border-radius: 3px;
&nbsp;&nbsp;&nbsp; -moz-box-shadow: rgba(255, 255, 255, 0.4) 0 1px, inset 0 20px 20px -10px white;
&nbsp;&nbsp;&nbsp; -webkit-box-shadow: rgba(255, 255, 255, 0.4) 0 1px, inset 0 20px 20px -10px white;
&nbsp;&nbsp;&nbsp; box-shadow: rgba(255, 255, 255, 0.4) 0 1px, inset 0 20px 20px -10px white;
}

#isexpanded:checked ~ #expand-btn, #isexpanded:checked ~ * #expand-btn {
&nbsp;&nbsp;&nbsp; background: #B5B5B5;
&nbsp;&nbsp;&nbsp; -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;
&nbsp;&nbsp;&nbsp; -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;
&nbsp;&nbsp;&nbsp; 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;
}

#isexpanded, .expandable {
&nbsp;&nbsp;&nbsp; display: none;
}

#isexpanded:checked ~ * tr.expandable {
&nbsp;&nbsp;&nbsp; display: table-row;
&nbsp;&nbsp;&nbsp; background: #cccccc;
}

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

&lt;input type="checkbox" id="isexpanded" /&gt;

&lt;h1&gt;Expandable elements&lt;/h1&gt;
&lt;table&gt;
&nbsp;&nbsp;&nbsp; &lt;thead&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &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;
&nbsp;&nbsp;&nbsp; &lt;/thead&gt;
&nbsp;&nbsp;&nbsp; &lt;tbody&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &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;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &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;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &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;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &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;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &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;
&nbsp;&nbsp;&nbsp; &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;[some sample text]&lt;/p&gt;
&lt;p&gt;&lt;label for="isexpanded" 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>
<h4 id="Using_hidden_radioboxes_in_order_to_store_some_CSS_boolean_values">Using hidden radioboxes in order to store some CSS boolean values</h4>
<p>You can also use the <code>:checked</code> pseudo-class applied to hidden radioboxes in order to build, for example, an <strong>image gallery with full-size images shown only when mouse clicks on previews</strong>. See <a class="internal" href="/@api/deki/files/6268/=css-checked-gallery.zip" title="css-checked-gallery.zip">this demo</a> for a possible cue.</p>
<div class="note">
  <strong>Note:</strong> For an analogous effect, but based on <a class="internal" href="/En/CSS/:hover" title="en/CSS/:hover"><code>:hover</code></a> pseudo-class and without hidden radioboxes, see <a class="internal" href="/@api/deki/files/6247/=css-gallery.zip" title="css-gallery.zip">this demo</a>, taken from the <a class="internal" href="/En/CSS/:hover" title="en/CSS/:hover">En/CSS/:hover</a> page.</div>
<h2 id="Specifications"><span>Specifications</span></h2>
<table class="standard-table">
  <thead>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comment</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>{{ SpecName('HTML WHATWG', '#selector-checked', ':checked') }}</td>
      <td>{{ Spec2('HTML WHATWG') }}</td>
      <td>No change.</td>
    </tr>
    <tr>
      <td>{{ SpecName('HTML5 W3C', '#selector-checked', ':checked') }}</td>
      <td>{{ Spec2('HTML5 W3C') }}</td>
      <td>Defines the semantic regarding HTML.</td>
    </tr>
    <tr>
      <td>{{ SpecName('CSS4 Selectors', '#checked-pseudo', ':checked') }}</td>
      <td>{{ Spec2('CSS4 Selectors') }}</td>
      <td>No change.</td>
    </tr>
    <tr>
      <td>{{ SpecName('CSS3 UI', '#pseudo-checked', ':checked') }}</td>
      <td>{{ Spec2('CSS3 UI') }}</td>
      <td>Link to Selectors Level 3.</td>
    </tr>
    <tr>
      <td>{{ SpecName('CSS3 Selectors', '#checked', ':checked') }}</td>
      <td>{{ Spec2('CSS3 Selectors') }}</td>
      <td>Defines the pseudo-class, but not the associated semantic</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_compatibility">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.0) }}</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.0) }}</td>
        <td>9.0</td>
        <td>9.5</td>
        <td>3.1</td>
      </tr>
    </tbody>
  </table>
</div>
<p>&nbsp;</p>
Revert to this revision