如何在CSS中重新设置radio类型的输入的样式?

时间:2019-04-23 07:47:59

标签: html css input

我想创建自己的单选按钮,但似乎无法重新设置它们的样式。

我得到了以下html:

.segment {
  all: unset;
  position: relative;
  border: solid 1px blue;
  height: 2rem;
  width: 3rem;
  -webkit-tap-highlight-color: transparent;
}
<div>
  <input id="segemnt0" class="segment" type="radio" name="segment">
  <input id="segemnt1" class="segment" type="radio" name="segment">
  <input id="segemnt2" class="segment" type="radio" name="segment">
</div>

这不起作用。什么都没有显示。如果我删除“全部:未设置;”它只显示没有边框等常规样式的复选框。

2 个答案:

答案 0 :(得分:2)

您可以自定义添加<label>标签和CSS的单选按钮

这是小提琴,可能会帮助您:

[type="radio"]:checked,
[type="radio"]:not(:checked) {
  position: absolute;
  left: -9999px;
}

[type="radio"]:checked+label,
[type="radio"]:not(:checked)+label {
  position: relative;
  padding-left: 28px;
  cursor: pointer;
  line-height: 20px;
  display: inline-block;
  color: #666;
}

[type="radio"]:checked+label:before,
[type="radio"]:not(:checked)+label:before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 18px;
  height: 18px;
  border: 1px solid #ddd;
  border-radius: 100%;
  background: #fff;
}

[type="radio"]:checked+label:after,
[type="radio"]:not(:checked)+label:after {
  content: '';
  width: 12px;
  height: 12px;
  background: #F87DA9;
  position: absolute;
  top: 4px;
  left: 4px;
  border-radius: 100%;
  -webkit-transition: all 0.2s ease;
  transition: all 0.2s ease;
}

[type="radio"]:not(:checked)+label:after {
  opacity: 0;
  -webkit-transform: scale(0);
  transform: scale(0);
}

[type="radio"]:checked+label:after {
  opacity: 1;
  -webkit-transform: scale(1);
  transform: scale(1);
}
<input id="segemnt0" class="segment" type="radio" name="segment">
<label for="segemnt0">one</label>
<input id="segemnt1" class="segment" type="radio" name="segment">
<label for="segemnt1">two</label>
<input id="segemnt2" class="segment" type="radio" name="segment">
<label for="segemnt2">three</label>

答案 1 :(得分:1)

尝试带有标签的样品。您需要在代码中添加标签以设置收音机的样式。

input[type=radio] {
  display: none;
}
input[type=radio] + label:before {
  content: '';
  display: inline-block;
  border: 2px solid #0F81D5;
  height: 12px;
  width: 12px;
  border-radius: 50%;
  margin: 2px 5px 0 0px
}
input[type=radio]:checked + label:before {
  border-width: 5px;
  height: 6px;
  width: 6px;
}
<div>
  <input type="radio" name="radio" id="radio1" />
  <label for="radio1">radio1</label>
</div>
<div>    
  <input type="radio" name="radio" id="radio2" />
  <label for="radio2">radio2</label>
</div>
<div>    
  <input type="radio" name="radio" id="radio3" />
  <label for="radio3">radio3</label>
</div>