Styling input radio buttons [duplicate] - html

This question already has answers here:
CSS - How to Style a Selected Radio Buttons Label?
(8 answers)
Closed 4 years ago.
I have already looked on here at how to style the input buttons I found some good answers but for some reason I can not style the inside of the button when checked. Take a look at my code maybe I did something wrong
.radio input[type='radio'] {
display: none; /*removes original button*/
}
.radio label:before { /*styles outer circle*/
content: " ";
display: inline-block;
position: relative;
top: 5px;
margin: 0 5px 0 0;
width: 20px;
height: 20px;
border-radius: 11px;
border: 2px solid orange;
background-color: transparent;
}
.radio label input[type='radio']:checked + label:after { /*styles inside circle*/
border-radius: 11px;
width: 12px;
height: 12px;
position: absolute;
top: 9px;
left: 10px;
content: " ";
display: block;
background-color: blue;
}
<div class="radio">
<label><input type="radio" name="gender" value="male"> Male</label>
<label><input type="radio" name="gender" value="female"> Female</label>
<label><input type="radio" name="gender" value="other"> Other</label>
</div>

You can't go backwards in css, thats why your code doesn't work
You need to add a span inside the label after your input and you can style it when the radio is checked
see code snippet:
.radio input[type='radio'] {
display: none;
/*removes original button*/
}
.radio label:before {
/*styles outer circle*/
content: " ";
display: inline-block;
position: relative;
top: 5px;
margin: 0 5px 0 0;
width: 20px;
height: 20px;
border-radius: 11px;
border: 2px solid orange;
background-color: transparent;
}
.radio label {
position: relative;
}
.radio label input[type='radio']:checked+span {
/*styles inside circle*/
border-radius: 11px;
width: 12px;
height: 12px;
position: absolute;
top: 1px;
left: 6px;
display: block;
background-color: blue;
}
<div class="radio">
<label><input type="radio" name="gender" value="male"> Male<span></span></label>
<label><input type="radio" name="gender" value="female"> Female<span></span></label>
<label><input type="radio" name="gender" value="other"> Other<span></span></label>
</div>

Please see below.
I have put the label behind the input field and redefined the CSS for label:before when the button is checked.
You had the input field as a child of the label but in the CSS it was approached as a sibling.
input[type='radio'] {
display: none;
}
label:before {
content: " ";
display: inline-block;
position: relative;
top: 5px;
margin: 0 5px 0 0;
width: 20px;
height: 20px;
border-radius: 11px;
border: 2px solid orange;
background-color: transparent;
}
input[type='radio']:checked+label:before {
position: relative;
border: 2px solid orange;
border-radius: 11px;
width: 20px;
height: 20px;
top: 5px;
content: " ";
display: inline-block;
background-color: blue;
}
<div class="radio">
<input id="male" type="radio" name="gender" value="male">
<label for="male">Male</label>
<input id="female" type="radio" name="gender" value="female">
<label for="female">Female</label>
<input id="other" type="radio" name="gender" value="other">
<label for="other">Other</label>
</div>

So your selector is incorrect and so is your html structure. You need to move your labels after your inputs (and target the input using a for attribute on the label and id on the input), then remove the first label from your selector
I have commented the css I have changed below
.radio input[type='radio'] {
display: none; /*removes original button*/
}
.radio label { /* add this so dot is relative to the label */
position: relative;
display: inline-block;
}
.radio label:before { /*styles outer circle*/
content: " ";
display: inline-block;
top: 5px;
margin: 0 5px 0 0;
width: 20px;
height: 20px;
border-radius: 11px;
border: 2px solid orange;
background-color: transparent;
vertical-align:middle; /* add this so text is vertically centred next to ring */
}
/* remove the first label from this selector */
.radio input[type='radio']:checked + label:after { /*styles inside circle*/
border-radius: 50%;
width: 12px;
height: 12px;
position: absolute;
top: 6px; /* I have changed the top and left so this is in the centre */
left: 6px;
content: " ";
display: block;
background-color: blue;
}
<div class="radio">
<input type="radio" name="gender" value="male" id="male"><label for="male">Male</label>
<input type="radio" name="gender" value="female" id="female"><label for="female"> Female</label>
<input type="radio" name="gender" value="other" id="other"><label for="other"> Other</label>
</div>

Related

How to make radio button like on/off switches using css

I have requirement of radio buttons with on/off method i.e when click on one radio button it should on and when click on second button first button should off but below code is not working as per my expectation.
Here is the snippet for that html and css code:
body {
background: #0288D1;
}
.checkboxes-and-radios {
margin: 80px auto 0;
width: 280px;
padding: 30px;
background: #fafafa;
input {
display: none;
}
label {
cursor: pointer;
padding-right: 35px;
position: relative;
display: block;
font-size: 18px;
padding: 15px 0
}
input[type="checkbox"],
input[type="radio"] {
position: absolute;
visibility: hidden !important;
}
input[type="checkbox"]+label,
input[type="radio"]+label {
&:before,
&:after {
content: '';
position: absolute;
top: 50%;
margin-top: -7.5px;
box-sizing: border-box;
}
&:before {
width: 30px;
height: 15px;
right: 0px;
background: #fff;
border: 1px solid #e4e3e1;
border-radius: 15px;
}
&:after {
width: 15px;
height: 15px;
right: 15px;
background: #BDBDBD;
border-radius: 50%;
transition: all 200ms ease-out;
}
}
input[type="checkbox"]:checked+label,
input[type="radio"]:checked+label {
&:after {
right: 0px;
background: #FF9800;
}
}
}
<div class="checkboxes-and-radios">
<h1>Radios:</h1>
<input type="radio" name="radio-cats" id="radio-1" value="1" checked>
<label for="radio-1">Radio Label 1</label>
<input type="radio" name="radio-cats" id="radio-2" value="2">
<label for="radio-2">Radio Label 2</label>
<input type="radio" name="radio-cats" id="radio-3" value="3" checked>
<label for="radio-3">Radio Label 3</label>
<h1>Checkboxes:</h1>
<input type="checkbox" name="checkbox-cats[]" id="checkbox-1" value="1" checked>
<label for="checkbox-1">Checkbox Label 1</label>
<input type="checkbox" name="checkbox-cats[]" id="checkbox-2" value="2">
<label for="checkbox-2">Checkbox Label 2</label>
<input type="checkbox" name="checkbox-cats[]" id="checkbox-3" value="3" checked>
<label for="checkbox-3">Checkbox Label 3</label>
</div>
expected output:
so how to add css that will look like in image shown in top?
Your code works like a charm for me. In your provided fiddle, the SCSS was not compiled to CSS. Here a compiled version. Radio buttons are switching correctly.
Here also a version on CodePen.
Your CSS is not CSS but SCSS, which has to be compiled to CSS.
You have to use a preprocessor to compile scss to css. You should start reading here:
http://sass-lang.com
body {
background: #0288D1;
}
.checkboxes-and-radios {
margin: 80px auto 0;
width: 280px;
padding: 30px;
background: #fafafa;
}
.checkboxes-and-radios input {
display: none;
}
.checkboxes-and-radios label {
cursor: pointer;
padding-right: 35px;
position: relative;
display: block;
font-size: 18px;
padding: 15px 0;
}
.checkboxes-and-radios input[type="checkbox"],
.checkboxes-and-radios input[type="radio"] {
position: absolute;
visibility: hidden !important;
}
.checkboxes-and-radios input[type="checkbox"]+label:before,
.checkboxes-and-radios input[type="checkbox"]+label:after,
.checkboxes-and-radios input[type="radio"]+label:before,
.checkboxes-and-radios input[type="radio"]+label:after {
content: '';
position: absolute;
top: 50%;
margin-top: -7.5px;
box-sizing: border-box;
}
.checkboxes-and-radios input[type="checkbox"]+label:before,
.checkboxes-and-radios input[type="radio"]+label:before {
width: 30px;
height: 15px;
right: 0px;
background: #fff;
border: 1px solid #e4e3e1;
border-radius: 15px;
}
.checkboxes-and-radios input[type="checkbox"]+label:after,
.checkboxes-and-radios input[type="radio"]+label:after {
width: 15px;
height: 15px;
right: 15px;
background: #BDBDBD;
border-radius: 50%;
transition: all 200ms ease-out;
}
.checkboxes-and-radios input[type="checkbox"]:checked+label:after,
.checkboxes-and-radios input[type="radio"]:checked+label:after {
right: 0px;
background: #FF9800;
}
<div class="checkboxes-and-radios">
<h1>Radios:</h1>
<input type="radio" name="radio-cats" id="radio-1" value="1" checked>
<label for="radio-1">Radio Label 1</label>
<input type="radio" name="radio-cats" id="radio-2" value="2">
<label for="radio-2">Radio Label 2</label>
<input type="radio" name="radio-cats" id="radio-3" value="3" checked>
<label for="radio-3">Radio Label 3</label>
<h1>Checkboxes:</h1>
<input type="checkbox" name="checkbox-cats1" id="checkbox-1" value="1" checked>
<label for="checkbox-1">Checkbox Label 1</label>
<input type="checkbox" name="checkbox-cats2" id="checkbox-2" value="2">
<label for="checkbox-2">Checkbox Label 2</label>
<input type="checkbox" name="checkbox-cats3" id="checkbox-3" value="3" checked>
<label for="checkbox-3">Checkbox Label 3</label>
</div>

How to set Radio Button Colors [duplicate]

I mean, a radio button itself consists of a round shape and a dot at the center (when the button is selected). What I want to change is the color of both. Can this be done using CSS?
A quick fix would be to overlay the radio button input style using :after, however it's probably a better practice to create your own custom toolkit.
input[type='radio']:after {
width: 15px;
height: 15px;
border-radius: 15px;
top: -2px;
left: -1px;
position: relative;
background-color: #d1d3d1;
content: '';
display: inline-block;
visibility: visible;
border: 2px solid white;
}
input[type='radio']:checked:after {
width: 15px;
height: 15px;
border-radius: 15px;
top: -2px;
left: -1px;
position: relative;
background-color: #ffa500;
content: '';
display: inline-block;
visibility: visible;
border: 2px solid white;
}
<input type='radio' name="gender"/>
<input type='radio' name="gender"/>
A radio button is a native element specific to each OS/browser. There is no way to change its color/style, unless you want to implement custom images or use a custom Javascript library which includes images (e.g. this - cached link)
As Fred mentioned, there is no way to natively style radio buttons in regards to color, size, etcc. But you can use CSS Pseudo elements to setup an impostor of any given radio button, and style it. Touching on what JamieD said, on how we can use the :after Pseudo element, you can use both :before and :after to achieve a desirable look.
Benefits of this approach:
Style your radio button and also Include a label for content.
Change the outer rim color and/or checked circle to any color you like.
Give it a transparent look with modifications to background color property and/or optional use of the opacity property.
Scale the size of your radio button.
Add various drop shadow properties such as CSS drop shadow inset where needed.
Blend this simple CSS/HTML trick into various Grid systems, such as Bootstrap 3.3.6, so it matches the rest of your Bootstrap components visually.
Explanation of short demo below:
Set up a relative in-line block for each radio button
Hide the native radio button sense there is no way to style it directly.
Style and align the label
Rebuilding CSS content on the :before Pseudo-element to do 2 things - style the outer rim of the radio button and set element to appear first (left of label content). You can learn basic steps on Pseudo-elements here - http://www.w3schools.com/css/css_pseudo_elements.asp
If the radio button is checked, request for label to display CSS content (the styled dot in the radio button) afterwards.
The HTML
<div class="radio-item">
<input type="radio" id="ritema" name="ritem" value="ropt1">
<label for="ritema">Option 1</label>
</div>
<div class="radio-item">
<input type="radio" id="ritemb" name="ritem" value="ropt2">
<label for="ritemb">Option 2</label>
</div>
The CSS
.radio-item {
display: inline-block;
position: relative;
padding: 0 6px;
margin: 10px 0 0;
}
.radio-item input[type='radio'] {
display: none;
}
.radio-item label {
color: #666;
font-weight: normal;
}
.radio-item label:before {
content: " ";
display: inline-block;
position: relative;
top: 5px;
margin: 0 5px 0 0;
width: 20px;
height: 20px;
border-radius: 11px;
border: 2px solid #004c97;
background-color: transparent;
}
.radio-item input[type=radio]:checked + label:after {
border-radius: 11px;
width: 12px;
height: 12px;
position: absolute;
top: 9px;
left: 10px;
content: " ";
display: block;
background: #004c97;
}
A short demo to see it in action
In conclusion, no JavaScript, images or batteries required. Pure CSS.
You can use the CSS accent-color property to change the color.
input[type='radio'] {
accent-color: #232323;
}
It works with Chrome/Edge 93+, Firefox 92+, and Safari 15.4+ (Browser support info from caniuse.)
You can achieve customized radio buttons in two pure CSS ways
Via removing standard appearance using CSS appearance and applying custom appearance. Unfortunately this was doesn't work in IE. Demo:
input[type="radio"] {
/* remove standard background appearance */
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
/* create custom radiobutton appearance */
display: inline-block;
width: 25px;
height: 25px;
padding: 6px;
/* background-color only for content */
background-clip: content-box;
border: 2px solid #bbbbbb;
background-color: #e7e6e7;
border-radius: 50%;
}
/* appearance for checked radiobutton */
input[type="radio"]:checked {
background-color: #93e026;
}
/* optional styles, I'm using this for centering radiobuttons */
.flex {
display: flex;
align-items: center;
}
<div class="flex">
<input type="radio" name="radio" id="radio1" />
<label for="radio1">RadioButton1</label>
</div>
<div class="flex">
<input type="radio" name="radio" id="radio2" />
<label for="radio2">RadioButton2</label>
</div>
<div class="flex">
<input type="radio" name="radio" id="radio3" />
<label for="radio3">RadioButton3</label>
</div>
Via hiding radiobutton and setting custom radiobutton appearance to label's pseudoselector. By the way no need for absolute positioning here (I see absolute positioning in most demos). Demo:
*,
*:before,
*:after {
box-sizing: border-box;
}
input[type="radio"] {
display: none;
}
input[type="radio"]+label:before {
content: "";
/* create custom radiobutton appearance */
display: inline-block;
width: 25px;
height: 25px;
padding: 6px;
margin-right: 3px;
/* background-color only for content */
background-clip: content-box;
border: 2px solid #bbbbbb;
background-color: #e7e6e7;
border-radius: 50%;
}
/* appearance for checked radiobutton */
input[type="radio"]:checked + label:before {
background-color: #93e026;
}
/* optional styles, I'm using this for centering radiobuttons */
label {
display: flex;
align-items: center;
}
<input type="radio" name="radio" id="radio1" />
<label for="radio1">RadioButton1</label>
<input type="radio" name="radio" id="radio2" />
<label for="radio2">RadioButton2</label>
<input type="radio" name="radio" id="radio3" />
<label for="radio3">RadioButton3</label>
Only if you are targeting webkit-based browsers (Chrome and Safari, maybe you are developing a Chrome WebApp, who knows...), you can use the following:
input[type='radio'] {
-webkit-appearance: none;
}
And then style it as if it were a simple HTML element, for example applying a background image.
Use input[type='radio']:active for when the input is selected, to provide the alternate graphics
Update: As of 2018 you can add the following to support multiple browser vendors:
input[type="radio"] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
Try something like this:
#yes{
border:2px solid white;
box-shadow:0 0 0 1px #392;
appearance:none;
border-radius:50%;
width:12px;
height:12px;
background-color:#fff;
transition:all ease-in 0.2s;
}
#yes:checked{
background-color:#392;
}
#no{
border:2px solid white;
box-shadow:0 0 0 1px #932;
appearance:none;
border-radius:50%;
width:12px;
height:12px;
background-color:#fff;
transition:all ease-in 0.2s;
}
#no:checked{
background-color:#932;
}
<input id="yes" type="radio" name="s"><label for="yes">Yes</label></br>
<input id="no" type="radio" name="s"><label for="no">No</label>
There is less of code, it looks better and you don't need to play with :before , :after and position to reach the effect.
you can use the checkbox hack as explained in css tricks
http://css-tricks.com/the-checkbox-hack/
working example of radio button:
http://codepen.io/Angelata/pen/Eypnq
input[type=radio]:checked ~ .check {}
input[type=radio]:checked ~ .check .inside{}
Works in IE9+, Firefox 3.5+, Safari 1.3+, Opera 6+, Chrome anything.
simple cross browser custom radio button example for you
.checkbox input{
display: none;
}
.checkbox input:checked + label{
color: #16B67F;
}
.checkbox input:checked + label i{
background-image: url('http://kuzroman.com/images/jswiddler/radio-button.svg');
}
.checkbox label i{
width: 15px;
height: 15px;
display: inline-block;
background: #fff url('http://kuzroman.com/images/jswiddler/circle.svg') no-repeat 50%;
background-size: 12px;
position: relative;
top: 1px;
left: -2px;
}
<div class="checkbox">
<input type="radio" name="sort" value="popularity" id="sort1">
<label for="sort1">
<i></i>
<span>first</span>
</label>
<input type="radio" name="sort" value="price" id="sort2">
<label for="sort2">
<i></i>
<span>second</span>
</label>
</div>
https://jsfiddle.net/kuzroman/ae1b34ay/
Well to create extra elements we can use :after, :before (so we don’t have to change the HTML that much). Then for radio buttons and checkboxes we can use :checked. There are a few other pseudo elements we can use as well (such as :hover). Using a mixture of these we can create some pretty cool custom forms. check this
I builded another fork of #klewis' code sample to demonstrate some playing with pure css and gradients by using :before/:after pseudo elements and a hidden radio input button.
HTML:
sample radio buttons:
<div style="background:lightgrey;">
<span class="radio-item">
<input type="radio" id="ritema" name="ritem" class="true" value="ropt1" checked="checked">
<label for="ritema">True</label>
</span>
<span class="radio-item">
<input type="radio" id="ritemb" name="ritem" class="false" value="ropt2">
<label for="ritemb">False</label>
</span>
</div>
:
CSS:
.radio-item input[type='radio'] {
visibility: hidden;
width: 20px;
height: 20px;
margin: 0 5px 0 5px;
padding: 0;
}
.radio-item input[type=radio]:before {
position: relative;
margin: 4px -25px -4px 0;
display: inline-block;
visibility: visible;
width: 20px;
height: 20px;
border-radius: 10px;
border: 2px inset rgba(150,150,150,0.75);
background: radial-gradient(ellipse at top left, rgb(255,255,255) 0%, rgb(250,250,250) 5%, rgb(230,230,230) 95%, rgb(225,225,225) 100%);
content: "";
}
.radio-item input[type=radio]:checked:after {
position: relative;
top: 0;
left: 9px;
display: inline-block;
visibility: visible;
border-radius: 6px;
width: 12px;
height: 12px;
background: radial-gradient(ellipse at top left, rgb(245,255,200) 0%, rgb(225,250,100) 5%, rgb(75,175,0) 95%, rgb(25,100,0) 100%);
content: "";
}
.radio-item input[type=radio].true:checked:after {
background: radial-gradient(ellipse at top left, rgb(245,255,200) 0%, rgb(225,250,100) 5%, rgb(75,175,0) 95%, rgb(25,100,0) 100%);
}
.radio-item input[type=radio].false:checked:after {
background: radial-gradient(ellipse at top left, rgb(255,225,200) 0%, rgb(250,200,150) 5%, rgb(200,25,0) 95%, rgb(100,25,0) 100%);
}
.radio-item label {
display: inline-block;
height: 25px;
line-height: 25px;
margin: 0;
padding: 0;
}
preview:
https://www.codeply.com/p/y47T4ylfib
For those who prefer to start development with a minimal example, here's a simple custom radio button that doesn't depend on label:
[type="radio"] {
visibility: hidden; /* hide default radio button */
/* you may need to adjust margin here, too */
}
[type="radio"]::before { /* create pseudoelement */
border: 2px solid gray; /* thickness, style, color */
height: .9em; /* height adjusts with font */
width: .9em; /* width adjusts with font */
border-radius: 50%; /* make it round */
display: block; /* or flex or inline-block */
content: " "; /* won't display without this */
cursor: pointer; /* appears clickable to mouse users */
visibility: visible; /* reverse the 'hidden' above */
}
[type="radio"]:checked::before { /* selected */
/* add middle dot when selected */
/* slightly bigger second value makes it smooth */
/* even more (e.g., 20% 50%) would make it fuzzy */
background: radial-gradient(gray 36%, transparent 38%);
}
<br>
<input type="radio" name="example" id="one" value="one">
<label for="one">one</label>
<br>
<br>
<input type="radio" name="example" id="two" value="two">
<label for="two">two</label>
Try this css with transition:
Demo
$DarkBrown: #292321;
$Orange: #CC3300;
div {
margin:0 0 0.75em 0;
}
input[type="radio"] {
display:none;
}
input[type="radio"] + label {
color: $DarkBrown;
font-family:Arial, sans-serif;
font-size:14px;
}
input[type="radio"] + label span {
display:inline-block;
width:19px;
height:19px;
margin:-1px 4px 0 0;
vertical-align:middle;
cursor:pointer;
-moz-border-radius: 50%;
border-radius: 50%;
}
input[type="radio"] + label span {
background-color:$DarkBrown;
}
input[type="radio"]:checked + label span{
background-color:$Orange;
}
input[type="radio"] + label span,
input[type="radio"]:checked + label span {
-webkit-transition:background-color 0.4s linear;
-o-transition:background-color 0.4s linear;
-moz-transition:background-color 0.4s linear;
transition:background-color 0.4s linear;
}
Html :
<div>
<input type="radio" id="radio01" name="radio" />
<label for="radio01"><span></span>Radio Button 1</label>
</div>
<div>
<input type="radio" id="radio02" name="radio" />
<label for="radio02"><span></span>Radio Button 2</label>
</div>
Simple , you can be used accent-color
View page source
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
input[type=radio] {
accent-color: red;
}
</style>
</head>
<body>
<label for="css">Are you like to css</label>
<input type="radio" id="css" value="css">
</body>
</html>
You should use the accent-color CSS property, which sets the accent color for user-interface controls such as inputs (radio buttons, checkboxes...) or progress bars and it's supported for most modern browsers.
input {
accent-color: red;
}
document.querySelector("input[name=accent-color]").addEventListener("input", () => {
document.documentElement.style.setProperty("--accent-color", event.target.value);
});
:root {
--accent-color: red;
}
input,
progress {
accent-color: var(--accent-color);
}
/* Other styles */
label {
display: flex;
align-items: center;
gap: .625rem;
margin-bottom: .625rem;
}
label:first-child {
font-size: 1.15rem;
font-weight: bold;
}
input {
flex: 0 0 auto;
height: 1.25rem;
width: 1.25rem;
}
input[type="color"] {
width: 3rem;
}
input[type="range"] {
width: 12.5rem;
}
<label>Change the accent color<input name="accent-color" type="color" value="#ff0000"></input></label><br>
<label><input name="radio" type="radio" checked></input>Radio button</label>
<label><input name="radio" type="radio"></input>Another radio button</label>
<label><input name="check" type="checkbox" checked></input>Checkbox</label>
<label><input name="range" type="range"></input>Range input</label>
<label><progress value="50" max="100"></progress>Progress bar</label>
This is not possible by native CSS. You'll have to use background images and some javascript tricks.
As other said, there's no way to achieve this in all browser, so best way of doing so crossbrowser is using javascript unobtrusively. Basically you have to turn your radiobutton into links (fully customizable via CSS). each click on link will be bound to the related radiobox, toggling his state and all the others.
For my use all I wanted to do was change the colour and nothing else, so I've taken the answer from #klewis and changed it to...
Make the radio the same as the browser default (Chrome in my case) using relative % and em instead of fixed px. Caveat: em is based on whatever the font-size of input[type=radio] is, which could be inherited. Adjustments to the values below may be necessary.
Keep accessibility functions (like an outline when focused) of the original radio button by not using display: none; and by applying :before and :after to the original radio instead of the label.
/* make default radio 'invisible' */
input[type=radio] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
/* make new radio outer circle */
input[type=radio]:before {
content: " ";
display: inline-block;
position: relative;
width: 0.8em;
height: 0.8em;
border-radius: 50%;
border: 1px solid grey;
background-color: transparent;
}
/* change colour of radio outer circle when checked */
input[type=radio]:checked:before {
border-color: green;
}
/* make new radio inner circle when checked */
input[type=radio]:checked:after {
content: " ";
display: block;
position: absolute;
width: 0.55em;
height: 0.55em;
border-radius: 50%;
top: 0.4em;
left: 0.13em;
background: green;
}
`
This Worked for me well,
Simply add css attribute:
input[type="radio"]{accent-color: red;}
Here is the link for resource
The simple way is to use accent-color
The accent-color CSS property sets the accent color for user-interface controls generated by some elements
Browsers that support accent-color currently apply it to the following HTML elements:
<input type="checkbox">
<input type="radio">
<input type="range">
<progress>
An runnable example
body {
display: grid;
padding: 3rem 0;
}
.accent {
accent-color: #30cc7e;
}
form {
display: grid;
grid-auto-columns: fit-content(50%);
grid-template-areas: "a a";
margin: auto;
padding: 0;
gap: 1rem;
}
form {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 2rem;
margin: auto;
}
form section:first-child {
color-scheme: light;
}
form section:last-child {
color-scheme: dark;
}
fieldset {
border-radius: 8px;
color-scheme: light;
display: flex;
flex: 1;
flex-direction: column;
gap: 1rem;
padding: 1rem;
}
.dark {
color-scheme: dark;
}
.dark fieldset {
background: #100f33;
border-color: #100f33;
color: #fff;
}
.dark .accent {
accent-color: hsla(180, 100%, 70%, 1);
}
h2 {
margin: 0;
}
.notice {
background: #fff9c4;
border-radius: 6px;
margin: 1.5rem auto;
padding: 0.5rem;
text-align: center;
}
#supports (accent-color: #fff) {
.notice {
display: none;
}
}
<div class="notice">
Your browser does not support the <code>accent-color</code> property.
</div>
<form action="">
<fieldset>
<h2>Checkboxes</h2>
<div>
<label for="checkbox">
Default
</label>
<input id="checkbox" type="checkbox" checked>
</div>
<div>
<label for="checkbox-accent">
Accent
</label>
<input id="checkbox-accent" type="checkbox" class="accent" checked>
</div>
</fieldset>
<fieldset>
<h2>Radio</h2>
<div>
<input id="radio" type="radio" checked>
<label for="radio">
Default
</label>
</div>
<div>
<input id="radio-accent" type="radio" class="accent" checked>
<label for="radio-accent">
Accent
</label>
</div>
</fieldset>
<fieldset>
<h2>Progress</h2>
<div>
<label for="progress">
Default
</label>
<progress id="progress" min="0" max="100" value="50"></progress>
</div>
<div>
<label for="progress-accent">
Accent
</label>
<progress id="progress-accent" class="accent" min="0" max="100" value="50"></progress>
</div>
</fieldset>
<fieldset>
<h2>Range</h2>
<div>
<label for="range">
Default
</label>
<input id="range" type="range">
</div>
<div>
<label for="range-accent">
Accent
</label>
<input id="range-accent" class="accent" type="range">
</div>
</fieldset>
</form>
You can use accent-color property in css to change background color of both checkbox and radio buttons.
input[type=radio] {
accent-color: red;
}
It may be helpful to bind radio-button to styled label. Futher details in this answer.
A clever way to do it would be to create a separate div with a height and width of -for example- 50px and then a radius of 50px lay this over your radio buttons...
You can embed a span element in the radio input then select a color of your choice to be rendered when a radio input is checked. Check out the example below sourced from w3schools.
<!DOCTYPE html>
<html>
<style>
/* The container */
.container {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default radio button */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create a custom radio button */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
border-radius: 50%;
}
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #ccc;
}
/* When the radio button is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: #00a80e;
}
/* Create the indicator (the dot/circle - hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the indicator (dot/circle) when checked */
.container input:checked ~ .checkmark:after {
display: block;
}
/* Style the indicator (dot/circle) */
.container .checkmark:after {
top: 9px;
left: 9px;
width: 8px;
height: 8px;
border-radius: 50%;
background: white;
}
</style>
<body>
<h1>Custom Radio Buttons</h1>
<label class="container">One
<input type="radio" checked="checked" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Two
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Three
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Four
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
</body>
Changing the background color at this code segment below does the trick.
/* When the radio button is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: #00a80e;
}
Sourced from how to create a custom radio button
If you are using react bootstrap Form.check you could do something like this
HTML
<Form.Check
type="radio"
id="Radio-card"
label={`check me out`}
name="paymentmethod"
value="card"
/>
SCSS
.form-check {
display: flex;
align-items: center;
input[type="radio"] {
-moz-appearance: none;
appearance: none;
width: 11px;
height: 11px;
padding: 1px;
background-clip: content-box;
border: 1px solid hotpink;
background-color: white;
border-radius: 50%;
}
input[type="radio"]:checked {
outline: none;
background-color: hotpink;
border: 1px solid hotpink;
}
label {
font-size: 14px;
font-weight: 600;
}
}
I changed the color and size of radio buttons. Try This
.radio-tile-group {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
}
.radio-tile-group .input-container {
position: relative;
margin: 0.9rem;
}
.radio-tile-group .input-container .radio-button {
opacity: 0;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
margin: 0;
cursor: pointer;
}
.radio-tile {
border: 1px solid #eea236;
}
.radio-tile-group .input-container .radio-tile-edit {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 25px;
font-size: 12px;
border-radius: 5px;
padding: 0.2rem;
transition: transform 300ms ease;
height: 25px;
}
#media (min-width: 375px) and (max-width: 812px) {
.radio-tile-group .input-container .radio-tile {
margin-inline: 18px;
}
}
.radio-tile-group .input-container .radio-button:checked+.radio-tile {
border: 3px solid #2980b9;
font-size: 12px;
color: #797979;
transform: scale(1.05, 1.05);
}
.radio-tile-group .input-container .radio-button:checked+.radio-tile .icon svg {
fill: white;
background-color: #2980b9;
}
.radio-tile-group .input-container .radio-button:checked+.radio-tile-edit {
border: 3px solid black;
/* font-size: 12px; */
color: #797979;
transform: scale(1.05, 1.05);
}
<label>Radio button colors:</label>
<br>
<div class="radio-tile-group">
<div class="input-container">
<label class="radio-tile-label" style="background-color: #b60205;border-radius: 5px;">
<input type="radio" value="#b60205" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color: #b60205;">
</label>
</div>
</div>
<div class="input-container">
<label class="radio-tile-label" style="background-color: #d93f0b; border-radius: 5px;">
<input type="radio" value="#d93f0b" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color: #d93f0b;">
</label>
</div>
</div>
<div class="input-container">
<label class="radio-tile-label" style="background-color: #fbca04; border-radius: 5px;">
<input type="radio" value="#fbca04" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color: #fbca04;">
</label>
</div>
</div>
<div class="input-container">
<label class="radio-tile-label" style="background-color: #0e8a16; border-radius: 5px;">
<input type="radio" value="#0e8a16" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color: #0e8a16;">
</label>
</div>
</div>
<div class="input-container">
<label class="radio-tile-label" style="background-color: #006b75; border-radius: 5px;">
<input type="radio" value="#006b75" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color:#006b75">
</label>
</div>
</div>
<div class="input-container">
<label class="radio-tile-label" style="background-color: #1d76db; border-radius: 5px;">
<input type="radio" value="#1d76db" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color: #1d76db;">
</label>
</div>
</div>
<div class="input-container">
<label class="radio-tile-label" style="background-color: #0052cc; border-radius: 5px;">
<input type="radio" value="#0052cc" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color: #0052cc;">
</label>
</div>
</div>
<div class="input-container">
<label class="radio-tile-label" style="background-color: #757575; border-radius: 5px;">
<input type="radio" value="#757575" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color: #757575;">
</label>
</div>
</div>
</div>
A simple fix would be to use the following CSS property.
input[type=radio]:checked{
background: \*colour*\;
border-radius: 15px;
border: 4px solid #dfdfdf;
}

CSS Radio-button and text outline

I'm having a problem with setting up box & shadow around my radio button. My CSS sets box only around radio button and shows nasty white square box around it. How to set border or outline around whole Radio-button + text to make selection more distinctive.
enrgy-form {
width: 50%;
float: right;
}
.label-width {
margin-left: 22px;
white-space: nowrap;
}
.label-nowrapp {
white-space: nowrap;
}
.selected-item input:checked {
/*border: 1px solid dodgerblue;*/
box-shadow: 3px 3px 11px 1px dodgerblue;
}
<div class="form-check enrgy-form">
<label class="form-check-label label-nowrapp selected-item">
<input class="form-check-input selected-item" type="radio" name="energy" formControlName="energy" value="Energy" (change)="setOptions()">Fuel-fired</label>
</div>
I think your best bet is to simulate the radio button with css so you can have the behavior you want.
You should first set the input to display: none and give it an id in your HTML so you can link it with the label, by giving the label a for attribute, this way you can control the check/uncheck of your radio button from the label.
Next you want to simulate the appearance of the radio button, i'll do this by adding two spans, one inside the other, so we can have a checked/unchecked status.
try this:
enrgy-form {
width: 50%;
float: right;
}
.label-width {
margin-left: 22px;
white-space: nowrap;
}
.label-nowrapp {
white-space: nowrap;
}
.selected-item {
display: none;
}
.selected-item:checked + label {
box-shadow: 0px 0px 11px 2px dodgerblue;
}
label{
padding: 3px;
}
label .bullet{
border-radius: 50%;
border: 1px solid gray;
background-color: lightgray;
margin-right: 3px;
display: inline-block;
width: 10px;
height: 10px;
position: relative;
}
.selected-item:checked + label .bullet .bullet-selected{
position: absolute;
top: 50%;
left: 50%;
border-radius: 50%;
transform: translate(-50%, -50%);
display: inline-block;
width: 5px;
height: 5px;
background-color: gray;
}
<div class="form-check enrgy-form">
<input class="form-check-input selected-item" type="radio" name="energy" formControlName="energy" value="Energy" (change)="setOptions()" id="someUniqueId"/>
<label class="form-check-label label-nowrapp" for="someUniqueId">
<span class="bullet">
<span class="bullet-selected"></span>
</span>
Fuel-fired
</label>
</div>
You could go the route where you style the whole radio button using :before and :after in CSS. That way you could even go nuts with animations and stuff...
It would require you to change the HTML a bit as well....
There's plenty of examples to be found if you search for "css custom radio".
[type="radio"]{
position: absolute;
left: -9999px;
}
[type="radio"] + label
{
position: relative;
padding: 0 20px;
cursor: pointer;
}
[type="radio"] + label:before{
content: '';
position: absolute;
left: 0;
top: 0;
width: 14px;
height: 14px;
border: 1px solid #ddd;
border-radius: 100%;
background: #fff;
}
[type="radio"]:checked + label:before{
box-shadow: 0px 1px 11px 1px dodgerblue;
}
[type="radio"] + label:after{
content: '';
display: none;
width: 10px;
height: 10px;
background: gray;
position: absolute;
top: 3px;
left: 3px;
border-radius: 100%;
}
[type="radio"]:checked + label:after {
display: block;
}
<div class="form-check enrgy-form">
<input type="radio" name="energy" id="one">
<label for="one">Fuel-fired</label>
</input>
<input type="radio" name="energy" id="two">
<label for="two">Something else</label>
</input>
</div>
Update
Here is a possible solution, you could modify it as you want!
.form-check {
position: relative;
display: inline-flex;
align-items: center;
font-size: 1rem;
}
.form-check-label {
font-size: 0.9em;
margin-right: 0.25em;
white-space: nowrap;
}
.form-check-input {
margin: 0;
margin-right: 0.5em;
}
.form-check-input:checked + .form-check-label:after {
content: '';
width: 100%;
height: 100%;
top: 0;
left: 0;
position: absolute;
border-radius: 1.5em 8px 8px 1.5em;
box-shadow: 3px 3px 11px 1px dodgerblue;
}
.medium { font-size: 2rem; }
.medium input[type=radio] { zoom: 2 }
.big { font-size: 3rem; }
.big input[type=radio] { zoom: 3 }
<div class="form-check">
<input id="inputcheck" class="form-check-input" type="radio" name="energy" formControlName="energy" value="Energy">
<label for="inputcheck" class="form-check-label">Fuel-fired normal</label>
</div>
<br><br>
<div class="form-check medium">
<input id="inputcheck1" class="form-check-input" type="radio" name="energy" formControlName="energy" value="Energy">
<label for="inputcheck1" class="form-check-label">Fuel-fired medium</label>
</div>
<br><br>
<div class="form-check big">
<input id="inputcheck2" class="form-check-input" type="radio" name="energy" formControlName="energy" value="Energy">
<label for="inputcheck2" class="form-check-label">Fuel-fired big</label>
</div>

CSS ''background-color" attribute not working on checkbox inside <div>

The heading pretty much explains it. I have a couple of checkboxes inside a scrollable div. But for some reasons the 'background-color' attribute doesn't work. Although the 'margin-top' does seem to work...
Just puzzling me how one attribute can work and another not. It's also not like the div has it's own set of background color attributes that could potentially over ride the checkboxes attributes.
Anyways, below is my HTML (which is generated by JSP):
<div class="listContainer">
<input type="checkbox" class="oddRow">item1<br/>
<input type="checkbox" class="evenRow">item2<br/>
<input type="checkbox" class="oddRow">item3<br/>
<input type="checkbox" class="evenRow">item4<br/>
...
</div>
And here is my CSS:
.listContainer {
border:2px solid #ccc;
width:340px;
height: 225px;
overflow-y: scroll;
margin-top: 20px;
padding-left: 10px;
}
.oddRow {
margin-top: 5px;
background-color: #ffffff;
}
.evenRow{
margin-top: 5px;
background-color: #9FFF9D;
}
A checkbox does not have background color.
But to add the effect, you may wrap each checkbox with a div that has color:
<div class="evenRow">
<input type="checkbox" />
</div>
<div class="oddRow">
<input type="checkbox" />
</div>
<div class="evenRow">
<input type="checkbox" />
</div>
<div class="oddRow">
<input type="checkbox" />
</div>
In addition to the currently accepted answer: You can set border and background of a checkbox/radiobutton, but how it is rendered in the end depends on the browser. For example, if you set a red background on a checkbox
IE will show a red border instead
Opera will show a red background as intended
Firefox, Safari and Chrome will do nothing
This German language article compares a few browsers and explains at least the IE behavior. It maybe bit older (still including Netscape), but when you test around you'll notice that not much has changed. Another comparison can be found here.
You can use peseudo elements like this:
input[type=checkbox] {
width: 30px;
height: 30px;
margin-right: 8px;
cursor: pointer;
font-size: 27px;
}
input[type=checkbox]:after {
content: " ";
background-color: #9FFF9D;
display: inline-block;
visibility: visible;
}
input[type=checkbox]:checked:after {
content: "\2714";
}
<label>Checkbox label
<input type="checkbox">
</label>
After so much trouble i got it.
.purple_checkbox:after {
content: " ";
background-color: #5C2799;
display: inline-block;
visibility: visible;
}
.purple_checkbox:checked:after {
content: "\2714";
box-shadow: 0px 2px 4px rgba(155, 155, 155, 0.15);
border-radius: 3px;
height: 12px;
display: block;
width: 12px;
text-align: center;
font-size: 9px;
color: white;
}
<input type="checkbox" class="purple_checkbox">
It will be like this when checked with this code.
My solution
Initially posted here.
input[type="checkbox"] {
cursor: pointer;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
outline: 0;
background: lightgray;
height: 16px;
width: 16px;
border: 1px solid white;
}
input[type="checkbox"]:checked {
background: #2aa1c0;
}
input[type="checkbox"]:hover {
filter: brightness(90%);
}
input[type="checkbox"]:disabled {
background: #e6e6e6;
opacity: 0.6;
pointer-events: none;
}
input[type="checkbox"]:after {
content: '';
position: relative;
left: 40%;
top: 20%;
width: 15%;
height: 40%;
border: solid #fff;
border-width: 0 2px 2px 0;
transform: rotate(45deg);
display: none;
}
input[type="checkbox"]:checked:after {
display: block;
}
input[type="checkbox"]:disabled:after {
border-color: #7b7b7b;
}
<input type="checkbox"><br>
<input type="checkbox" checked><br>
<input type="checkbox" disabled><br>
<input type="checkbox" disabled checked><br>
2022 - there is a much better solution to this problem now
Just use the accent-color property and make sure you achieve proper contrast ratios for accessibility:
.blue-checkbox {
accent-color: #00eaff;
height: 30px; /* not needed */
width: 30px; /* not needed */
}
<input class="blue-checkbox" type="checkbox" />
We can provide background color from the css file. Try this one,
<!DOCTYPE html>
<html>
<head>
<style>
input[type="checkbox"] {
width: 25px;
height: 25px;
background: gray;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
border: none;
outline: none;
position: relative;
left: -5px;
top: -5px;
cursor: pointer;
}
input[type="checkbox"]:checked {
background: blue;
}
.checkbox-container {
position: absolute;
display: inline-block;
margin: 20px;
width: 25px;
height: 25px;
overflow: hidden;
}
</style>
</head>
<body>
<div class="checkbox-container">
<input type="checkbox" />
</div>
</body>
</html>
The Best solution to change background checkbox color
input[type=checkbox] {
margin-right: 5px;
cursor: pointer;
font-size: 14px;
width: 15px;
height: 12px;
position: relative;
}
input[type=checkbox]:after {
position: absolute;
width: 10px;
height: 15px;
top: 0;
content: " ";
background-color: #ff0000;
color: #fff;
display: inline-block;
visibility: visible;
padding: 0px 3px;
border-radius: 3px;
}
input[type=checkbox]:checked:after {
content: "✓";
font-size: 12px;
}
<input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle" value="Car" checked> I have a car<br>
<input type="checkbox" name="vehicle" value="Car" checked> I have a bus<br>
Improving another answer here
input[type=checkbox] {
cursor: pointer;
margin-right: 10px;
}
input[type=checkbox]:after {
content: " ";
background-color: lightgray;
display: inline-block;
position: relative;
top: -4px;
width: 24px;
height: 24px;
margin-right: 10px;
}
input[type=checkbox]:checked:after {
content: "\00a0\2714";
}
When you input the body tag, press space just one time without closing the tag and input bgcolor="red", just for instance. Then choose a diff color for your font.

How do I change the color of radio buttons?

I mean, a radio button itself consists of a round shape and a dot at the center (when the button is selected). What I want to change is the color of both. Can this be done using CSS?
A quick fix would be to overlay the radio button input style using :after, however it's probably a better practice to create your own custom toolkit.
input[type='radio']:after {
width: 15px;
height: 15px;
border-radius: 15px;
top: -2px;
left: -1px;
position: relative;
background-color: #d1d3d1;
content: '';
display: inline-block;
visibility: visible;
border: 2px solid white;
}
input[type='radio']:checked:after {
width: 15px;
height: 15px;
border-radius: 15px;
top: -2px;
left: -1px;
position: relative;
background-color: #ffa500;
content: '';
display: inline-block;
visibility: visible;
border: 2px solid white;
}
<input type='radio' name="gender"/>
<input type='radio' name="gender"/>
A radio button is a native element specific to each OS/browser. There is no way to change its color/style, unless you want to implement custom images or use a custom Javascript library which includes images (e.g. this - cached link)
As Fred mentioned, there is no way to natively style radio buttons in regards to color, size, etcc. But you can use CSS Pseudo elements to setup an impostor of any given radio button, and style it. Touching on what JamieD said, on how we can use the :after Pseudo element, you can use both :before and :after to achieve a desirable look.
Benefits of this approach:
Style your radio button and also Include a label for content.
Change the outer rim color and/or checked circle to any color you like.
Give it a transparent look with modifications to background color property and/or optional use of the opacity property.
Scale the size of your radio button.
Add various drop shadow properties such as CSS drop shadow inset where needed.
Blend this simple CSS/HTML trick into various Grid systems, such as Bootstrap 3.3.6, so it matches the rest of your Bootstrap components visually.
Explanation of short demo below:
Set up a relative in-line block for each radio button
Hide the native radio button sense there is no way to style it directly.
Style and align the label
Rebuilding CSS content on the :before Pseudo-element to do 2 things - style the outer rim of the radio button and set element to appear first (left of label content). You can learn basic steps on Pseudo-elements here - http://www.w3schools.com/css/css_pseudo_elements.asp
If the radio button is checked, request for label to display CSS content (the styled dot in the radio button) afterwards.
The HTML
<div class="radio-item">
<input type="radio" id="ritema" name="ritem" value="ropt1">
<label for="ritema">Option 1</label>
</div>
<div class="radio-item">
<input type="radio" id="ritemb" name="ritem" value="ropt2">
<label for="ritemb">Option 2</label>
</div>
The CSS
.radio-item {
display: inline-block;
position: relative;
padding: 0 6px;
margin: 10px 0 0;
}
.radio-item input[type='radio'] {
display: none;
}
.radio-item label {
color: #666;
font-weight: normal;
}
.radio-item label:before {
content: " ";
display: inline-block;
position: relative;
top: 5px;
margin: 0 5px 0 0;
width: 20px;
height: 20px;
border-radius: 11px;
border: 2px solid #004c97;
background-color: transparent;
}
.radio-item input[type=radio]:checked + label:after {
border-radius: 11px;
width: 12px;
height: 12px;
position: absolute;
top: 9px;
left: 10px;
content: " ";
display: block;
background: #004c97;
}
A short demo to see it in action
In conclusion, no JavaScript, images or batteries required. Pure CSS.
You can use the CSS accent-color property to change the color.
input[type='radio'] {
accent-color: #232323;
}
It works with Chrome/Edge 93+, Firefox 92+, and Safari 15.4+ (Browser support info from caniuse.)
You can achieve customized radio buttons in two pure CSS ways
Via removing standard appearance using CSS appearance and applying custom appearance. Unfortunately this was doesn't work in IE. Demo:
input[type="radio"] {
/* remove standard background appearance */
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
/* create custom radiobutton appearance */
display: inline-block;
width: 25px;
height: 25px;
padding: 6px;
/* background-color only for content */
background-clip: content-box;
border: 2px solid #bbbbbb;
background-color: #e7e6e7;
border-radius: 50%;
}
/* appearance for checked radiobutton */
input[type="radio"]:checked {
background-color: #93e026;
}
/* optional styles, I'm using this for centering radiobuttons */
.flex {
display: flex;
align-items: center;
}
<div class="flex">
<input type="radio" name="radio" id="radio1" />
<label for="radio1">RadioButton1</label>
</div>
<div class="flex">
<input type="radio" name="radio" id="radio2" />
<label for="radio2">RadioButton2</label>
</div>
<div class="flex">
<input type="radio" name="radio" id="radio3" />
<label for="radio3">RadioButton3</label>
</div>
Via hiding radiobutton and setting custom radiobutton appearance to label's pseudoselector. By the way no need for absolute positioning here (I see absolute positioning in most demos). Demo:
*,
*:before,
*:after {
box-sizing: border-box;
}
input[type="radio"] {
display: none;
}
input[type="radio"]+label:before {
content: "";
/* create custom radiobutton appearance */
display: inline-block;
width: 25px;
height: 25px;
padding: 6px;
margin-right: 3px;
/* background-color only for content */
background-clip: content-box;
border: 2px solid #bbbbbb;
background-color: #e7e6e7;
border-radius: 50%;
}
/* appearance for checked radiobutton */
input[type="radio"]:checked + label:before {
background-color: #93e026;
}
/* optional styles, I'm using this for centering radiobuttons */
label {
display: flex;
align-items: center;
}
<input type="radio" name="radio" id="radio1" />
<label for="radio1">RadioButton1</label>
<input type="radio" name="radio" id="radio2" />
<label for="radio2">RadioButton2</label>
<input type="radio" name="radio" id="radio3" />
<label for="radio3">RadioButton3</label>
Only if you are targeting webkit-based browsers (Chrome and Safari, maybe you are developing a Chrome WebApp, who knows...), you can use the following:
input[type='radio'] {
-webkit-appearance: none;
}
And then style it as if it were a simple HTML element, for example applying a background image.
Use input[type='radio']:active for when the input is selected, to provide the alternate graphics
Update: As of 2018 you can add the following to support multiple browser vendors:
input[type="radio"] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
Try something like this:
#yes{
border:2px solid white;
box-shadow:0 0 0 1px #392;
appearance:none;
border-radius:50%;
width:12px;
height:12px;
background-color:#fff;
transition:all ease-in 0.2s;
}
#yes:checked{
background-color:#392;
}
#no{
border:2px solid white;
box-shadow:0 0 0 1px #932;
appearance:none;
border-radius:50%;
width:12px;
height:12px;
background-color:#fff;
transition:all ease-in 0.2s;
}
#no:checked{
background-color:#932;
}
<input id="yes" type="radio" name="s"><label for="yes">Yes</label></br>
<input id="no" type="radio" name="s"><label for="no">No</label>
There is less of code, it looks better and you don't need to play with :before , :after and position to reach the effect.
you can use the checkbox hack as explained in css tricks
http://css-tricks.com/the-checkbox-hack/
working example of radio button:
http://codepen.io/Angelata/pen/Eypnq
input[type=radio]:checked ~ .check {}
input[type=radio]:checked ~ .check .inside{}
Works in IE9+, Firefox 3.5+, Safari 1.3+, Opera 6+, Chrome anything.
simple cross browser custom radio button example for you
.checkbox input{
display: none;
}
.checkbox input:checked + label{
color: #16B67F;
}
.checkbox input:checked + label i{
background-image: url('http://kuzroman.com/images/jswiddler/radio-button.svg');
}
.checkbox label i{
width: 15px;
height: 15px;
display: inline-block;
background: #fff url('http://kuzroman.com/images/jswiddler/circle.svg') no-repeat 50%;
background-size: 12px;
position: relative;
top: 1px;
left: -2px;
}
<div class="checkbox">
<input type="radio" name="sort" value="popularity" id="sort1">
<label for="sort1">
<i></i>
<span>first</span>
</label>
<input type="radio" name="sort" value="price" id="sort2">
<label for="sort2">
<i></i>
<span>second</span>
</label>
</div>
https://jsfiddle.net/kuzroman/ae1b34ay/
Well to create extra elements we can use :after, :before (so we don’t have to change the HTML that much). Then for radio buttons and checkboxes we can use :checked. There are a few other pseudo elements we can use as well (such as :hover). Using a mixture of these we can create some pretty cool custom forms. check this
I builded another fork of #klewis' code sample to demonstrate some playing with pure css and gradients by using :before/:after pseudo elements and a hidden radio input button.
HTML:
sample radio buttons:
<div style="background:lightgrey;">
<span class="radio-item">
<input type="radio" id="ritema" name="ritem" class="true" value="ropt1" checked="checked">
<label for="ritema">True</label>
</span>
<span class="radio-item">
<input type="radio" id="ritemb" name="ritem" class="false" value="ropt2">
<label for="ritemb">False</label>
</span>
</div>
:
CSS:
.radio-item input[type='radio'] {
visibility: hidden;
width: 20px;
height: 20px;
margin: 0 5px 0 5px;
padding: 0;
}
.radio-item input[type=radio]:before {
position: relative;
margin: 4px -25px -4px 0;
display: inline-block;
visibility: visible;
width: 20px;
height: 20px;
border-radius: 10px;
border: 2px inset rgba(150,150,150,0.75);
background: radial-gradient(ellipse at top left, rgb(255,255,255) 0%, rgb(250,250,250) 5%, rgb(230,230,230) 95%, rgb(225,225,225) 100%);
content: "";
}
.radio-item input[type=radio]:checked:after {
position: relative;
top: 0;
left: 9px;
display: inline-block;
visibility: visible;
border-radius: 6px;
width: 12px;
height: 12px;
background: radial-gradient(ellipse at top left, rgb(245,255,200) 0%, rgb(225,250,100) 5%, rgb(75,175,0) 95%, rgb(25,100,0) 100%);
content: "";
}
.radio-item input[type=radio].true:checked:after {
background: radial-gradient(ellipse at top left, rgb(245,255,200) 0%, rgb(225,250,100) 5%, rgb(75,175,0) 95%, rgb(25,100,0) 100%);
}
.radio-item input[type=radio].false:checked:after {
background: radial-gradient(ellipse at top left, rgb(255,225,200) 0%, rgb(250,200,150) 5%, rgb(200,25,0) 95%, rgb(100,25,0) 100%);
}
.radio-item label {
display: inline-block;
height: 25px;
line-height: 25px;
margin: 0;
padding: 0;
}
preview:
https://www.codeply.com/p/y47T4ylfib
For those who prefer to start development with a minimal example, here's a simple custom radio button that doesn't depend on label:
[type="radio"] {
visibility: hidden; /* hide default radio button */
/* you may need to adjust margin here, too */
}
[type="radio"]::before { /* create pseudoelement */
border: 2px solid gray; /* thickness, style, color */
height: .9em; /* height adjusts with font */
width: .9em; /* width adjusts with font */
border-radius: 50%; /* make it round */
display: block; /* or flex or inline-block */
content: " "; /* won't display without this */
cursor: pointer; /* appears clickable to mouse users */
visibility: visible; /* reverse the 'hidden' above */
}
[type="radio"]:checked::before { /* selected */
/* add middle dot when selected */
/* slightly bigger second value makes it smooth */
/* even more (e.g., 20% 50%) would make it fuzzy */
background: radial-gradient(gray 36%, transparent 38%);
}
<br>
<input type="radio" name="example" id="one" value="one">
<label for="one">one</label>
<br>
<br>
<input type="radio" name="example" id="two" value="two">
<label for="two">two</label>
Try this css with transition:
Demo
$DarkBrown: #292321;
$Orange: #CC3300;
div {
margin:0 0 0.75em 0;
}
input[type="radio"] {
display:none;
}
input[type="radio"] + label {
color: $DarkBrown;
font-family:Arial, sans-serif;
font-size:14px;
}
input[type="radio"] + label span {
display:inline-block;
width:19px;
height:19px;
margin:-1px 4px 0 0;
vertical-align:middle;
cursor:pointer;
-moz-border-radius: 50%;
border-radius: 50%;
}
input[type="radio"] + label span {
background-color:$DarkBrown;
}
input[type="radio"]:checked + label span{
background-color:$Orange;
}
input[type="radio"] + label span,
input[type="radio"]:checked + label span {
-webkit-transition:background-color 0.4s linear;
-o-transition:background-color 0.4s linear;
-moz-transition:background-color 0.4s linear;
transition:background-color 0.4s linear;
}
Html :
<div>
<input type="radio" id="radio01" name="radio" />
<label for="radio01"><span></span>Radio Button 1</label>
</div>
<div>
<input type="radio" id="radio02" name="radio" />
<label for="radio02"><span></span>Radio Button 2</label>
</div>
Simple , you can be used accent-color
View page source
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
input[type=radio] {
accent-color: red;
}
</style>
</head>
<body>
<label for="css">Are you like to css</label>
<input type="radio" id="css" value="css">
</body>
</html>
You should use the accent-color CSS property, which sets the accent color for user-interface controls such as inputs (radio buttons, checkboxes...) or progress bars and it's supported for most modern browsers.
input {
accent-color: red;
}
document.querySelector("input[name=accent-color]").addEventListener("input", () => {
document.documentElement.style.setProperty("--accent-color", event.target.value);
});
:root {
--accent-color: red;
}
input,
progress {
accent-color: var(--accent-color);
}
/* Other styles */
label {
display: flex;
align-items: center;
gap: .625rem;
margin-bottom: .625rem;
}
label:first-child {
font-size: 1.15rem;
font-weight: bold;
}
input {
flex: 0 0 auto;
height: 1.25rem;
width: 1.25rem;
}
input[type="color"] {
width: 3rem;
}
input[type="range"] {
width: 12.5rem;
}
<label>Change the accent color<input name="accent-color" type="color" value="#ff0000"></input></label><br>
<label><input name="radio" type="radio" checked></input>Radio button</label>
<label><input name="radio" type="radio"></input>Another radio button</label>
<label><input name="check" type="checkbox" checked></input>Checkbox</label>
<label><input name="range" type="range"></input>Range input</label>
<label><progress value="50" max="100"></progress>Progress bar</label>
This is not possible by native CSS. You'll have to use background images and some javascript tricks.
As other said, there's no way to achieve this in all browser, so best way of doing so crossbrowser is using javascript unobtrusively. Basically you have to turn your radiobutton into links (fully customizable via CSS). each click on link will be bound to the related radiobox, toggling his state and all the others.
For my use all I wanted to do was change the colour and nothing else, so I've taken the answer from #klewis and changed it to...
Make the radio the same as the browser default (Chrome in my case) using relative % and em instead of fixed px. Caveat: em is based on whatever the font-size of input[type=radio] is, which could be inherited. Adjustments to the values below may be necessary.
Keep accessibility functions (like an outline when focused) of the original radio button by not using display: none; and by applying :before and :after to the original radio instead of the label.
/* make default radio 'invisible' */
input[type=radio] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
/* make new radio outer circle */
input[type=radio]:before {
content: " ";
display: inline-block;
position: relative;
width: 0.8em;
height: 0.8em;
border-radius: 50%;
border: 1px solid grey;
background-color: transparent;
}
/* change colour of radio outer circle when checked */
input[type=radio]:checked:before {
border-color: green;
}
/* make new radio inner circle when checked */
input[type=radio]:checked:after {
content: " ";
display: block;
position: absolute;
width: 0.55em;
height: 0.55em;
border-radius: 50%;
top: 0.4em;
left: 0.13em;
background: green;
}
`
This Worked for me well,
Simply add css attribute:
input[type="radio"]{accent-color: red;}
Here is the link for resource
The simple way is to use accent-color
The accent-color CSS property sets the accent color for user-interface controls generated by some elements
Browsers that support accent-color currently apply it to the following HTML elements:
<input type="checkbox">
<input type="radio">
<input type="range">
<progress>
An runnable example
body {
display: grid;
padding: 3rem 0;
}
.accent {
accent-color: #30cc7e;
}
form {
display: grid;
grid-auto-columns: fit-content(50%);
grid-template-areas: "a a";
margin: auto;
padding: 0;
gap: 1rem;
}
form {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 2rem;
margin: auto;
}
form section:first-child {
color-scheme: light;
}
form section:last-child {
color-scheme: dark;
}
fieldset {
border-radius: 8px;
color-scheme: light;
display: flex;
flex: 1;
flex-direction: column;
gap: 1rem;
padding: 1rem;
}
.dark {
color-scheme: dark;
}
.dark fieldset {
background: #100f33;
border-color: #100f33;
color: #fff;
}
.dark .accent {
accent-color: hsla(180, 100%, 70%, 1);
}
h2 {
margin: 0;
}
.notice {
background: #fff9c4;
border-radius: 6px;
margin: 1.5rem auto;
padding: 0.5rem;
text-align: center;
}
#supports (accent-color: #fff) {
.notice {
display: none;
}
}
<div class="notice">
Your browser does not support the <code>accent-color</code> property.
</div>
<form action="">
<fieldset>
<h2>Checkboxes</h2>
<div>
<label for="checkbox">
Default
</label>
<input id="checkbox" type="checkbox" checked>
</div>
<div>
<label for="checkbox-accent">
Accent
</label>
<input id="checkbox-accent" type="checkbox" class="accent" checked>
</div>
</fieldset>
<fieldset>
<h2>Radio</h2>
<div>
<input id="radio" type="radio" checked>
<label for="radio">
Default
</label>
</div>
<div>
<input id="radio-accent" type="radio" class="accent" checked>
<label for="radio-accent">
Accent
</label>
</div>
</fieldset>
<fieldset>
<h2>Progress</h2>
<div>
<label for="progress">
Default
</label>
<progress id="progress" min="0" max="100" value="50"></progress>
</div>
<div>
<label for="progress-accent">
Accent
</label>
<progress id="progress-accent" class="accent" min="0" max="100" value="50"></progress>
</div>
</fieldset>
<fieldset>
<h2>Range</h2>
<div>
<label for="range">
Default
</label>
<input id="range" type="range">
</div>
<div>
<label for="range-accent">
Accent
</label>
<input id="range-accent" class="accent" type="range">
</div>
</fieldset>
</form>
You can use accent-color property in css to change background color of both checkbox and radio buttons.
input[type=radio] {
accent-color: red;
}
It may be helpful to bind radio-button to styled label. Futher details in this answer.
A clever way to do it would be to create a separate div with a height and width of -for example- 50px and then a radius of 50px lay this over your radio buttons...
You can embed a span element in the radio input then select a color of your choice to be rendered when a radio input is checked. Check out the example below sourced from w3schools.
<!DOCTYPE html>
<html>
<style>
/* The container */
.container {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default radio button */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create a custom radio button */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
border-radius: 50%;
}
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #ccc;
}
/* When the radio button is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: #00a80e;
}
/* Create the indicator (the dot/circle - hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the indicator (dot/circle) when checked */
.container input:checked ~ .checkmark:after {
display: block;
}
/* Style the indicator (dot/circle) */
.container .checkmark:after {
top: 9px;
left: 9px;
width: 8px;
height: 8px;
border-radius: 50%;
background: white;
}
</style>
<body>
<h1>Custom Radio Buttons</h1>
<label class="container">One
<input type="radio" checked="checked" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Two
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Three
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Four
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
</body>
Changing the background color at this code segment below does the trick.
/* When the radio button is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: #00a80e;
}
Sourced from how to create a custom radio button
If you are using react bootstrap Form.check you could do something like this
HTML
<Form.Check
type="radio"
id="Radio-card"
label={`check me out`}
name="paymentmethod"
value="card"
/>
SCSS
.form-check {
display: flex;
align-items: center;
input[type="radio"] {
-moz-appearance: none;
appearance: none;
width: 11px;
height: 11px;
padding: 1px;
background-clip: content-box;
border: 1px solid hotpink;
background-color: white;
border-radius: 50%;
}
input[type="radio"]:checked {
outline: none;
background-color: hotpink;
border: 1px solid hotpink;
}
label {
font-size: 14px;
font-weight: 600;
}
}
I changed the color and size of radio buttons. Try This
.radio-tile-group {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
}
.radio-tile-group .input-container {
position: relative;
margin: 0.9rem;
}
.radio-tile-group .input-container .radio-button {
opacity: 0;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
margin: 0;
cursor: pointer;
}
.radio-tile {
border: 1px solid #eea236;
}
.radio-tile-group .input-container .radio-tile-edit {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 25px;
font-size: 12px;
border-radius: 5px;
padding: 0.2rem;
transition: transform 300ms ease;
height: 25px;
}
#media (min-width: 375px) and (max-width: 812px) {
.radio-tile-group .input-container .radio-tile {
margin-inline: 18px;
}
}
.radio-tile-group .input-container .radio-button:checked+.radio-tile {
border: 3px solid #2980b9;
font-size: 12px;
color: #797979;
transform: scale(1.05, 1.05);
}
.radio-tile-group .input-container .radio-button:checked+.radio-tile .icon svg {
fill: white;
background-color: #2980b9;
}
.radio-tile-group .input-container .radio-button:checked+.radio-tile-edit {
border: 3px solid black;
/* font-size: 12px; */
color: #797979;
transform: scale(1.05, 1.05);
}
<label>Radio button colors:</label>
<br>
<div class="radio-tile-group">
<div class="input-container">
<label class="radio-tile-label" style="background-color: #b60205;border-radius: 5px;">
<input type="radio" value="#b60205" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color: #b60205;">
</label>
</div>
</div>
<div class="input-container">
<label class="radio-tile-label" style="background-color: #d93f0b; border-radius: 5px;">
<input type="radio" value="#d93f0b" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color: #d93f0b;">
</label>
</div>
</div>
<div class="input-container">
<label class="radio-tile-label" style="background-color: #fbca04; border-radius: 5px;">
<input type="radio" value="#fbca04" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color: #fbca04;">
</label>
</div>
</div>
<div class="input-container">
<label class="radio-tile-label" style="background-color: #0e8a16; border-radius: 5px;">
<input type="radio" value="#0e8a16" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color: #0e8a16;">
</label>
</div>
</div>
<div class="input-container">
<label class="radio-tile-label" style="background-color: #006b75; border-radius: 5px;">
<input type="radio" value="#006b75" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color:#006b75">
</label>
</div>
</div>
<div class="input-container">
<label class="radio-tile-label" style="background-color: #1d76db; border-radius: 5px;">
<input type="radio" value="#1d76db" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color: #1d76db;">
</label>
</div>
</div>
<div class="input-container">
<label class="radio-tile-label" style="background-color: #0052cc; border-radius: 5px;">
<input type="radio" value="#0052cc" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color: #0052cc;">
</label>
</div>
</div>
<div class="input-container">
<label class="radio-tile-label" style="background-color: #757575; border-radius: 5px;">
<input type="radio" value="#757575" class= "radio-button uncheckall" name="print_color">
<div class="radio-tile-edit" style="background-color: #757575;">
</label>
</div>
</div>
</div>
A simple fix would be to use the following CSS property.
input[type=radio]:checked{
background: \*colour*\;
border-radius: 15px;
border: 4px solid #dfdfdf;
}