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

标签 html css checkbox angular-material

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

enter image description here

HTML 代码:

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

提前致谢。

最佳答案

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

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

我已经根据 this 编写了一些 CSS (checkboxfour) 但我对其略作了更改以使其符合您的要求。

.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 个部分都是关于颜色的。中间的是您分享的图片中的橙色。

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

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

希望对您有所帮助。

关于html - 如何使用自定义 css 制作圆形复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37832505/

相关文章:

html - 3级div内的CSS相对与绝对

html - 在表格行上滚动不平滑 - Windows Phone

HTML : hover effect for all cell with different colours

javascript - 复选框 $.change 被自身触发并循环,因为它正在 $.change 内部被修改

javascript - 类事件处理程序不检测复选框是否被选中

javascript - 仅在确认()返回 true 后单选按钮才会更改

javascript - 如何在 Accordion 中隐藏/可见元素

jquery - CSS/jQuery 动画定位问题

html - CSS 获取文章中心

javascript - 根据在 javascript 和 asp.net 中选中的复选框禁用/启用 HtmlSelect