Customizing Checkbox And Radio Button In HTML Using Font Awesome

The article describes how we can customize the html checkbox and radio button using FontAwesome, css, and Jquery.

Introduction

Most of the time, there is a requirement of customizing the html input's UI due to different reasons,  it could be one of the below:

  • We want the users to see a similar UI irrespective of browsers and operating systems.
  • Customized UI looks great and it changes the look and feel of the application.

And we do it by downloading a CSS file, Jquery file or combination of CSS and Jquery file. Well that’s great – but actually that has some size which will increase the load time of our application and you don't have control over them.

So, the next question will be how to do it?

We can customize the UI using CSS. In the case of html input type - button, text, textarea, and dropdown we can customize the UI using CSS by modifying its height and width, adding some border, changing the hover style, etc.

But it doesn’t work well in case of radio buttons and checkboxes.

So, in this article - I am going to explain how we can use CSS and FontAwesome to customize the checkbox and radio button.

Prerequisites

  • Html
  • Css
  • Jquery

Let’s Start.

Creating Checkbox


Create a html page with some checkbox

  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <title>Custom Checkbox using </title>  
  7.     <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">  
  8.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>  
  9.     <style>  
  10.         body {  
  11.             font-size: 20px;  
  12.         }  
  13.     </style>  
  14. </head>  
  15.   
  16. <body>  
  17.     <div class="checkbox1">  
  18.         <h2>Checkbox 1</h2>  
  19.         <h3>Q.What is your most favourite food?</h3>  
  20.         <ul style="list-style-type:none;">  
  21.             <li><input type="checkbox" /><label>Burger</label></li>  
  22.             <li><input type="checkbox" /><label>Pizza</label></li>  
  23.             <li><input type="checkbox" /><label>SandWitch</label></li>  
  24.             <li><input type="checkbox" /><label>Chicken with Rice</label></li>  
  25.             <li><input type="checkbox" /><label>Chicken with Paratha</label></li>  
  26.         </ul>  
  27.     </div>  
  28. </body>  
  29.   
  30. </html>  
and the output of the above code will be -

CSS

Now let's customize the checkbox using font awesome. You can see I have already loaded the font awesome and jquery using cdn.

Customizing checkbox

We will have to add some css code, so paste the below code.

 

  1. <style>  
  2.     input[type=checkbox] {  
  3.         display: none;  
  4.     }  
  5.       
  6.     input[type=checkbox]+label:before {  
  7.         font-family: FontAwesome;  
  8.         display: inline-block;  
  9.     }  
  10.     /* checkbox1 */  
  11.     /* unchecked icon */  
  12.       
  13.     .checkbox1 input[type=checkbox]+label:before {  
  14.         content: "\f096";  
  15.         letter-spacing: 10px;  
  16.     }  
  17.     /* checked icon */  
  18.       
  19.     .checkbox1 input[type=checkbox]:checked+label:before {  
  20.         letter-spacing: 5px;  
  21.         content: "\f046";  
  22.     }  
  23. </style>  

Above code will modify the Checkbox UI. So the next question will be what is the above CSS code doing? So here is the trick.

  1. We are hiding the Checkbox –so the original checkbox won’t be visible.
  2. We are selecting the checkbox and label by selector – “input[type=checkbox] + label ” and using pseudo CSS Selector “before” to change everything.
  3. We are changing the font family to “font awesome” and displaying to “inline block”.
  4. We have two situations – (i) To show the checkbox when it is checked (ii) To show the checkbox when it is unchecked. So we are using the native work of checkbox but not UI.
  5. So when the checkbox will be checked, the content will be something else (“\f096” ) and when the checkbox will not be checked, the content will be something else (“\f046”). And these all are being done by CSS.

    Now, if you will render the above page in browser, you will see the customized checkbox, but when you will click on it - it won't work. That is because currently the customized checkbox is only an icon (you can say a picture) so we will have to manually change the icon when user clicks on icon.

Add Jquery to click on checkbox.

  1. <script>  
  2.     $('ul li').click(function() {  
  3.         var CheckBox = $(this).find('input[type="checkbox"]');  
  4.         if (CheckBox.attr('checked')) {  
  5.             CheckBox.attr('checked'false);  
  6.         } else {  
  7.             CheckBox.attr('checked'true);  
  8.         }  
  9.   
  10.     })  
  11. </script>  
The above script listens for the click event on "li" and when the click event is triggered - it will find the respective checkbox and toggle its checked attribute.

Now, view the page in the browser. You will see the customized checkbox which will work the same as the native checkbox with different look.

CSS

I have created the four types of checkboxes. Please see the html page in download file. Below is the collection of four checkboxes.

CSS

The CSS of every checkbox is same, only the content value is changed. Now, you can even change the color of the icon by applying the CSS “color” and size.

Note

You can get the content value from the internet by searching font awesome content values.

Creating Radio Button

Create a html page with some Radio Buttons.
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <title>Custom Checkbox using </title>  
  7.     <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">  
  8.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>  
  9.   
  10.     <style>  
  11.         body {  
  12.             . font-size: 20px;  
  13.         }  
  14.     </style>  
  15. </head>  
  16.   
  17. <body>  
  18.   
  19.     <div class="radio1">  
  20.         <h2>Radio1</h2>  
  21.         <h3>Q.What is your most favourite food?</h3>  
  22.         <ul style="list-style-type:none;">  
  23.             <li><input type="radio" name="radio1" /><label>Burger</label></li>  
  24.             <li><input type="radio" name="radio1" /><label>Pizza</label></li>  
  25.             <li><input type="radio" name="radio1" /><label>SandWitch</label></li>  
  26.             <li><input type="radio" name="radio1" /><label>Chicken with Rice</label></li>  
  27.             <li><input type="radio" name="radio1" /><label>Chicken with Paratha</label></li>  
  28.         </ul>  
  29.   
  30.     </div>  
  31. </body>  
  32.   
  33. </html>  
and the output of the above code will be,

CSS

Customizing checkbox

We will have to add some css code, so paste the below code.
  1. <style>  
  2.     input[type=radio] {  
  3.         display: none;  
  4.     }  
  5.       
  6.     input[type=radio]+label:before {  
  7.         font-family: FontAwesome;  
  8.         display: inline-block;  
  9.     }  
  10.     /* radio1 */  
  11.     /* unchecked icon */  
  12.       
  13.     .radio1 input[type=radio]+label:before {  
  14.         content: "\f1db";  
  15.         letter-spacing: 10px;  
  16.     }  
  17.     /* checked icon */  
  18.       
  19.     .radio1 input[type=radio]:checked+label:before {  
  20.         letter-spacing: 5px;  
  21.         content: "\f192";  
  22.     }  
  23. </style>  
The css code is the same as the code for checkboxes, only content value is changed.

Add Jquery to click on checkbox.
  1. <script>  
  2.     $('ul li').click(function() {  
  3.         var $radio = $(this).find('input[type="radio"]');  
  4.         $radio.prop('checked', !$radio.prop('checked'));  
  5.         $radio.prop('checked'true);  
  6.     })  
  7. </script>  
Now, view the page in the browser. You will see the customized radio buttons which will work the same as the native radio button with different look.

CSS

I have created the three types of radio buttons. Please see the html page in download file. Below is the screenshot for the collection of three radio buttons 

CSS

Now, these checkboxes and radio buttons look good and the most important thing is that you have control over them.