CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

:Checked


The :checked pseudo-class in CSS is used to select and style form elements like radio buttons, checkboxes, and options in a select element that are in a selected or "checked" state. This allows you to provide visual feedback to users when they interact with these input types.


Example 1: Styling a Checked Checkbox

/* This CSS rule targets a checkbox input when it is checked. */
input[type="checkbox"]:checked {
  /* The box-shadow property adds a blue glow around the checkbox. */
  box-shadow: 0 0 5px 2px dodgerblue;
}

Explanation

This code applies a blue shadow around a checkbox when the user clicks on it. The input[type="checkbox"] part selects all checkboxes, and the :checked pseudo-class narrows that down to only the one that is currently checked.


Example 2: Revealing Content When a Radio Button is Checked

/* This CSS hides a div by default. */
.hidden-content {
  display: none;
}

/* When the radio button with the id 'show' is checked, the adjacent .hidden-content div is displayed. */
#show:checked ~ .hidden-content {
  display: block;
}

Explanation

This example demonstrates how to show a hidden element when a specific radio button is selected. The ~ is the general sibling combinator, which selects the .hidden-content div that follows the checked radio button.


Example 3: Changing the Label Style of a Checked Option

/* This CSS rule targets the label of a checked radio button. */
input[type="radio"]:checked + label {
  /* The color property changes the text color to green. */
  color: green;
  /* The font-weight property makes the text bold. */
  font-weight: bold;
}

Explanation

This code changes the text color and weight of a label associated with a radio button when that button is selected. The + is the adjacent sibling combinator, selecting the label that immediately follows the checked radio button.


Example 4: Custom Styled Checkbox

/* This CSS creates a custom styled checkbox. */
.custom-checkbox:checked {
  background-color: #2196F3; /* Blue background when checked */
  border-color: #2196F3;
}

.custom-checkbox:checked::before {
  content: '\2713'; /* Adds a checkmark character */
  display: block;
  color: white;
  text-align: center;
}

Explanation

This CSS provides a more customized look for a checkbox. When the checkbox is checked, its background color changes, and a checkmark character is inserted using the ::before pseudo-element.


Example 5: Highlighting a selected option in a dropdown

/* This rule styles the selected option within a select element. */
option:checked {
    background: linear-gradient(to right, #66bb6a, #43a047); /* Green gradient background */
    color: white; /* White text color */
}

Explanation

This code enhances the user experience of a dropdown menu by styling the currently selected <option>. It applies a green gradient background and white text to the option that has been :checked.