css - 带有 Font Awesome 5 图标的复选框

标签 css font-awesome pseudo-element font-awesome-5

有没有人设计过里面有 font awesome 5 图标的复选框?

我已经用谷歌搜索过了,只找到了 FA-4 的例子,比如 http://flatlogic.github.io/awesome-bootstrap-checkbox/demo/

我更新到 FA5 并使用 CSS 伪元素运行其他东西,但在复选框内它没有运行。我对没有伪元素的其他解决方案持开放态度。

提前致谢!

fiddle 示例:

Fiddle with FA-4.7

/* Checkboxes */

.checkbox input[type="checkbox"] {
  display: none;
}

.checkbox label {
  padding-left: 0;
}

.checkbox label:before {
  content: "";
  width: 20px;
  height: 20px;
  display: inline-block;
  vertical-align: bottom;
  margin-right: 10px;
  line-height: 20px;
  text-align: center;
  border: 1px solid #ccc;
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  font-family: "FontAwesome";
}

.checkbox input[type="checkbox"]:checked+label::before {
  content: "\f00c";
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<div class="checkbox">
  <input type="checkbox" id="profile_notifications" value="" checked/>
  <label for="profile_notifications">
    I agree
  </label>
</div>

Fiddle with FA-5

/* Checkboxes */

.checkbox {
  padding-left: 10px;
  padding-top: 10px;
}

.checkbox input[type="checkbox"] {
  display: none;
}

.checkbox label {
  padding-left: 0;
}

.checkbox label:before {
  content: "";
  width: 20px;
  height: 20px;
  display: inline-block;
  vertical-align: bottom;
  margin-right: 10px;
  line-height: 20px;
  text-align: center;
  border: 1px solid #ccc;
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  font-family: "Font Awesome 5 Solid";
  
}

.checkbox input[type="checkbox"]:checked+label::before {
  font-family: "Font Awesome 5 Solid";
  content: "\f00c";
}

.test {
  padding-left: 10px;
  padding-top: 10px;
}
.test .pseudo-element:before {
  display: none;
  font-family: "Font Awesome 5 Solid";
  content: "\f00c";
}
<script defer src="https://use.fontawesome.com/releases/v5.0.10/js/all.js" integrity="sha384-slN8GvtUJGnv6ca26v8EzVaR9DC58QEwsIk9q1QXdCU8Yu8ck/tL/5szYlBbqmS+" crossorigin="anonymous"></script>
<script>
    window.FontAwesomeConfig = {
    searchPseudoElements: true
  }
</script>

<div class="checkbox">
  <input type="checkbox" id="profile_notifications" value="" checked/>
  <label for="profile_notifications">
    Doesn't work!
  </label>
</div>

<div class="test">
  <label class="pseudo-element">This works!</label>
</div>

最佳答案

以上对我不起作用。我正在使用 Bootstrap 3.3.7(还使用 bootstrap 3.4.0 进行了测试)和 font awesome 5.5.0 free。 对我有用的是将其添加到我的自定义 css 文件中:

/* To get rid of the original and the benefit of not having the blue outline on focus */
input[type='checkbox'], input[type='radio'] {
    display:none;
}
.checkbox input[type='checkbox'] + label:before {
    font-family: 'Font Awesome 5 Free';
    content: "\f00c";
    color: #fff;
}
/* font weight is the only important one. The size and padding makes it look nicer */
.checkbox input[type='checkbox']:checked + label:before {
    font-weight:900;
    font-size: 10px;
    padding-left: 3px;
    padding-top: 0px;
}
.checkbox input[type="checkbox"]:checked + label::after,
.checkbox input[type="radio"]:checked + label::after {
    content: "";
}

编辑: 似乎将这种复选框放在 .input-group-addon 中时,复选框周围的边距和 checkbox::before 内的填充有点偏离。所以我用这个:

.input-group-addon .checkbox, .input-group-addon .radio {
    margin-top: 0px;
    margin-bottom: 0px;
}
.input-group-addon .checkbox input[type='checkbox']:checked + label:before {
    font-weight:900;
    font-size: 10px;
    padding-left: 2px;
    padding-top: 2px;
}

关于css - 带有 Font Awesome 5 图标的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49776701/

相关文章:

html - 如何使用 CSS 伪元素 :before{ content: '' } to affect an <option> element?

css - 设置两个div之间的div高度

当字体大小增加时,CSS Web 字体不会扩展 DIV?

Javascript: "Share button"在两个评论框 (jsfiddle) 上打开和关闭?

html - 使用 :after element - Animate out when mouse is out of button dimensions 对按钮的悬停效果

html - 带背景图片的 Internet Explorer 伪元素

html - 边框和尺寸在 Firefox 中不起作用

javascript - 在函数中,jQuery 选择器返回 PreviousObject 而不是所选对象

HTML/CSS : Can percentage width divs always completely fill their parent container (like a table cell does)?

javascript - Safari 上的 Font-Awesome onclick 问题,适用于 Chrome 和 Firefox