如何使用自定义 css 制作圆形复选框?

新手上路,请多包涵

如何使用自定义 CSS 使复选框变圆?类似于下图的东西。我知道有像 bootstrap 这样的框架提供了这个,但我不想将框架用于这个唯一目的,因为我已经在使用角材料了。

在此处输入图像描述

HTML 代码:

     <label class="checkbox-inline">
        <input type="checkbox" name="favoriteColors" ng-model="notification.checked">
    </label>

提前致谢。

原文由 Sha 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.4k
2 个回答

如果您确定要复选框而不是单选按钮

复选框一般是方形的,可以勾选几个,单选按钮是圆形的,但是一组只能选一个

我已经基于 (checkboxfour) 编写了一些 CSS,但我对其进行了轻微更改以使其符合您的要求。

 .customcb {
  width: 17px;
  height: 17px;
  margin: 2px 0 2px 0;
  background: #ddd;
  border-radius: 100%;
  position: relative;
  -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, .5);
  -moz-box-shadow: 0 1px 1px rgba(0, 0, 0, .5);
  box-shadow: 0 1px 1px rgba(0, 0, 0, .5);
}
.customcb label.inner {
  display: block;
  width: 12.75px;
  height: 12.75px;
  border-radius: 100px;
  -webkit-transition: all .5s ease;
  -moz-transition: all .5s ease;
  -o-transition: all .5s ease;
  -ms-transition: all .5s ease;
  transition: all .5s ease;
  cursor: pointer;
  position: absolute;
  top: 2.125px;
  left: 2.125px;
  z-index: 1;
  background: #eee;
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .5);
  -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .5);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, .5)
}
.customcb label.outer {
  margin-left: 25px;
}
.customcb [type=checkbox] {
  display: none
}
.red.customcb input[type=checkbox]:checked+label.inner {
  background: red
}
.orange.customcb input[type=checkbox]:checked+label.inner {
  background: #d61
}
.green.customcb input[type=checkbox]:checked+label.inner {
  background: green
}
 <div class="red customcb">
  <input type="checkbox" value="1" id="customcb1" name="" />
  <label class="inner" for="customcb1"></label>
  <label class="outer" for="customcb1">Red</label>
</div>

<div class="orange customcb">
  <input type="checkbox" value="1" id="customcb2" name="" />
  <label class="inner" for="customcb2"></label>
  <label class="outer" for="customcb2">Amber</label>
</div>

<div class="green customcb">
  <input type="checkbox" value="1" id="customcb3" name="" />
  <label class="inner" for="customcb3"></label>
  <label class="outer" for="customcb3">Green</label>
</div>

我还展示了您可以通过更改类来使用不同的颜色,当然还可以将颜色添加到 CSS,CSS 的最后 3 个部分都是关于颜色的。中间的那个是你分享的图片中的橙色。

如果您按照我链接的教程进行操作,您将很好地了解我所做的事情以及原因。

我认为这可能值得一读, 涵盖了所有输入类型(如果您已经了解它们,请随意忽略)

我希望这有帮助。

原文由 Andrew Bone 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题