Explain Codes LogoExplain Codes Logo

How do I change the color of radio buttons?

css
responsive-design
best-practices
accessibility
Anton ShumikhinbyAnton Shumikhin·Jan 13, 2025
TLDR

Quickly customize your radio button color with a custom CSS style paired with the :before pseudo-element for a truly fetching visual.

HTML:

<input type="radio" id="radioExample" name="group" hidden> <label for="radioExample" class="radio-style"></label>

CSS:

.radio-style { display: inline-block; width: 20px; height: 20px; border-radius: 50%; /* Makes it round, just like a good pizza! */ background: #eee; /* Unchecked color - subtle and elegant like a gray squirrel */ border: 2px solid #ccc; /* Strengthens the button, like spinach to Popeye */ } .radio-style::before { content: ''; display: inline-block; width: 100%; height: 100%; border-radius: 50%; /* Keep the round shape consistent. No square pizzas here! */ background: green; /* Button color. Because who doesn't love green! */ opacity: 0; /* Hide the green, for now! Like a chameleon */ transition: opacity 0.3s; /* Smooth change, smoother than a fresh jar of Skippy */ } #radioExample:checked + .radio-style::before { opacity: 1; /* Now you see me! */ }

We toggle the opacity in ::before, revealing the color when the radio button is checked.

Alter the background of .radio-style for the unchecked state and ::before for the active state's color.

Taming browser peculiarities

Modern browsers and accent-color

The newly introduced accent-color property in CSS gives us a magic wand to color UI elements like radio buttons. But remember, old browsers might behave like grumpy cats towards it:

input[type='radio'] { accent-color: #ff4500; /* Orangered, to add an orangey zest to your day! */ }

This works like a charm on Chrome/Edge 93+, Firefox 92+, and Safari 15.4+. Don't forget to check the browser compatibility for the new CSS trick you learn!

Manoeuvring position & Size

The position: relative for the parent and position: absolute for the :after pseudo-element lets you keep your custom elements reined in:

.radio-style { position: relative; /* Parent's position. Like Gandalf saying "You Shall Not Pass" to alignment issues! */ } .radio-style::after { position: absolute; /* Child's position. Like a young adventurer exploring within the permitted boundaries. */ top: 5px; left: 5px; }

Scale the size of your custom radio buttons using transform: scale(), accommodating different designs without extra JavaScript or images.

Interaction & Alignment

Align custom styles to perfection

When setting custom radio buttons in grid systems, use flexbox or grid layouts for a polished look:

.container { display: flex; align-items: center; /* Pure vertical alignment, like a monk in meditation */ justify-content: center; /* All roads lead to Rome, or in this case, the center of the page */ }

Don't forget mouseover (:hover), focus (:focus), and active (:active) states to keep the user engaged, like a good book or a riveting movie.

Building shields against limitations

Advanced customization might call for custom images or JavaScript libraries like Ryan Fait's. Remember—when CSS has its hands full, these alternatives come to your rescue:

  • Custom images act as radio buttons, toggling visibility as needed.
  • Up the ante with JavaScript by animating the selection process, possibly even adding sound effects or haptic feedback.

Intelligent customization

Checkbox Hack: For the JavaScript skeptics

The Checkbox Hack lets you toggle display changes using label elements and the :checked pseudo-class. It's like a secret portal that leads to a room full of display possibilities:

<input type="checkbox" id="toggle" hidden> <label for="toggle" class="button">Toggle</label>

Embrace the subsequent sibling combinator (+) and the :checked pseudo-class to kickstart the style revolution when the checkbox is toggled:

#toggle:checked + .button { background-color: #4CAF50; /* Green, because it's not easy being green. Unless you're this button, of course */ }

Inclusivity in design

Accessible, stylish radio buttons

When styling custom radio buttons, let's not forget about accessibility. Make your buttons screen reader-friendly, ensure their structure is semantically accurate, and offer focus states. Remember, inclusivity makes the world a better place!