Okay, let's create custom radio buttons with a custom color and a bigger dot when checked using HTML and CSS. We'll hide the native radio button and style its associated label to act as the custom visual.
This approach ensures good accessibility because the actual <input type="radio"> is still present in the DOM, allowing screen readers and keyboard navigation to work correctly.
Key Concepts:
- Hiding Native Input: We'll use
position: absolute; opacity: 0; (or clip: rect()) to hide the actual radio input while keeping it accessible.
- Styling the
label: We'll use the ::before pseudo-element for the outer circle and the ::after pseudo-element for the inner dot.
- Sibling Selector (
+): We'll use input[type="radio"]:checked + label to apply styles to the label (and its pseudo-elements) when its associated radio button is checked.
- CSS Variables: For easy customization of colors and sizes.
HTML Structure:
<div class="radio-group">
<h2>Choose Your Favorite Color</h2>
<div class="custom-radio">
<input type="radio" id="color-blue" name="favorite-color" value="blue" checked>
<label for="color-blue">Ocean Blue</label>
</div>
<div class="custom-radio">
<input type="radio" id="color-green" name="favorite-color" value="green">
<label for="color-green">Forest Green</label>
</div>
<div class="custom-radio">
<input type="radio" id="color-red" name="favorite-color" value="red">
<label for="color-red">Ruby Red</label>
</div>
<div class="custom-radio">
<input type="radio" id="color-purple" name="favorite-color" value="purple">
<label for="color-purple">Royal Purple</label>
</div>
</div>
CSS Styling:
:root {
--radio-size: 20px;
--dot-size-unchecked: 0px; /* Dot starts hidden */
--dot-size-checked: 12px; /* Bigger dot when checked */
--border-color-unchecked: #ccc;
--border-color-checked: #007bff; /* Custom color for border when checked */
--dot-color: #007bff; /* Custom color for the dot */
--focus-outline-color: #88BEE8; /* For accessibility */
--transition-speed: 0.2s;
}
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f4f4f4;
margin: 0;
}
.radio-group {
background-color: #fff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}
h2 {
margin-top: 0;
margin-bottom: 25px;
color: #333;
text-align: center;
}
.custom-radio {
margin-bottom: 15px;
/* This makes the label a positioning context for the absolutely positioned input */
position: relative;
padding-left: calc(var(--radio-size) + 15px); /* Space for the custom radio button */
display: block; /* Each radio on its own line */
}
/* Hide the native radio input */
.custom-radio input[type="radio"] {
position: absolute;
left: 0;
top: 0;
width: var(--radio-size);
height: var(--radio-size);
opacity: 0; /* Make it invisible */
z-index: 1; /* Ensure it's clickable over the custom design */
cursor: pointer;
}
/* Style the custom radio button using the label's ::before pseudo-element */
.custom-radio label {
display: inline-block;
position: relative; /* Needed for positioning the ::after dot */
padding-left: 0; /* Reset default padding if any */
cursor: pointer;
line-height: var(--radio-size); /* Align text vertically */
color: #555;
user-select: none; /* Prevent text selection */
}
.custom-radio label::before {
content: '';
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
width: var(--radio-size);
height: var(--radio-size);
border: 2px solid var(--border-color-unchecked);
border-radius: 50%;
background-color: #fff;
transition: border-color var(--transition-speed) ease-in-out,
background-color var(--transition-speed) ease-in-out;
}
/* Style the inner dot using the label's ::after pseudo-element */
.custom-radio label::after {
content: '';
position: absolute;
top: 50%;
left: calc(var(--radio-size) / 2); /* Center horizontally */
transform: translate(-50%, -50%); /* Fine-tune centering */
width: var(--dot-size-unchecked); /* Start hidden */
height: var(--dot-size-unchecked); /* Start hidden */
border-radius: 50%;
background-color: var(--dot-color);
transition: width var(--transition-speed) ease-in-out,
height var(--transition-speed) ease-in-out,
background-color var(--transition-speed) ease-in-out;
}
/* State when the radio input is checked */
.custom-radio input[type="radio"]:checked + label::before {
border-color: var(--border-color-checked);
background-color: #fff; /* Keep outer ring white, or change if desired */
}
.custom-radio input[type="radio"]:checked + label::after {
width: var(--dot-size-checked); /* Make the dot bigger */
height: var(--dot-size-checked); /* Make the dot bigger */
background-color: var(--dot-color); /* Ensure dot color is applied */
}
/* Accessibility: Focus state for keyboard navigation */
.custom-radio input[type="radio"]:focus + label::before {
box-shadow: 0 0 0 3px var(--focus-outline-color);
outline: none; /* Remove default browser outline */
}
/* Optional: Hover effect for the custom radio */
.custom-radio label:hover::before {
border-color: var(--border-color-checked);
}
/* Optional: Slight opacity change on dot when radio is active/pressed */
.custom-radio input[type="radio"]:active + label::after {
background-color: #4a90e2; /* A slightly darker shade */
}
Explanation:
radio-group and custom-radio: Used for overall layout and grouping individual radio buttons.
- Hiding
input[type="radio"]:
position: absolute; left: 0; top: 0; width: var(--radio-size); height: var(--radio-size);: Makes the actual input element occupy the exact same space as where we want our custom visual to be.
opacity: 0;: Makes it completely transparent.
z-index: 1;: Ensures it sits on top of the custom visual created by the label's pseudo-elements, making it clickable.
cursor: pointer;: Shows the hand cursor when hovering over the hidden input, indicating it's interactive.
- Styling the
label (.custom-radio label):
display: inline-block; position: relative;: Important! position: relative; is crucial because it makes the label the positioning context for its ::after pseudo-element (the dot), allowing the dot to be absolutely centered within the custom radio's visual space.
padding-left: calc(var(--radio-size) + 15px);: Shifts the text content of the label to the right, making space for our custom radio circle on the left.
cursor: pointer;: Makes the label clickable, which is good practice for form elements.
- The Outer Circle (
label::before):
content: '';: Required for pseudo-elements.
position: absolute; top: 50%; left: 0; transform: translateY(-50%);: Positions it vertically centered at the very left of the label's content area.
width, height, border-radius: 50%;: Creates the circular shape.
border: 2px solid var(--border-color-unchecked);: The default border.
transition: Smoothly animates changes to its border-color.
- The Inner Dot (
label::after):
content: '';: Required.
position: absolute; top: 50%; left: calc(var(--radio-size) / 2); transform: translate(-50%, -50%);: Positions it precisely in the center of where the ::before circle is.
width: var(--dot-size-unchecked); height: var(--dot-size-unchecked);: Starts with 0px width/height, effectively hiding it.
This setup provides a fully customizable, accessible, and visually appealing custom radio button experience.