【问题标题】:Custom designing checkbox buttons自定义设计复选框按钮
【发布时间】:2016-12-08 21:04:16
【问题描述】:

我正在尝试自定义设计复选框按钮,使它们看起来不像典型的复选框按钮。我试图实现以下目标:

(注意 checkcross 标记是图像。)

到目前为止我已经做到了:

但我似乎无法摆脱出现勾号的白色小复选框。我正在尝试更改选择时复选框按钮的 css。例如,是检查边框变为绿色,否则保持灰色。

有可能吗?

编辑:

.btn-primary {
  background-color: #f6f9fc;
  border: 1px solid Rgba(61, 70, 77, 0.1);
  font-size: 11px;
  color: #3d464d;
  width: 200px;
  height: 200px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />

<label class="btn btn-primary">
  <input type="checkbox" value="yes">yes
</label>

<label class="btn btn-primary">
  <input type="checkbox" value="no">no
</label>

【问题讨论】:

  • 是的,确定它们是图像,但您能发布您的代码吗?小提琴会很棒
  • @NicoO 谢谢你的评论。我已经解决了这个问题,在我的特殊情况下它无法提供帮助。
  • @JohannKratzik 请查看已编辑的问题。
  • 点击标签将切换复选框,因此您可以设置input[type="checkbox"] {display: none; } 来完成任务!

标签: html css checkbox


【解决方案1】:

只是玩玩,只有 css,没有图像或 js:

input[type="checkbox"].myClass {
  display: none;
}
input[type="checkbox"].myClass + label {
  box-shadow: inset 0 0 0 1px silver;
  border-radius: 0.25em;
  display: inline-block;
  font-family: sans-serif;
  opacity: 0.5;
  padding: 1em 1em 2em 1em;
  text-align: center;
  width: 10em;
  -webkit-user-select: none;
  user-select: none;
}
input[type="checkbox"].myClass + label:before {
  background-image: linear-gradient(to left, black, black), linear-gradient(to left, black, black);
  background-size: 2px 50%, 25% 2px;
  background-repeat: no-repeat;
  background-position: 0.75em 0.625em, 0.75em 0.625em;
  border-radius: 50%;
  box-shadow: inset 0 0 0 2px black;
  content: "";
  display: block;
  height: 2em;
  margin: 1em auto;
  transform: rotate(-135deg);
  width: 2em;
}
input[type="checkbox"].myClass + label:after {
  content: "Yes";
}
input[type="checkbox"].myClass:checked + label {
  box-shadow: inset 0 0 0 1px red, 0 0 0.25em 0 silver;
  color: red;
  opacity: 1;
}
input[type="checkbox"].myClass:checked + label:before {
  background-image: linear-gradient(to left, red, red), linear-gradient(to left, red, red);
  background-size: 2px 50%, 50% 2px;
  background-repeat: no-repeat;
  background-position: center center, center center;
  box-shadow: inset 0 0 0 2px red;
}
input[type="checkbox"].myClass:checked + label:after {
  content: "No";
}
<input class="myClass" id="o1" type="checkbox" /><label for="o1"></label>

<input class="myClass" id="o2" type="checkbox" checked/><label for="o2"></label>

【讨论】:

  • 感谢您的回答。这是比使用图像更好的解决方案。
  • 只有一个问题,如果我想为“是”和“否”设置单独的框,如何将这两个复选框的 CSS 分开?目前 css 正在应用于全局“输入”元素。
猜你喜欢
  • 2012-01-07
  • 1970-01-01
  • 2022-10-25
  • 2012-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多