Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Checkbox

Hello i am trying to create a custom checkbox for my website like the image link provided. What would be the best way to go around doing this? Many thanks.

Custom checkboxes

like image 435
HAWKES08 Avatar asked Jun 27 '11 11:06

HAWKES08


People also ask

How do I make a check box in HTML?

The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!

How do you put a checkbox in CSS?

A checkbox is an HTML element that takes input from the user. It is possible to style a checkbox using Pseudo Elements like :before, :after, hover and :checked. To style the checkbox the user first needs to hide the default checkbox which can be done by setting the value of the visibility property to hidden.

How do I color a checkbox in CSS?

Use the accent-color property to change the checkbox color in CSS.


2 Answers

There is a CSS trick that actually works by hiding the checkbox (or radio), defining a label (which in all relevant browsers will turn the checkbox on/off) that will be the visual representation, and using the :checked and + selectors.

This is just a simple example:

.foscheck input { display: none; }
.foscheck label { display: block; width: 20px; height: 20px; background: red; }
.foscheck input:checked + label { background: blue; }
<div class="foscheck">
  <input type="checkbox" id="fos1" />
  <label for="fos1"></label>
</div>

jsFiddle Demo

Downsides: the :checked selector unfortunately does not work on IE, only from IE9. You can apply a Javascript fallback only for IE through conditional comments though.

Note: For accessibility, you should have some text describing the checkbox in the label, I just wanted to illustrate the effect.

like image 181
kapa Avatar answered Oct 12 '22 07:10

kapa


jQuery is your best bet, this is a checkbox plugin for example, but there are hundreds of them so something else may suit you better. Just google 'jquery custom checkbox'.

like image 32
Jordan Wallwork Avatar answered Oct 12 '22 05:10

Jordan Wallwork