【问题标题】:changing the look of the radio button更改单选按钮的外观
【发布时间】:2017-03-25 00:37:33
【问题描述】:

我确信之前有人问过这个问题,但我正在寻找一种非常具体的方式来设置单选按钮的样式。 这是我目前拥有的fiddle

 <label for="pretty">
    <input type="radio" value="pretty" name="quality" id="pretty"> <span>pretty</span>
  </label>

  <label for="accessible-and-pretty">
    <input type="radio" value="pretty"  name="quality" id="accessible-and-pretty" checked> <span>accessible and pretty</span>
  </label>

我的问题是如何实现附加图像的外观和感觉?

我无法弄清楚如何使绿色区域变小,或者我需要做些什么来实现这一点

【问题讨论】:

  • 尝试谷歌搜索“radio button css”。
  • 嗯,是的,如果我没有谷歌,我不会说有一种方法可以在之前和之后做到这一点。不是我要找的答案
  • 我做了更新..看看上面的小提琴..我仍然需要帮助

标签: javascript css radio-button


【解决方案1】:

随意更改颜色、渐变和尺寸以满足您的要求。

.container {
  width: 100px;
  margin: 0 auto;
}
.radio-grp {
  margin-bottom: 10px;
}
input[type="radio"] {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  height: 0;
  width: 0;
}

input[type="radio"] + label {
  display: inline-block;
  position: relative;
  height: 30px;
  width: 30px;
  border-radius: 100%;
  background-color: #fff;
  border: 1px solid #ddd;
}

input[type="radio"] + label,
.radio-txt {
  cursor: pointer;
}

label.radio-txt {
  display: inline-block;
  float: right;
  padding-top: 5px;
}

input[type="radio"] + label:before {
  content: '';
  position: absolute;
  height: 22px;
  width: 22px;
  border-radius: 100%;
  top: 4px;
  left: 4px;
  box-shadow: 4px 5px 10px 0px #ccc inset;
}

input[type="radio"] + label:after {
  content: '';
  position: absolute;
  height: 10px;
  width: 10px;
  border-radius: 100%;
  top: 10px;
  left: 10px;
  transition: background 0.1s linear;
}

input[type="radio"]:checked + label:after {
  background-color: #888888;
}
<div class="container">
  <div class="radio-grp">
    <input type="radio" name="demo" value="1" id="radio1" checked>
    <label for="radio1"></label>
    <label for="radio1" class="radio-txt">Demo 1</label>
  </div>
  <div class="radio-grp">
    <input type="radio" name="demo" id="radio2" value="2">
    <label for="radio2"></label>
    <label for="radio2" class="radio-txt">Demo 2</label>
  </div>
  <div class="radio-grp">
    <input type="radio" name="demo" id="radio3" value="3">
    <label for="radio3"></label>
    <label for="radio3" class="radio-txt">Demo 3</label>
  </div>
</div>

【讨论】:

  • 你太棒了,谢谢!正是我想要的。快速提问。当我单击每个按钮时,我试图检查调试器中的代码,第一个按钮保持标记为选中,其余的不会将属性更改为选中。我的猜测是这些按钮默认应该这样做。我需要这个 Js 吗?
  • 尝试在他们的更改事件而不是他们的点击上触发调试器,我已经在第一个输入中添加了检查属性,以便在页面加载时保持检查,如果你希望所有字段都是,只需将其删除页面加载时未选中。
  • 您可以使用 :checked 选择器找到当前选中的选项。
  • 谢谢我明白了!
  • 乐于助人!
猜你喜欢
  • 2010-10-28
  • 2017-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多