【问题标题】:Creating a custom checkbox with CSS & Bootstrap使用 CSS 和 Bootstrap 创建自定义复选框
【发布时间】:2014-12-19 07:56:22
【问题描述】:

我使用引导框架进行了以下标记。

<div class="col-md-4">
  <div class="custom-container">
    <img class="center-block img-responsive img-circle invite-contact-trybe" src="{$img}" alt="Contact Image">
    <input class="invite-contact-checkbox" type="checkbox">
  </div>
</div>

我想实现以下目标:

有没有办法使用 CSS 来做到这一点?

我显然需要 3 个状态:

首字母:

.custom-container input[type=checkbox]{}

某种形式的悬停状态:

.custom-container input[type=checkbox]:hover{}

勾选时:

.custom-container  input[type=checkbox]:checked{}

谁能提出解决方案?

【问题讨论】:

  • hongkiat.com/blog/css3-checkbox-radio 有相当多的插件、想法等。谷歌为它。 Bootstrap 只是 css 和 js,没有为此内置任何东西。 Bootstrap 人尤其远离原生元素的样式。

标签: html css twitter-bootstrap checkbox customization


【解决方案1】:

背景图片复选框替换

让我们创建这个

这是一个使用:before 伪元素以及:checked:hover 状态的非常简单的示例。

有了这个干净的 HTML

<input type="checkbox" id="inputOne" />
<label for="inputOne"></label>

注意匹配的forid 属性,这会将标签附加到复选框。此外,元素的顺序非常重要;标签必须在输入之后,以便我们可以使用 input:checked 设置样式

以及这个基本 CSS

  • 复选框被display: none隐藏,所有交互都通过标签完成

  • :after 伪元素被赋予一个 unicode 标记 (\2714),但也可以使用背景图像进行标记。

  • 由边界半径引起的锯齿状边缘可以通过匹配颜色box-shadow 来柔化。当背景图像不是纯色块时,边框的内边缘看起来很好。

  • transition: all 0.4s 为边框创建平滑淡入/淡出。

我在 CSS cmets 中添加了更多指导。

完整示例

input[type=checkbox] {
  display: none;
}
/* 
- Style each label that is directly after the input
- position: relative; will ensure that any position: absolute children will position themselves in relation to it
*/

input[type=checkbox] + label {
  position: relative;
  background: url(http://i.stack.imgur.com/ocgp1.jpg) no-repeat;
  height: 50px;
  width: 50px;
  display: block;
  border-radius: 50%;
  transition: box-shadow 0.4s, border 0.4s;
  border: solid 5px #FFF;
  box-shadow: 0 0 1px #FFF;/* Soften the jagged edge */
  cursor: pointer;
}
/* Provide a border when hovered and when the checkbox before it is checked */

input[type=checkbox] + label:hover,
input[type=checkbox]:checked + label {
  border: solid 5px #F00;
  box-shadow: 0 0 1px #F00;
  /* Soften the jagged edge */
}
/* 
- Create a pseudo element :after when checked and provide a tick
- Center the content
*/

input[type=checkbox]:checked + label:after {
  content: '\2714';
  /*content is required, though it can be empty - content: '';*/
  height: 1em;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  margin: auto;
  color: #F00;
  line-height: 1;
  font-size: 18px;
  text-align: center;
}
<input type="checkbox" id="inputOne" />
<label for="inputOne"></label>

【讨论】:

  • 很好的答案,也喜欢使用新的stackoverflow代码sn-p!非常感谢,当我实现它时,我会在这里发布一个指向我最终解决方案的链接。谢谢 :D 声誉++
  • 我担心使用 css 对图像进行硬编码。你不能在这个解决方案中使用 元数据吗?我正在动态创建您看到的这些复选框! :)
  • @Chris - 您可以使用如下变量将背景图像放置在 HTML 中的标签上:&lt;label style="background: {$img}"&gt; 并从 CSS 样式表中删除当前背景图像。这样你提供网址的方法就不会改变:)
  • @misterManSam:你能帮我处理多张图片吗?当我尝试此代码时,它适用于单个图像,但多个图像会检查第一张图像,请帮助
  • @Rebecca - 如果您仍在寻找解决方案,您只需重命名每个 idfor 属性,以便每个复选框输入与其标签匹配。 Here is an example
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 2016-04-15
相关资源
最近更新 更多