Grow your CSS skills. Land your dream job.

:checked

Last updated on:

The :checked pseudo-class in CSS selects elements when they are in the selected state. It is only associated with input (<input>) elements of type radio and checkbox . The :checked pseudo-class selector matches radio and checkbox input types when checked or toggled to an on state. If they are not selected or checked, there is no match.

So when a checkbox is checked, and you are targeting the label immediately after it:

input[type=checkbox] + label {
  color: #ccc;
  font-style: italic;
} 
input[type=checkbox]:checked + label {
  color: #f00;
  font-style: normal;
} 

The label text will turn from grey italic to red normal font.

<input type="checkbox" id="ossm" name="ossm"> 
<label for="ossm">CSS is Awesome</label> 

The above is an example of using the :checked pseudo-class to make forms more accessible. The :checked pseudo-class can be used with hidden inputs and their visible labels to build interactive widgets, such as image galleries.

More Resources

Browser Support

Chrome Safari Firefox Opera IE Android iOS
Any 3.1+ Any 9+ 9+ any any

Comments

  1. RITURAJ
    Permalink to comment#

    “:checked” selector work to find out the checkbox who are checked .
    example

    html code::

    jQuery code::
    $(document).ready(function(){
    alert($(“#Demo :checked”).attr(‘id’));
    });

    Result
    1

    • Paul Garcia
      Permalink to comment#

      Not just to find out the element checked but also to style it:

      #Demo:checked { color:red; text-decoration:underline; } or
      input[type=”radio”]:checked{ color:red; … }

  2. Kuro
    Permalink to comment#

    CSS is Awesome

    if I move the order, how to write the css code?
    I’ve repetition something like that, but confuse how to make it simpler like you had

  3. The above is an example of using the :checked pseudo-class to make forms more accessible.

    I’m using this technique to hide/show navigation menus in a responsive layout. However, I’m discovering that since the LABEL tag cannot have FOCUS, that this technique is not at all accessible (at least not to keyboard users). Is there a way around this?

    Also, in my experience the adjacent sibling selector (‘+’) does not work in iOS (must use the general sibling selector ‘~’).

    • Camilla
      Permalink to comment#

      IOS 8 seems to be sensitive to this. Fixed my problem to change from + to ~ ! Thanks!

  4. Might be worth mentioning that unchecked checkboxes can be specifically targeted by using :not(:checked).

    Browser support is almost the same. See http://css-tricks.com/almanac/selectors/n/not/

  5. Skumbabs
    Permalink to comment#

    Hello,

    any ideas/tricks to use it on ie8 as long as :checked does not exist?

    Thanks

    • Tanya

      Hi, for ie8 I add class ‘checked’ and use
      input[type=checkbox].checked + label {
      color: #f00;
      font-style: normal;
      }

  6. TSF NAEM
    Permalink to comment#

    there are various types of input fields in a HTML page such as text, number, email, radio, checkbox. Now how will i modify style to all input
    except radio and check box. is it right code if i do that this way :

    input:not([typy=”radio & checkbox”]){

    float:right;

    }

  7. Sadly putting the label after the relevant field means that the field/label pair are not accessible according to the WCAG 2.0. There are a lot of examples of this field + label pattern out there but in order for the markup to make sense to all users and assistive technology the label should come first.

    I await the day we can use selectors which target the preceding item

    • Apologies, the above statement isn’t actually correct, it was the Surfright standards that stipulated this. Labels after radios and checkboxes are valid. The WCAG 2.0 guidelines state that radiobuttons and checkboxes can have the label positioned after and (as far as I can tell) that this is a matter of visual positioning only.

  8. every
    Permalink to comment#

    Thank you man!

  9. Browser support: Android: any?

    No, unfortunately not. You named it. Android 4.2 is still affected. Your proposed hack has severe performance issues on some devices.

    I don’t have an other solution, though.

  10. mikhil jose
    Permalink to comment#

    Is there any solution if we want to repeat the “id” along with “check box” more times????? I need more check boxes and labels in my project..
    :(
    (Excuse me for my worst English)

  11. Jugal
    Permalink to comment#

    Hi
    Can any one tell this

    i have two radio button.

    If i check one radio button , i want that other radio button should be checked. means check other radio button on another radio button’s checked.

  12. Jugal
    Permalink to comment#

    hi

    One more thing it should be done by HTML and CSS. Without any use of javascript

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like ```this```. You don't need to escape code in backticks, Markdown does that for you.

Sadly, it's kind of broken. WordPress only accepts a subset of HTML in comments, which makes sense, because certainly some HTML can't be allowed, like <script> tags. But this stripping happens before the comment is processed by Markdown (via Jetpack). It seems to me that would be reversed, because after Markdown processes code in backticks, it's escaped, thus safe. If you think you can fix this issue, get in touch!

If you need to make sure the code (typically HTML) you post absolutely posts correctly, escape it and put it within <pre><code> tags.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".