--- meta: title: Checkbox description: Checkboxes allow the user to toggle an option on or off. layout: component --- ```html:preview Checkbox ``` ```jsx:react import SlCheckbox from '@shoelace-style/shoelace/dist/react/checkbox'; const App = () => Checkbox; ``` :::tip This component works with standard `
` elements. Please refer to the section on [form controls](/getting-started/form-controls) to learn more about form submission and client-side validation. ::: ## Examples ### Checked Use the `checked` attribute to activate the checkbox. ```html:preview Checked ``` ```jsx:react import SlCheckbox from '@shoelace-style/shoelace/dist/react/checkbox'; const App = () => Checked; ``` ### Indeterminate Use the `indeterminate` attribute to make the checkbox indeterminate. ```html:preview Indeterminate ``` ```jsx:react import SlCheckbox from '@shoelace-style/shoelace/dist/react/checkbox'; const App = () => Indeterminate; ``` ### Disabled Use the `disabled` attribute to disable the checkbox. ```html:preview Disabled ``` ```jsx:react import SlCheckbox from '@shoelace-style/shoelace/dist/react/checkbox'; const App = () => Disabled; ``` ### Sizes Use the `size` attribute to change a checkbox's size. ```html:preview Small
Medium
Large ``` ```jsx:react import SlCheckbox from '@shoelace-style/shoelace/dist/react/checkbox'; const App = () => ( <> Small
Medium
Large ); ``` ### Custom Validity Use the `setCustomValidity()` method to set a custom validation message. This will prevent the form from submitting and make the browser display the error message you provide. To clear the error, call this function with an empty string. ```html:preview Check me
Submit
``` {% raw %} ```jsx:react import { useEffect, useRef } from 'react'; import SlButton from '@shoelace-style/shoelace/dist/react/button'; import SlCheckbox from '@shoelace-style/shoelace/dist/react/checkbox'; const App = () => { const checkbox = useRef(null); const errorMessage = `Don't forget to check me!`; function handleChange() { checkbox.current.setCustomValidity(checkbox.current.checked ? '' : errorMessage); } function handleSubmit(event) { event.preventDefault(); alert('All fields are valid!'); } useEffect(() => { checkbox.current.setCustomValidity(errorMessage); }, []); return (
Check me
Submit
); }; ``` {% endraw %}