
CSS Topics
- CSS Intro
- CSS How To
- CSS Editors
- CSS Properties
- CSS Selectors
- .class
- .class1.class2
- .class1 .class2
- #id
- * (all)
- element
- element.class
- element,element
- element element
- element>element
- element+element
- element1~element2
- [attribute]
- [attribute=value]
- [attribute~=value]
- [attribute|=value]
- [attribute^=value]
- [attribute$=value]
- [attribute*=value]
- :active
- ::after
- ::before
- :checked
- :default
- :disabled
- :empty
- :enabled
- :first-child
- ::first-letter
- ::first-line
- :first-of-type
- :focus
- :fullscreen
- :has()
- :hover
- :in-range
- :indeterminate
- :invalid
- :lang()
- :last-child
- :last-of-type
- :link
- ::marker
- :not()
- :nth-child()
- :nth-last-child()
- :nth-last-of-type()
- :nth-of-type()
- :only-of-type
- :only-child
- :optional
- :out-of-range
- ::placeholder
- :read-only
- :read-write
- :required
- :root
- ::selection
- :target
- :valid
- :visited
- CSS Comments
- CSS Length
- CSS Image Sprites
- CSS Grid Layout
- CSS Grid Flexbox
- CSS @charset Rule
- CSS @font-face Rule
- CSS @import Rule
- CSS @keyframes Rule
- CSS @media Rule
CSS :checked Selector

Photo Credit to CodeToFun
Introduction
The :checked
selector in CSS is used to style form elements that are checked or selected.
This selector applies to checkboxes (<input type="checkbox">), radio buttons (<input type="radio">
), and <option>
elements in a dropdown (<select>
), allowing you to create custom styles when these elements are selected.
Syntax
The signature of the :checked
Selector is as follows:
:checked {
/* CSS properties */
}
The :checked
pseudo-class can be applied to input elements like checkboxes, radio buttons, and options in a select dropdown.
Example
Hereβs an example demonstrating how the :checked
selector can be used:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS :checked Selector Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<form>
<h3>Choose your preferences:</h3>
<input type="checkbox" id="subscribe" checked>
<label for="subscribe">Subscribe to newsletter</label><br>
<input type="radio" id="male" name="gender">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" checked>
<label for="female">Female</label><br>
<select>
<option value="yes" checked>Yes</option>
<option value="no">No</option>
</select>
</form>
</body>
</html>
CSS
/* Style for checked checkboxes and radio buttons */
input:checked {
outline: 2px solid blue;
background-color: #cce7ff;
}
/* Style for checked options in select dropdowns */
option:checked {
background-color: #cce7ff;
}
In this example:
- Checked checkboxes and radio buttons are highlighted with a blue outline and light blue background.
- The selected option in the dropdown gets a background color change when checked.
Usage Tips
- The
:checked
selector is especially useful when customizing checkboxes, radio buttons, and dropdowns beyond default browser styles. - It can be combined with other pseudo-classes or selectors. For example,
input[type="radio"]:checked
targets only checked radio buttons. - To enhance user experience, you can use transitions to animate changes when elements are checked.
Common Pitfalls
- The
:checked
pseudo-class only applies to elements that can have a checked state, such as checkboxes, radio buttons, and select options. It will not work on other form elements. - Ensure that your styles do not conflict with the form's default behavior, especially if you are using JavaScript to dynamically update the checked state.
- If using a select dropdown, keep in mind that browsers handle the styling of options differently, so test for cross-browser compatibility.
Conclusion
The :checked
selector is a versatile and powerful way to style form elements based on their selection state.
By applying this pseudo-class, you can create a more interactive and visually appealing form, guiding users through their choices. Itβs a great tool for enhancing the look and feel of checkboxes, radio buttons, and dropdowns on your web page.
Join our Community:
Author

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS :checked Selector), please comment here. I will help you immediately.