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

How to make a checkbox in circle shape with custom css?

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

HTML代码:

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

提前致谢。

你直接使用这里的css http://flatlogic.github.io/awesome-bootstrap-checkbox/demo/ .. 只需在您的代码中包含 .checkbox-circle class 并使用。

使用单选按钮。 如果你想多选,你可以为所有单选按钮设置相同的名称并使用不同的id。

您可以为标签使用伪类,并在视觉上隐藏实际的复选框。

/* Base for label styling */
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
  position: absolute;
  left: -9999px;
}
[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label {
  position: relative;
  padding-left: 25px;
  cursor: pointer;
}

/* checkbox aspect */
[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before {
  content: '';
  position: absolute;
  left:0; top: 2px;
  width: 18px; height: 18px;
  border-radius: 9px; //this should be half of height and width
  border: 1px solid #999;
  background: #ffff;
  box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
}
/* checked mark aspect */
[type="checkbox"]:checked + label:before {
  background: orange;
}

希望这对您有所帮助...:-)

/**
     * Checkbox Four
     */
    .checkboxFour {
    width: 40px;
    height: 40px;
    background: #ddd;
    margin: 20px 90px;
    border-radius: 100%;
    position: relative;
    -webkit-box-shadow: 0px 1px 3px rgba(0,0,0,0.5);
    -moz-box-shadow: 0px 1px 3px rgba(0,0,0,0.5);
    box-shadow: 0px 1px 3px rgba(0,0,0,0.5);
    }

    /**
     * Create the checkbox button
     */
    .checkboxFour label {
    display: block;
    width: 30px;
    height: 30px;
    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: 5px;
    left: 5px;
    z-index: 1;
    background: #333;
    -webkit-box-shadow:inset 0px 1px 3px rgba(0,0,0,0.5);
    -moz-box-shadow:inset 0px 1px 3px rgba(0,0,0,0.5);
    box-shadow:inset 0px 1px 3px rgba(0,0,0,0.5);
    }

    /**
     * Create the checked state
     */
    .checkboxFour input[type=checkbox]:checked + label {
    background: #26ca28;
    }
<section>

      <div class="checkboxFour">
      <input type="checkbox" value="1" id="checkboxFourInput" name="" />
      <label for="checkboxFourInput"></label>
      </div>
    </section>

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

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

我已经根据 this(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>

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

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

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

希望对您有所帮助。

只需将复选框的类型设置为等于复选框,并在 CSS 样式表中将边框半径添加为 100%,并指定高度和宽度(它们应该彼此相同,以便做一个正方形作为你的圆的底部),为了使它们完全圆润,就像在完美的圆圈中一样。就这么简单。

使用

.custom-checkbox .custom-control-label::before {
  border-radius: 999px;
}

https://jsfiddle.net/lucashuang/ew4gc1aL/24/

https://i.stack.imgur.com/dtiYB.png