How do I change the color of radio buttons?
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)
How to have different colors of radio buttons?
Right now in your CSS, you're only checking whether or not any radiobutton is checked, and painting it blue. You need to create an option so that you can differ between radiobuttons (to give them different colours for instance).
Classes are great for this, they're pretty much meant for this functionality.
Adapt your HTML so that it looks like this:
<div>
<input type="radio" checked="checked" class="blue" id="radio01" name="radio" />
<label for="radio01"><span></span>Radio Button 1</label>
</div>
<div>
<input type="radio" id="radio02" class="green" name="radio" />
<label for="radio02"><span></span>Radio Button 2</label>
</div>
Notice how the radiobuttons now differ in their class attributes (so now you can access them seperately.
Adapt your CSS like this:
input[type="radio"].green:checked + label span{
background-color:green;
border:1px solid blue;
}
input[type="radio"].blue:checked + label span{
background-color:blue;
border:1px solid blue;
}
Now your CSS will paint radiobuttons that have their class set to blue, blue, and the ones classed as green, green.
How can I style radio buttons with different colors?
That would be a nice experiment to play around with css. My concern is about the usability and the user expectations.
By changing the way a radio button is perceived, especially the status of the current state I believe we are undermining the effectiveness of the form itself.
As a user I would be concerned to see that the "no" option has a red highlight.
You know when the thing you type in a form is not valid?
Having the "no" option in red would seem like it is not a valid option to pick.
Making the "yes" option in green will make it seem like is the only valid option a user can choose. That is misleading to say the least.
I would strongly advice to NOT make the two radio buttons in any particular colour and maintain the native browser colours.
In any case, here is the code to make it work.
Note that I have changed the markup and added the two distinct css classes.
The markup now has the words red
and green
attached to the <label>
rather than the <input>
elements.
From the css I've moved the background declaration from the generic one to the two specific colours.
Let me know if you have any additional issues with the specificity. It can be fixed.
.custom-control-input:checked~.custom-control-label::before { color: #fff; border-color: #7B1FA2;}
.custom-control-input:checked~.custom-control-label.red::before { background-color: red;}
.custom-control-input:checked~.custom-control-label.green::before { background-color: green;}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" /><div class="custom-control custom-radio custom-control-inline"> <input type="radio" id="rd_1" name="rd" class="custom-control-input" value="Yes"> <label class="custom-control-label green" for="rd_1">Yes</label></div><div class="custom-control custom-radio custom-control-inline"> <input type="radio" id="rd_2" name="rd" class="custom-control-input" value="No"> <label class="custom-control-label red" for="rd_2">No</label></div>
Is it possible to change the color of selected radio button's center circle
You can mimic the radio button using some round border trick (to render the outer circle) and the pseudo-element :before
(to render the inner circle) which can fortunately be used on an input field of radio
type:
input[type='radio'] {
-webkit-appearance:none;
width:20px;
height:20px;
border:1px solid darkgray;
border-radius:50%;
outline:none;
box-shadow:0 0 5px 0px gray inset;
}
input[type='radio']:hover {
box-shadow:0 0 5px 0px orange inset;
}
input[type='radio']:before {
content:'';
display:block;
width:60%;
height:60%;
margin: 20% auto;
border-radius:50%;
}
input[type='radio']:checked:before {
background:green;
}
Working Demo.
How change the color of the radio button
There are two ways to style radio buttons (and checkboxes by the way) only with CSS:
via styling label's pseudoselector and hiding radio buttons
/* completely hiding radio button */
input[type="radio"] {
display: none;
}
/* simulate radiobutton appearance using pseudoselector */
input[type="radio"] + label:before {
content: "";
/* create custom radiobutton appearance */
display: inline-block;
width: 15px;
height: 15px;
padding: 3px;
margin-right: 5px;
/* background-color only for content */
background-clip: content-box;
border: 1px solid #bbbbbb;
background-color: #e7e6e7;
border-radius: 50%;
}
/* appearance of checked radiobutton */
input[type="radio"]:checked + label:before {
background-color: red;
}
/* resetting default box-sizing */
*,
*:before,
*:after {
box-sizing: border-box;
}
/* optional styles for centering radiobuttons */
.sound-signal label {
display: inline-flex;
align-items: center;
}<div class="sound-signal">
Signal sonore:
<input type="radio" name="soundsignal" id="soundsignal1" checked="checked">
<label for="soundsignal1">Oui</label>
<input type="radio" name="soundsignal" id="soundsignal2">
<label for="soundsignal2">Non</label>
</div>how to use different background color for radio button
Try this solution:
https://jsfiddle.net/vadimb/n5pjgev6/420/
The key point is to use reference variable
#name
to get the current value for updating form:<label [ng-class]="{good: sel1.checked}">
<input #sel1 type="radio" name="name1" [(ng-model)]="data.sel1"> Good
</label>To set the correct class - we use
ngClass
directive with taking status from concrete reference variable.
Related Topics
Material-Ui - Apply Max-Height to Select Children
Javafx Listview Selection Bar Text Color When Using Cellfactory
Internet Explorer Not Recognizing CSS for Svg Elements
React Js: Apply Material-Ui CSSbaseline
How to Center an Image Within a Col Element in Bootstrap 3+
Mvc: I Have Deployed My Application, But CSS Only Works When I Log In
Change The Size /Spacing of Label Text in R Shiny Fileinput
Firefox Displaying Table-Cell Incorrectly (Chrome Working Good)
Creating Small Navbar and a Side Panel in Materialize CSS
CSS Start Repeating Background from Defined Position
Angular Error: Postcss Received Undefined Instead of CSS String
Making a Row of Divs All Be The Same Height Using CSS
Why We Are Using Sass, Even We Are Using SCSS
Adding Background Image to Shiny Navbarpage
How to Style The Last Slotted Element in a Web Component
Trying to Build Less (Less CSS) Using a Build Script with Nodejs
Springboot - Resource Interpreted as Stylesheet But Transferred with Mime Type Text/Htm