【问题标题】:Customize Radio and Checkbox with CSS [duplicate]使用 CSS 自定义单选和复选框 [重复]
【发布时间】:2016-04-21 05:42:48
【问题描述】:

是否可以仅使用 CSS 自定义单选框和复选框的外观?我看到有很多关于此的内容,但大多数解决方案都需要使用图像和 javascript。

【问题讨论】:

  • 老兄,你问了这个问题,然后在你发帖 30 秒后自己回答了......
  • 这绝对是重复的。然而,我最喜欢的方法是在标签中包装一个复选框,隐藏输入元素本身,并设置标签的样式。

标签: css checkbox radio-button


【解决方案1】:

看起来 bootswatch.com 的人在使用纯 CSS 自定义输入方面做得很好。我在这里简化了他们在小提琴上的方法: https://jsfiddle.net/mthomas9261/qujbq4gs/

HTML:

<input type="radio" value="a" name="test">
<input type="radio" value="b" name="test">

<input type="checkbox" name="a" value="a">
<input type="checkbox" name="b" value="b">

CSS:

/*****************************************************
******************      Radio        *****************
******************************************************/

/* Hide Original Radio Button */
input[type="radio"] {
  position: relative;
  margin-top: 6px;
  margin-right: 4px;
  vertical-align: top;
  border: none;
  background-color: transparent;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
}
/* Remove standard blue selection outline */
input[type="radio"]:focus {
  outline: none;
}
/* New radio button */
input[type="radio"]:before,
input[type="radio"]:after {
  content: "";
  display: block;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  -webkit-transition: 240ms;
  -o-transition: 240ms;
  transition: 240ms;
}
input[type="radio"]:before {
  position: absolute;
  left: 1px;
  top: -2px;
  background-color: red;
  -webkit-transform: scale(0);
  -ms-transform: scale(0);
  -o-transform: scale(0);
  transform: scale(0);
}
input[type="radio"]:after {
  position: relative;
  top: -3px;
  border: 1px solid #E0E0E0;
}
/* New radio checked */
input[type="radio"]:checked:before {
  -webkit-transform: scale(0.5);
  -ms-transform: scale(0.5);
  -o-transform: scale(0.5);
  transform: scale(0.5);
}
input[type="radio"]:checked:after {
  border-color: #E0E0E0;
}
/* New radio disabled */
input[type="radio"]:disabled:after,
input[type="radio"]:disabled:checked:after {
  border-color: #F1F1F1;
}
input[type="radio"]:disabled:checked:before {
  background-color: #F1F1F1;
}

/*****************************************************
****************      Checkbox        ****************
******************************************************/
/* Hide Original Checkbox  */
input[type="checkbox"] {
  position: relative;
  border: none;
  margin-bottom: -4px;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
}
/* Display New Checkox */
input[type="checkbox"] {
  position: relative;
  border: none;
  margin-bottom: -4px;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
}
input[type="checkbox"]:focus,
.checkbox input[type="checkbox"]:focus,
.checkbox-inline input[type="checkbox"]:focus {
  outline: none;
}
input[type="checkbox"]:focus:after,
.checkbox input[type="checkbox"]:focus:after,
.checkbox-inline input[type="checkbox"]:focus:after {
  border-color: #E0E0E0;
}
input[type="checkbox"]:after,
.checkbox input[type="checkbox"]:after,
.checkbox-inline input[type="checkbox"]:after {
  content: "";
  display: block;
  width: 14px;
  height: 14px;
  margin-top: -2px;
  margin-right: 5px;
  border: 1px solid #E0E0E0;
  border-radius: 1px;
  -webkit-transition: 240ms;
  -o-transition: 240ms;
  transition: 240ms;
}
/* Checkbox Checked  */
input[type="checkbox"]:checked:before {
  content: "";
  position: absolute;
  top: -2px;
  left: 5px;
  display: table;
  width: 4px;
  height: 9px;
  border: 3px solid red;
  border-top-width: 0;
  border-left-width: 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}
input[type="checkbox"]:checked:after {
  border-color: #E0E0E0;
}
/* Checkbox Disabled  */
input[type="checkbox"]:disabled:after {
  border-color: #F1F1F1;
}
input[type="checkbox"]:disabled:checked:after {
  background-color: #F1F1F1;
  border-color: transparent;
}

【讨论】:

  • input[type="radio"]:before 和类似的在技术上是无效的 HTML5。 input 是一个 void 元素,规范未定义对 ::before::after 伪元素的支持。
猜你喜欢
  • 2011-09-18
  • 1970-01-01
  • 2016-02-11
  • 2016-04-15
  • 2014-12-19
  • 2019-10-02
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多