Checkboxes are a fundamental element for web form, they are often needed to allow users to manifest their agreement to terms & conditions of services, making them a mandatory element to allow a registration or a purchase.
The HTML <input type="checkbox">
element belongs to the form
family and it has the advantage/disadvantage of being styled and managed differently based on the operative system and browser where it’s being displayed, this can result in a series of difficulties of changing its visual appearance via CSS, often leading to irregular results.
The fact that the element takes the appearance similar to other elements used by the visitor’s system is an advantage because it will look like a familiar element, already known, with a predictable behavior, but on mobile CSS changes may be required in order to make the element more easy to be selected.
Problems arise when hyper text links are nearby the checkbox, in this case the approximation system of the touch screen will often prefer the link instead of the checkbox, with the side-effect of making extremely difficult for the user to tick the item, and unfortunately the padding
property will not increase the invisible area around the checkbox.
The label tag and the for attribute
The best way to increase the interactive area around the checkbox is to use the label
tag, adding the for
attribute equal to the ID
attribute of the checkbox, which may be nested inside it or visually overlapped. This approach allows to style freely the label
area, with the advantage that wherever the user will interact inside it the checkbox will respond too.
CSS pseudo content
To make the selection even easier it might be preferable to put all the relevant content inside the label
, this way the user can interact anywhere on the message and the checkbox will respond, but in this case it will not be possible to style the area independently, but the same effect can be achieved with a CSS pseudo element :before
.
Despite the fact of being “pseudo” fake content, it is still regarded as content of the label
element, and as such it will trigger the checkbox change via the for
attribute when selected.