Enjoy this long list of open source CSS Radio Button code examples. They are open-source, so you can integrate them into your website very quickly — and for free.
1. Styled CSS Radio Buttons
As you select different options, watch the dot worm hop from the previous to the current! Concept based on this Dribbble shot. Update 1/24/20: iOS Safari-related fix
Heres an interaction which allows you to change your card theme. We focused on the two simple micro animations which indicate the accomplishment of changing your card – which is quite a joyful moment for most users. This Pen is a recreation of a Dribbble Shot, which can be viewed here: ht.
8. Mobile Radio Buttons With Small Animation
Mobile Radio Buttons as real buttons, simple look and feel with a small animation. Easy to use and to handle.
9. Checkboxes And Radios (dark/light) — Pure CSS
10. Animated SVG Radio Buttons
11. Get Fit CSS Radio Inputs
12. Underground Radios
The radio button highlight travels underground Inspired by this pen: https://dribbble.com/shots/4214518-Radio-button
13. Radio Button Dot-Slider (Pure CSS)
A range-click slider with a sliding dot indicator, labels, validity-conditional styling, and NO JS. Works 100% on JS-restricted sites.
14. Responsive Toggle Switch
A toggle switch to use in your forms (using radio inputs as the core) that is responsive. Styled with CSS, using Flexbox for sizing.
15. Minimal CSS Radio Inputs
It’s been decades — checkboxes and radio buttons still look terrible and don’t play well without some love. Starting with well-formed HTML, look what we can do with a bit of style and some Font Awesome glyphs.
16. CSS Radio Buttons Interaction ✨
17. MacOS Mojave Dark Mode Checkbox & Radio Buttons
18. Radio Input
19. Material Design Radio Buttons
20. [CSS] Alignment Button
CSS tricks: use flex-grow for transition. inspired from Alignment button Pair programming with RulinShyu & yuanyu, @ Happy CSSer — 27 by VS Code Live Share
In this article, you will learn everything about the HTML radio button. Its use, how to group it, how to style it using CSS, adding triggering events on it using JavaScript, etc.
You must have filled some online form before and would have interacted with options in a form where you have multiple choices to select but only one can be selected. Example gender selection , blood group selection , etc.
These are called radio buttons . Ideally, only one radio button can be selected for a single choice. For example, you can either select male , female , or transgender for gender selection.
HTML Radio Button
A radio button is an element of type=»radio» which is used to select only one choice among multiple choices.
It is generally used within a form but can be used alone.
To create a radio button give element a type of radio . Example
Here is an example of an HTML radio button:
HTML radio button example
You can check the above output by clicking on the button and you will observe that not only one choice but multiple choices can be selected above. Thinking why? 💭🤔
This is because none of the radio buttons are connected to each other with a common purpose. They are all independent and can be used for different purposes that are multiple of them are selected at a time.
To connect them together give them a common purpose by specifying name attribute.
HTML Radio Button Group
Practically radio buttons are used in group . So that when one button is selected then the other already get unselected if it was previously selected.
In a group of radio buttons, all the radio elements are connected. Only one button can be selected at a time.
Here is an example where radio buttons are not part of a group .
Radio buttons in the above example are not part of any group.
A radio group is defined by the name attribute. When 2 or more radio buttons are in a group they have the same name attribute.
When multiple radio buttons are given the same name then it becomes part of a group and when you select any radio button of that group it automatically deselects any other selected button in that group.
In the above example, all three radio buttons are part of a single group with name=»contact» which put them in a single group.
Radio Button Value
All radio buttons shown above have no value. This means if you select one then they don’t know for what value you want to select it.
Just an option written in front of it doesn’t mean it will take it as input.
To specify value for radio button give it a value attribute. Example
Now if you select any radio button then it will take the value of that radio button as input.
You have done a lot😊 let’s relax for a minute🍸🍹
What we have covered till now:
Radio button is created using element having type=»radio»
Radio button must have name attribute to be part of a group.
Radio button should have value attribute to specify value for that radio button.
Checked Radio Button
It is better to select the most probable radio button by default because if no option is selected while submitting the form then the radio group is not included in the submitted data at all.
You can do it by using the checked attribute.
checked attribute is a boolean attribute. It is either true or false .
Just using checked is same as checked=»true» .
Example: HTML radio button default selected
Radio Button Attributes
Here is a brief description of some frequently used attributes in radio buttons.
Attribute
description
type
It specifies input type, it must be added to create the type of button you want, i.e. radio
name
It creates a radio group, 1 or more than one radio buttons with the same name comes under same group
value
It delivers the selected button to the server if the radio button is checked
checked
It selects a radio button by default
Radio Button Label
Radio buttons has a very small clickable area, you have to pin-point the small box to click it. But you can increase the clickable area by using a label with a radio button.
Radio button label is created using element.
Radio button label must have a for attribute that targets the radio button.
And the radio button must have an id attribute that with the same value is used in for attribute.
Example: HTML radio button label
Now the clickable area of the radio button is increased. You can select any radio button either by clicking on the label or by clicking on the radio button.
Radio button onclick
You can trigger an event to know which radio button is checked. For this select all radio buttons and add an onclick event to all button.
When the button is checked the event will be triggered and you can alert a message with it.
// selecting radio button const buttons = document.querySelectorAll("input[type='radio']"); // adding event to all radio buttons buttons.forEach(button => < button.onclick = () => < if (button.checked) < alert(button.value + " selected as contact option!"); >> >)
Radio Button Style
Create your own custom radio button and style it using CSS since the browser’s radio button is styled by the browser itself.
Let the HTML code for the form with radio button be as follows:
Here is complete CSS code to create a custom styled radio button.
When a form is submitted with a radio button then the form’s data include an entry in the form name=value , here in the above case, one option could be contact=email .
If there is no value attribute in a radio button then data related to the radio button is not reported to the server.
Conclusion
You learned about radio buttons. Creating a group of radio buttons. Different types of attributes in it. Triggering an event when the button is checked and learned to style radio buttons.