:checked
Quick Summary for :checked
The :checked CSS pseudo-class selector represents any radio (<input type="radio">), checkbox (<input type="checkbox">), or option (<option> in a <select>) element that is checked or toggled to an on state.
Code Usage for :checked
/* Matches any checked/selected radio, checkbox, or option */ :checked {   margin-left: 25px;   border: 1px solid blue; } 
More Details for :checked

:checked

The :checked CSS pseudo-class selector represents any radio (<input type="radio">), checkbox (<input type="checkbox">), or option (<option> in a <select>) element that is checked or toggled to an on state.

/* Matches any checked/selected radio, checkbox, or option */ :checked {   margin-left: 25px;   border: 1px solid blue; } 

The user can engage this state by checking/selecting an element, or disengage it by unchecking/deselecting the element.

Note: Because browsers often treat <option>s as replaced elements, the extent to which they can be styled with the :checked pseudo-class varies from browser to browser.

Syntax

:checked

Examples

Basic example

HTML
<div>   <input type="radio" name="my-input" id="yes">   <label for="yes">Yes</label>    <input type="radio" name="my-input" id="no">   <label for="no">No</label> </div>  <div>   <input type="checkbox" name="my-checkbox" id="opt-in">   <label for="opt-in">Check me!</label> </div>  <select name="my-select" id="fruit">   <option value="opt1">Apples</option>   <option value="opt2">Grapes</option>   <option value="opt3">Pears</option> </select> 
CSS
div, select {   margin: 8px; }  /* Labels for checked inputs */ input:checked + label {   color: red; }  /* Radio element, when checked */ input[type="radio"]:checked {   box-shadow: 0 0 0 3px orange; }  /* Checkbox element, when checked */ input[type="checkbox"]:checked {   box-shadow: 0 0 0 3px hotpink; }  /* Option elements, when selected */ option:checked {   box-shadow: 0 0 0 3px lime;   color: red; } 
Result

Toggling elements with a hidden checkbox

This example utilizes the :checked pseudo-class to let the user toggle content based on the state of a checkbox, all without using JavaScript.

HTML
<input type="checkbox" id="expand-toggle" />  <table>   <thead>     <tr><th>Column #1</th><th>Column #2</th><th>Column #3</th></tr>   </thead>   <tbody>     <tr class="expandable"><td>[more text]</td><td>[more text]</td><td>[more 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>[more text]</td><td>[more text]</td><td>[more text]</td></tr>     <tr class="expandable"><td>[more text]</td><td>[more text]</td><td>[more text]</td></tr>   </tbody> </table>  <label for="expand-toggle" id="expand-btn">Toggle hidden rows</label> 
CSS
/* Hide the toggle checkbox */ #expand-toggle {   display: none; }  /* Hide expandable content by default */ .expandable {   visibility: collapse;   background: #ddd; }  /* Style the button */ #expand-btn {   display: inline-block;   margin-top: 12px;   padding: 5px 11px;   background-color: #ff7;   border: 1px solid;   border-radius: 3px; }  /* Show hidden content when the checkbox is checked */ #expand-toggle:checked ~ * .expandable {   visibility: visible; }  /* Style the button when the checkbox is checked */ #expand-toggle:checked ~ #expand-btn {   background-color: #ccc; } 
Result

You can use the :checked pseudo-class to build an image gallery with full-size images that show only when the user clicks on a thumbnail. See this demo for a possible cue.

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

Specifications

Specification
HTML Standard # selector-checked
Selectors Level 4 # checked

See also

Web forms — working with user data Styling web forms Related HTML elements: <input type="checkbox">, <input type="radio">, <select>, and <option> Replaced elements

Last modified: Aug 12, 2021, by MDN contributors

Select your preferred language English (US)EspañolFrançais日本語한국어Português (do Brasil)Русский中文 (简体) Change language

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

You could also follow me on twitter. I have a couple of youtube channels if you want to see some video related content. RuneScape 3, Minecraft and also a coding channel here Web Dev.

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote

"Olivia, my eldest daughter, caught measles when she was seven years old. As the illness took its usual course I can remember reading to her often in bed and not feeling particularly alarmed about it. Then one morning, when she was well on the road to recovery, I was sitting on her bed showing her how to fashion little animals out of coloured pipe-cleaners, and when it came to her turn to make one herself, I noticed that her fingers and her mind were not working together and she couldn’t do anything. 'Are you feeling all right?' I asked her. 'I feel all sleepy,' she said. In an hour, she was unconscious. In twelve hours she was dead. The measles had turned into a terrible thing called measles encephalitis and there was nothing the doctors could do to save her. That was...in 1962, but even now, if a child with measles happens to develop the same deadly reaction from measles as Olivia did, there would still be nothing the doctors could do to help her. On the other hand, there is today something that parents can do to make sure that this sort of tragedy does not happen to a child of theirs. They can insist that their child is immunised against measles. ...I dedicated two of my books to Olivia, the first was ‘James and the Giant Peach’. That was when she was still alive. The second was ‘The BFG’, dedicated to her memory after she had died from measles. You will see her name at the beginning of each of these books. And I know how happy she would be if only she could know that her death had helped to save a good deal of illness and death among other children."

I just checked google books for BFG, and the dedication is there. 

https://www.google.com.au/books/edition/_/quybcXrFhCIC?hl=en&gbpv=1 


Roald Dahl, 1986
Random CSS Property

@font-feature-values

The @font-feature-values CSS at-rule lets you use a common name in the font-variant-alternates property for features activated differently in OpenType. This can help simplify your CSS when using multiple fonts.
@character-variant css reference