LoginSignup
4
3

More than 5 years have passed since last update.

チェックボックスやラジオボタンの見た目に Font Awesome のアイコンを使う

Last updated at Posted at 2017-12-08

制限事項

サンプルコードの HTML は Bootstrap 4 を導入していることを前提としています。また Bootstrap 4 の ドキュメント に掲載されている HTML に合わせるため、input 要素に関しては label 要素の中に input 要素を配置する書き方とします。

ソース

HTML (Slim)

.form-check
  label.form-check-label
    input.form-check-input type='checkbox' name='madohomu' value='madoka'
    span.label-name 鹿目まどか
.form-check
  label.form-check-label
    input.form-check-input type='checkbox' name='madohomu' value='homura'
    span.label-name 暁美 ほむら

.form-check
  label.form-check-label
    input.form-check-input  type='radio' name='kyosaya' value='kyoko' checked=true
    span.label-name 佐倉 杏子
.form-check
  label.form-check-label
    input.form-check-input type='radio' name='kyosaya' value='kyoko'
    span.label-name 美樹 さやか

CSS (SCSS)

input[type='checkbox'] {
  display: none;

  & + .label-name::before {
    display: inline-block;
    font-family: FontAwesome;
    margin-left: -1.25rem; // .form-check-label の padding-left を打ち消す。
    margin-left: -1.25rem; 
    width: 1.2rem;
  }

  & + .label-name::before {
    content: "\f096"; // fa-square-o
  }

  &:checked + .label-name::before {
    content: "\f046"; // fa-check-square-o
  }
}

input[type='radio'] {
  display: none;

  & + .label-name::before {
    display: inline-block;
    font-family: FontAwesome;
    margin-left: -1.25rem;
    width: 1.2rem;
  }

  & + .label-name::before {
    content: "\f10c";
  }

  &:checked + .label-name::before {
    content: "\f05d"; // fa-check-circle-o
  }
}

スクリーンショット

スクリーンショット 2017-12-08 16.12.24.png

デモ

Checkbox meets Font Awesome

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3