How to Style CSS Checkboxes with Font Awesome

32,998

Solution 1

Ok, that CSS you have won't work because its wrong.

Why? Because when you say "input + label", you should have an HTML markup like the one below:

<input type="checkbox" name="ofcards-rarity[]" value="15">
<label>Legendary (36)</label> //You will be querying this label css with input + label

See, <label> is placed immediately after <input>. You can confirm this HERE

Now in your case, your <input> was a child of you <label>, looking like this:

<label>
            <input type="checkbox" name="ofcards-rarity[]" value="15">Legendary (36)
</label>

To query that, you could have done something like this:

label>input[type=checkbox] {

}
label>input[type=checkbox]:checked {

}

And since you wanted to put something beetwen them, you add this to your css:

label>input[type=checkbox]:before {

}
label>input[type=checkbox]:checked:before {

}

I've adjusted it for you. It's not the easiest/cutest way to implement it, but at least works with your current HTML.

Here's the FIDDLE

Solution 2

I created checkboxes and radio buttons using Font Awesome. The ones I found online had something or the other missing. I needed ones that could be scaled and I didn't want any unclickable gap between the checkbox and its label.

Here are links to the repository and the demo

Solution 3

No JavaScript but small html manipulation like adding label with "for" attribute. so that when ever click on label checkbox click will trigger. enter image description here

    .form input[type="checkbox"]{
  display:none;  
}
.form input[type="checkbox"] + label.fa {
    color: #88E2E2;
    font-size: 25px;
    width: 25px;
    height: 25px;
    cursor: pointer;
}
.form input[type="checkbox"]:checked +label.fa{
    background: #fff;
}
.form input[type="checkbox"] + label.fa:before {
     display:inline-block;
     content: "\f096";
     cursor:pointer;
}

.form input[type="checkbox"]:checked +label.fa:before{
    content:"\f046";
    position: relative;
    left: 2px;
}
 <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
    <form class="form">
    <input id="check_1" type="checkbox"/><label class="fa" for="check_1"></label>
          <input id="check_2" type="checkbox"/><label class="fa" for="check_2"></label>
    <input id="check_3" type="checkbox"/><label class="fa" for="check_3"></label>

    </form>
Share:
32,998
Greenhoe
Author by

Greenhoe

Updated on October 13, 2020

Comments

  • Greenhoe
    Greenhoe over 3 years

    I am trying to style my checkboxes with Font Awesome, the checkboxes are auto generated from a plugin I"m using with wordpress I have a mockup of what everything looks like in JSFiddle

    http://jsfiddle.net/bBPY5/1/

    It seems to be something a bit wrong with my CSS but I can't figure out what.

    <div id="sidebar-cards-archive">
    <ul>
        <li class="cat-item cat-item-12">
            <label>
                <input type="checkbox" name="ofcards-rarity[]" value="12">Common (223)</label>
        </li>
        <li class="cat-item cat-item-14">
            <label>
                <input type="checkbox" name="ofcards-rarity[]" value="14">Epic (40)</label>
        </li>
        <li class="cat-item cat-item-11">
            <label>
                <input type="checkbox" name="ofcards-rarity[]" value="11">Free (83)</label>
        </li>
        <li class="cat-item cat-item-15">
            <label>
                <input type="checkbox" name="ofcards-rarity[]" value="15">Legendary (36)</label>
        </li>
        <li class="cat-item cat-item-13">
            <label>
                <input type="checkbox" name="ofcards-rarity[]" value="13">Rare (84)</label>
        </li>
    </ul>
    </div>
    

    Here is the CSS

     @import url(//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);
     #sidebar-cards-archive ul li {
         list-style: none;
     }
     /*** custom checkboxes ***/
     input[type=checkbox] {
         display:none;
     }
     /* to hide the checkbox itself */
     input[type=checkbox] + label:before {
         font-family: FontAwesome;
         display: inline-block;
     }
     input[type=checkbox] + label:before {
         content:"\f096";
     }
     /* unchecked icon */
     input[type=checkbox] + label:before {
         letter-spacing: 10px;
     }
     /* space between checkbox and label */
     input[type=checkbox]:checked + label:before {
         content:"\f046";
     }
     /* checked icon */
     input[type=checkbox]:checked + label:before {
         letter-spacing: 5px;
     }
     /* allow space for check mark */
    
  • Greenhoe
    Greenhoe about 10 years
    Thank you very much! I greatly appreciate you helping me with this.
  • technophyle
    technophyle over 6 years
    The problem with your solution is it is not accessible.