Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS -webkit-appearance: none; is causing checkbox to not be checked

Tags:

css

All, I have a checkbox that I applied the following CSS style to:

-webkit-appearance: none;

This same code is on some text fields I have and these still continue to work just fine. Why is this functionality causing the checkbox to not allowed to be checked?

I like the styling of the checkbox this way but still need the functionality to work. If I change the code to:

-webkit-appearance: checkbox;

It displays the standard checkbox. Any ideas? Here is a JS Fiddle for demonstration purposes: http://jsfiddle.net/VWC76/

like image 751
user1048676 Avatar asked Nov 18 '13 20:11

user1048676


People also ask

How do you stop a checkbox from being checked?

Clicks on either of the checkboxes will be ignored because of e. preventDefault(); Whereas, e. stopPropagation(); will prevent the bubbling up of the click event to the parent table.

How do I make checkboxes checked in CSS?

A checkbox cannot be checked in CSS, unfortunately. It relies on the checked attribute of the input element, and attributes cannot be modified via CSS. Alternatively, you could look into a JavaScript solution, but of course the best way would be to edit the HTML directly.

How do I make a checkbox not clickable CSS?

You can put a transparent div on top of the checkbox to make it un-clickable by toggling a class on the parent object.

How do you check if a checkbox is enabled or not?

prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.


2 Answers

You just nuked all styles of checkbox on WebKit, so yes you can't see whether they're checked or not (they still are, it just isn't visible to us sighted people without a screen reader).

You need to style the checked state with the :checked pseudo: http://jsfiddle.net/VWC76/450/

input[type=checkbox]:checked {
    background-color: red;
    /* or whatever styles you want depending on your design */
    /* be as obvious as possible that it's a checkbox and that it's checked! */
}

EDIT:

  • appearance:none now exists outside of WebKit/Blink (caniuse). Just use Autoprefixer if you've better to do than adding prefixes by hand :)
  • A good implementation: accessible custom checkbox and radio form controls

EDIT 2:

  • in the fiddle demo, adding focusable elements before and after checkbox to show it works with keyboard (just click on the first occurence of "Test" and then tab tab space tab). It lacks any visual cue that checkbox/label is focused which is a bad thing (it's a demo). Best seen on Chrome which is a worse thing :p (you need Autoprefixer. Try on Codepen)
like image 158
FelipeAls Avatar answered Oct 25 '22 10:10

FelipeAls


You need to add a input[type=checkbox]:checked

input[type=checkbox]:checked {
   background: #BADA55;
}

If this is what you're looking for?

like image 32
Ace Avatar answered Oct 25 '22 08:10

Ace