自定义单选按钮未显示

时间:2018-02-08 14:18:34

标签: html html5 css3 radio-button pseudo-element

我已经放置了自定义单选按钮,但单选按钮未显示。它按照这样的安排工作

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

为什么它不起作用。这段代码有什么问题吗?

我的HTML代码

<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代码

.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

2 个答案:

答案 0 :(得分:0)

加号是adjacent sibling selector - 它表示在您的情况下直接跟随的元素,在span之后需要input才能使样式生效:

&#13;
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

问题在于css样式,你正在设置自定义package com.example.afa.geobuddy; import android.view.View; import android.widget.Button; public class onClickListener implements View.OnClickListener { Button cancel_button = (Button) findViewById(R.id.btn_cancel); @Override public void onClick(View v) { } } 的类.radio_btns [type="radio"],它被添加到单选按钮并且无法正常显示,因此删除那些css属性以查看单选按钮。