1

I have an issue with my radio buttons, I try to put a border color when it is checked, nothing happens. I tried to read other topics about it, even tried to paste the answers I've found but it still doesn't change the border. It's probably some silly mistake that I made but I just can't find it, does anyone have the answer? Thanks a lot.

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

input[type="radio"]:checked {
  border-color: orange;
}
<div id="radio">
     <label>
        <input type="radio" name="sexe" value="Homme" id="homme">
        Homme
    </label>
    <label>
        <input type="radio" name="sexe" value="Femme" id="femme">
        Femme
    </label>
</div>
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
Guillaume Marc
  • 67
  • 1
  • 10

2 Answers2

4

You can not really change the style of basic radio button. You have to create a custom radio button css. Try this css:

 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: orange;
}

It works for me. I hope I can help.

B.Dani
  • 131
  • 3
2

Apparently, browsers don't allow much custom styling on checkboxes/radio buttons. - Jeremy Thille's comment

You could however, create your own radio button through css, an example of this can be found in this JsFiddle

What happens here:

  • We hide the borswer's radio input
  • We style create a custom radio button through css .checkmark
  • We show / hide a custom checked indicator using :checked, :after and the ~ General sibling combinator
  • Lastly, we style the checked indicator

Example found here

NOTE, as this is an example, it may be more than you require

The code

/* 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: #2196F3;
}

/* 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;
}
<label class="container">Homme
  <input type="radio" checked="checked" name="sexe">
  <span class="checkmark"></span>
</label>
<label class="container">Femme
  <input type="radio" checked="checked" name="sexe">
  <span class="checkmark"></span>
</label>

Hope this helps getting to your desired result

Mike Donkers
  • 3,589
  • 2
  • 21
  • 34
  • Alright!I didn't know we had to go through so much for a bit of coloring haha. That's exactly what I wanted, thanks a lot! – Guillaume Marc Jan 26 '18 at 10:48
  • @GuillaumeMarc You may not have to go through exactly as much as in my answer, it is simply an example of how you `could` do it. Glad I could help! Don't forget to mark the question as answered by clicking the checkmark next to the answer `you` think is answers it the best – Mike Donkers Jan 26 '18 at 10:52
  • Yes I get that, I'll look into it a bit more to grasp the whole mechanic. I'll sure do, thanks again! – Guillaume Marc Jan 26 '18 at 10:53