Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style a checkbox using CSS

Tags:

html

css

checkbox

I am trying to style a checkbox using the following:

<input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" />

But the style is not applied. The checkbox still displays its default style. How do I give it the specified style?

like image 912
Salman Virk Avatar asked Nov 10 '10 19:11

Salman Virk


People also ask

How do I style a checkbox using CSS?

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. Example 1: Consider the example where HTML checkbox is styled using CSS.

How do you color a checkbox in CSS?

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

How do you change the shape of a check box?

To change size, color, or border style of the check box, select the Use a style to format text typed into the empty control box, and then click New Style. Under Formatting, select a font size for the check box. In the Color list, select a color. To select a different border, select Format > Border.


1 Answers

UPDATE:

The below answer references the state of things before widespread availability of CSS 3. In modern browsers (including Internet Explorer 9 and later) it is more straightforward to create checkbox replacements with your preferred styling, without using JavaScript.

Here are some useful links:

  • Creating Custom Form Checkboxes with Just CSS
  • Easy CSS Checkbox Generator
  • Stuff You Can Do With The Checkbox Hack
  • Implementing Custom Checkboxes and Radio Buttons with CSS3
  • How to Style a Checkbox With CSS

It is worth noting that the fundamental issue has not changed. You still can't apply styles (borders, etc.) directly to the checkbox element and have those styles affect the display of the HTML checkbox. What has changed, however, is that it's now possible to hide the actual checkbox and replace it with a styled element of your own, using nothing but CSS. In particular, because CSS now has a widely supported :checked selector, you can make your replacement correctly reflect the checked status of the box.


OLDER ANSWER

Here's a useful article about styling checkboxes. Basically, that writer found that it varies tremendously from browser to browser, and that many browsers always display the default checkbox no matter how you style it. So there really isn't an easy way.

It's not hard to imagine a workaround where you would use JavaScript to overlay an image on the checkbox and have clicks on that image cause the real checkbox to be checked. Users without JavaScript would see the default checkbox.

Edited to add: here's a nice script that does this for you; it hides the real checkbox element, replaces it with a styled span, and redirects the click events.

like image 94
18 revs, 7 users 78% Avatar answered Sep 18 '22 21:09

18 revs, 7 users 78%