Introduction
At the core of pretty checkbox are three input controls:
- Checkbox
- Radio
- Switch
Concepts
Each input control uses CSS to mask, or hide, the actual input element. This means there are no real accessibility concerns when using using pretty checkbox to beautify ๐
our input controls.
Base div
Each control is wrapped by a div that contains a class attribute with pretty:
To astute reader might notice p-default. This influences many things such as animations, colors, and possible styles that can be applied. We'll learn more about this later ๐
Adding the input
Remember that pretty checkbox styles our input control, but we're still on the hook for adding it as a child of our base div:
input is complete hidden to the user via CSS. Inspecting the element we'll see the following styles applied:
New to CSS? No worries. This effectively hides the input from the users by setting the opacity to 0. What's nice here is we're not blocking functionality; our input still behaves like a checkbox.
Tieing it All Together
The last stretch is adding an inner div with a class name of state. This contains the label used to render alongside the input control:
Danger, Will Robinson!
Omitting the label prevents our pretty input from rendering completely. If you don't need a label add it to the DOM anyway without any content.
Our label element is important. There's a lot of CSS work happening behind the scenes with this little element. By using a combination of :before to create the box before the label and :after to create the interior of the box, we create our pretty checkbox ๐ฒ
Accessibility
For the most part, pretty checkbox is accessible; however, it's always recommended to add for and id attributes:
Basic Usage
Checkbox
A basic example of checkbox uses the input element with type="checkbox":
Radio
A basic example of radio uses the input element with type="radio" and to make our control round we can use the p-round selector:
Switch
Switch can be used as either a radio or a checkbox โ you can decide by changing the type attribute value from checkbox to radio (or vice versa). In the root div we need to replace p-default with p-switch to active switch styles: