0

I have put custom radio button, but radio button is not showing. It works when arrange like this

<label>   
<input type="radio" name="gender" />
<span>Female</span>
</label>

Why it is not working. Is there any wrong with this code?

My HTML Code

<div class="radio_btns">
<label><span>Gender</span></label>
<label>
  <span>Female</span>
  <input type="radio" name="gender" />
</label>
<label>
  <span>Male</span>
  <input type="radio" name="gender" />
</label>
</div>

CSS Code

.radio_btns {width:100%; float:left;}
.radio_btns [type="radio"] {border: 0; clip: rect(0 0 0 0); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px;}
.radio_btns label {display: block; cursor: pointer; line-height: 2.5;}
.radio_btns [type="radio"] + span {display: block;}
.radio_btns [type="radio"] + span:before {content: ''; display: inline-block; width: 1em; height: 1em; vertical-align: -0.25em; border-radius: 1em; border: 0.125em solid #fff; box-shadow: 0 0 0 0.15em #000; margin-right: 0.75em; transition: 0.5s ease all;}
.radio_btns [type="radio"]:checked + span:before {background: #07eb07; box-shadow: 0 0 0 0.25em #000;}
.radio_btns label {float:left; width:150px;}

What I get with this code

tom
  • 73
  • 8
User113
  • 153
  • 1
  • 2
  • 14
  • You are using a neighbor selector, but it will not affect the previous neighbor. Also, look at [this](https://stackoverflow.com/questions/1817792/is-there-a-previous-sibling-css-selector) post. – Anonymous Feb 08 '18 at 14:23

2 Answers2

0

The plus is an adjacent sibling selector - it means the element directly following so in your case, you need a span after your input for the styles to work:

.radio_btns {
  width: 100%;
  float: left;
}

.radio_btns [type="radio"] {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
}

.radio_btns label {
  display: block;
  cursor: pointer;
  line-height: 2.5;
}

.radio_btns [type="radio"]+span {
  display: inline-block;
}

.radio_btns [type="radio"]+span:before {
  content: '';
  display: inline-block;
  width: 1em;
  height: 1em;
  vertical-align: -0.25em;
  border-radius: 1em;
  border: 0.125em solid #fff;
  box-shadow: 0 0 0 0.15em #000;
  margin-right: 0.75em;
  transition: 0.5s ease all;
}

.radio_btns [type="radio"]:checked+span:before {
  background: #07eb07;
  box-shadow: 0 0 0 0.25em #000;
}

.radio_btns label {
  float: left;
  width: 150px;
}
<div class="radio_btns">
  <label><span>Gender</span></label>
  <label>
  <span>Female</span>
  <input type="radio" name="gender" />
  <span></span>
</label>
  <label>
  <span>Male</span>
  <input type="radio" name="gender" />
  <span></span>
</label>
</div>
Pete
  • 57,112
  • 28
  • 117
  • 166
-1

The problem is with the css style, there for the class .radio_btns [type="radio"] you are setting custom height: 1px; width: 1px; and position: absolute; which is added to the radio button and not able to visible properly so remove those css property to see the radio buttons.

Umesh M Gowda
  • 121
  • 1
  • 13