【问题标题】:How to color the border of checkbox in css [duplicate]如何在css中为复选框的边框着色[重复]
【发布时间】:2017-01-18 10:34:38
【问题描述】:

我想给复选框的边框上色。我写了下面的 css -

input[type=checkbox] {
            height: 15px;
            width: 15px;

            border: 1px solid #007dc6;
            -webkit-appearance: none;
        }

这仅适用于 chrome。我不想在 CSS 中编写特定于浏览器的代码。 css 应该适用于所有最新的浏览器。

【问题讨论】:

  • 复选框在所有浏览器中看起来都不一样……它们都有自己的实现。您最好使用下面答案中提到的替换方法进行完全控制和跨浏览器比较。

标签: css


【解决方案1】:

可以通过隐藏默认复选框并使用 :before 元素和 :checkednot(:checked) 选择器设置我们自己的复选框来操作复选框的外观

一个例子:

body {
  font-family: 'segoe ui';
  background-color: white;
  padding: 10px;
}
.space {
  height: 10px;
}
.checkbox * {
  box-sizing: border-box;
  position: relative;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
.checkbox {
  display: inline-block;
}
.checkbox > input {
  display: none;
}
.checkbox > label {
  vertical-align: top;
  font-size: 18px;
  padding-left: 30px;
}
.checkbox > [type="checkbox"] + label:before {
  color: #777;
  content: '';
  position: absolute;
  left: 0px;
  display: inline-block;
  min-height: 20px;
  height: 20px;
  width: 20px;
  border: 2px solid #777;
  font-size: 15px;
  vertical-align: top;
  text-align: center;
  transition: all 0.2s ease-in;
  content: '';
}
.checkbox.radio-square > [type="checkbox"] + label:before {
  border-radius: 0px;
}
.checkbox.radio-rounded > [type="checkbox"] + label:before {
  border-radius: 25%;
}
.checkbox.radio-blue > [type="checkbox"] + label:before {
  border: 2px solid #ccc;
}
.checkbox > [type="checkbox"] + label:hover:before {
  border-color: lightgreen;
}
.checkbox > [type="checkbox"]:checked + label:before {
  width: 8px;
  height: 16px;
  border-top: transparent;
  border-left: transparent;
  border-color: green;
  border-width: 4px;
  transform: rotate(45deg);
  top: -4px;
  left: 4px;
}
<div class="checkbox">
  <input id="chkTest" type="checkbox" />
  <label for="chkTest">Task one</label>
  <div class="space">

  </div>

  <input id="chkTest1" type="checkbox" />
  <label for="chkTest1">Task two</label>
</div>

小提琴:https://jsfiddle.net/jo_Geek/jc81o4x0/

【讨论】:

  • 我的代码中的 HTML 是
    所以它没有标签作为 input[type=checkbox] 的兄弟。您能为我的 html 代码提供示例吗?
  • 没有标签的复选框,用户怎么知道它的用途? ?
猜你喜欢
  • 1970-01-01
  • 2015-05-27
  • 2016-07-23
  • 2014-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
  • 2011-09-14
相关资源
最近更新 更多