Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change border-color of radio button when selected

I want to have a green radio button surrounded with a green border when I select it.
This is what I have:

input[type='radio'] {
        -webkit-appearance: none;
        width: 10px;
        height: 10px;
        border-radius: 50%;
        outline: none;
        box-shadow: 0 0 0 2px gray;
    }

    input[type='radio']:before {
        content: '';
        display: block;
        width: 60%;
        height: 60%;
        margin: 20% auto;
        border-radius: 50%;
    }

    input[type='radio']:checked:before {
        background: green;
    }

    .role {
        margin-right: 80px;
        margin-left: 20px;
        font-weight: normal;
    }

    .checkbox label {
        margin-bottom: 20px !important;
    }

    .roles {
        margin-bottom: 40px;
    }

And the template:

<div class="roles">
    <input type="radio" name="role" value="ONE" id="one">
    <label class="role" for="one">ONE</label>
    <input type="radio" name="role" value="TWO" id="two">
    <label class="role" for="two">TWO</label>
</div>

Here is a Jsfiddle: Demo

Like you can see the border-color never changes to green... I tried to add border-color property to no avail... How can I do it?

Thanks!

like image 693
Anna Avatar asked Apr 24 '17 13:04

Anna


People also ask

How do you change the radio button border color in flutter?

To change radio button color in Flutter, add fillColor property to the Radio widget. Inside the fillColor use the MaterialStateColor to add the color of your choice.

How do I color a radio button in CSS?

Syntax: accent-color : auto | color | initial | inherit; Property values: auto: The browser will set the accent color for the control elements.


2 Answers

Hey mate you need to check your css. The border you got is created by box-shadow and not by a border:

here is a fiddle that is working. Have Fun

input[type='radio'] {
        -webkit-appearance: none;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        outline: none;
        border: 3px solid gray;
    }

    input[type='radio']:before {
        content: '';
        display: block;
        width: 60%;
        height: 60%;
        margin: 20% auto;
        border-radius: 50%;
    }

 input[type="radio"]:checked:before {
        background: green;
        
    }
    
    input[type="radio"]:checked {
      border-color:green;
    }

    .role {
        margin-right: 80px;
        margin-left: 20px;
        font-weight: normal;
    }

    .checkbox label {
        margin-bottom: 20px !important;
    }

    .roles {
        margin-bottom: 40px;
    }
<div class="roles">
    <input type="radio" name="role" value="ONE" id="one">
    <label class="role" for="one">ONE</label>
    <input type="radio" name="role" value="TWO" id="two">
    <label class="role" for="two">TWO</label>
</div>
like image 154
RacoonOnMoon Avatar answered Sep 27 '22 23:09

RacoonOnMoon


You could just update the box-shadow color:

input[type='radio'] {
  -webkit-appearance: none;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  outline: none;
  box-shadow: 0 0 0 2px gray;
}

input[type='radio']:checked {
  box-shadow: 0 0 0 2px green;
}

input[type='radio']:before {
  content: '';
  display: block;
  width: 60%;
  height: 60%;
  margin: 20% auto;
  border-radius: 50%;
}

input[type='radio']:checked:before {
  background: green;
}

.role {
  margin-right: 80px;
  margin-left: 20px;
  font-weight: normal;
}

.checkbox label {
  margin-bottom: 20px !important;
}

.roles {
  margin-bottom: 40px;
}
<div class="roles">
  <input type="radio" name="role" value="ONE" id="one">
  <label class="role" for="one">ONE</label>
  <input type="radio" name="role" value="TWO" id="two">
  <label class="role" for="two">TWO</label>
</div>
like image 36
sol Avatar answered Sep 27 '22 23:09

sol