Checkbox in HTML is used when a user can turn an option on or off, or when the user can select multiple independent choices from a group. It is created with <input type="checkbox">. A checkbox is common in signup forms, preference panels, filters, settings pages, permission screens, terms acceptance, newsletter forms, and any form where more than one option may be true at the same time.
The most important thing to understand is that a checkbox is different from a radio button. A radio button is for one choice from a group. A checkbox is for an independent yes/no choice or multiple selections. If the user can choose HTML, CSS, and JavaScript together, use checkboxes. If the user must choose only one plan, one payment method, or one answer, use radio buttons.
Basic Syntax of Checkbox in HTML
A checkbox uses the input tag with type="checkbox". It should have a name, value, id, and connected label. The label makes the option easier to click and improves accessibility. The value is the data submitted when the checkbox is checked.
<input type="checkbox" id="subscribe" name="subscribe" value="yes">
<label for="subscribe">Subscribe to newsletter</label>
If this checkbox is checked and the form is submitted, the browser sends subscribe=yes. If the checkbox is not checked, the browser normally sends nothing for that checkbox. This behavior is important because unchecked checkboxes do not submit a false value automatically.
Checked Checkbox by Default
The checked attribute makes a checkbox selected when the page loads. This is useful for default preferences, but it should be used carefully. Do not pre-check sensitive options such as marketing consent, paid add-ons, or agreement fields unless the user clearly expects it.
<input type="checkbox" id="dark-mode" name="dark_mode" value="on" checked>
<label for="dark-mode">Enable dark mode</label>
In this example, the checkbox starts as selected. The user can still click it to turn the option off. For account settings and preferences, checked can represent an existing saved value when the user is editing a form.
Multiple Checkboxes with Same Name
Checkboxes become powerful when the user can select multiple values from a list. For example, a skills form may allow the user to select HTML, CSS, JavaScript, React, and Python together. These checkboxes often share the same name, but each checkbox has a different value.
<fieldset>
<legend>Select your skills</legend>
<input type="checkbox" id="skill-html" name="skills" value="html">
<label for="skill-html">HTML</label>
<input type="checkbox" id="skill-css" name="skills" value="css">
<label for="skill-css">CSS</label>
<input type="checkbox" id="skill-js" name="skills" value="javascript">
<label for="skill-js">JavaScript</label>
</fieldset>
If the user selects HTML and JavaScript, the form sends two values for the same name. Different backend languages handle this in slightly different ways. Some frameworks collect them as an array, while basic server code may need special handling. The idea is the same: one group name can represent multiple selected values.
Checkbox Attributes
| Attribute | Purpose | Example |
|---|---|---|
| type | Creates checkbox behavior | type=”checkbox” |
| name | Submitted key for form data | name=”skills” |
| value | Submitted value when checked | value=”html” |
| checked | Selects checkbox by default | checked |
| required | Requires the checkbox before submit | required |
| disabled | Makes checkbox unavailable | disabled |
| id | Connects checkbox to label | id=”agree” |
The name and value attributes matter most for form submission. The id matters for labels and JavaScript. The checked attribute controls default state. The required attribute is useful for agreement checkboxes such as accepting terms and conditions.
Required Checkbox for Agreement
A common use of checkbox is agreement confirmation. The user must check the box before submitting the form. This is normally done with the required attribute. The label should be clear and should not hide important legal or payment details.
<form action="/signup" method="post">
<input type="checkbox" id="terms" name="terms" value="accepted" required>
<label for="terms">I accept the terms and conditions</label>
<button type="submit">Create Account</button>
</form>
When this form is submitted, the browser checks whether the terms checkbox is selected. If not, it blocks the submission and asks the user to complete the field. This improves user experience, but the server must still check the value because browser validation can be bypassed.
Checkbox vs Radio Button
| Feature | Checkbox | Radio Button |
|---|---|---|
| Selection rule | Zero, one, or multiple choices | One choice from a group |
| Best for | Skills, filters, preferences | Plan, payment method, answer |
| Unchecked state | Usually submits nothing | Group submits selected value |
| Independent option | Yes | No, grouped by name |
| Example | Select topics of interest | Choose account type |
Use checkbox when every option can be independently true or false. Use radio buttons when the options compete with each other and only one can be true. This distinction keeps forms logical and prevents confusing data.
Reading Checkbox Value with JavaScript
JavaScript can read whether a checkbox is selected using the checked property. For a group of checkboxes, JavaScript can collect all checked values with a selector.
const agree = document.querySelector("#terms");
if (agree.checked) {
console.log("Terms accepted");
}
const selectedSkills = Array.from(
document.querySelectorAll('input[name="skills"]:checked')
).map(input => input.value);
Use checked when you need true or false state. Use value when you need the submitted data value. Beginners sometimes read the value property and expect it to say whether the checkbox is selected, but value only stores the configured value.
Styling Checkboxes
Native checkboxes are styled by the browser and operating system. You can still control spacing, label layout, grouping, and focus styles with CSS. If you create custom checkboxes, preserve keyboard access, focus visibility, and screen reader behavior. A beautiful checkbox that cannot be used with a keyboard is a broken checkbox.
.checkbox-option {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
}
Common Checkbox Mistakes
- Forgetting that unchecked checkboxes usually submit nothing.
- Using checkbox when only one option should be selected.
- Forgetting the value attribute, which makes submitted data unclear.
- Using the same id for multiple checkboxes.
- Not connecting labels, which makes checkboxes harder to click.
- Trusting required validation without backend validation.
Checkboxes are simple, but their form submission behavior matters. Use them for independent choices, give every option a clear label, set meaningful values, group related options with fieldset and legend when needed, and always validate important values on the server.
Checkboxes in Filters and Settings
Checkboxes are heavily used in filter interfaces because filters are often independent. A product page may allow users to select several brands, several sizes, and several price-related options together. A tutorial page may allow users to filter by HTML, CSS, JavaScript, Python, and C at the same time. In these situations, a checkbox group is more natural than a select dropdown because users can see all active choices immediately.
Settings pages also use checkboxes for preferences such as email notifications, SMS alerts, compact mode, auto-save, weekly reports, or showing advanced options. Each preference is usually a separate true or false value. If the user can turn one setting on without affecting another setting, checkbox is the correct control.
Handling Unchecked Values on the Server
Since unchecked checkboxes normally submit nothing, the backend must handle missing values correctly. If a settings form has newsletter unchecked, the request may not contain newsletter at all. The server should treat that missing key as false instead of assuming the form is broken. Some developers add a hidden input before the checkbox to submit a default value, but this must be done carefully so the backend reads the final intended value.
<input type="hidden" name="newsletter" value="no">
<input type="checkbox" name="newsletter" value="yes">
With this pattern, the form always submits a newsletter value. If the checkbox is unchecked, only no is submitted. If it is checked, yes is also submitted. Different backend frameworks handle duplicate names differently, so test this pattern before relying on it in production.
Accessible Checkbox Groups
For a single checkbox, a clear label is usually enough. For several related checkboxes, use fieldset and legend so the user understands the group question. This is especially important for screen reader users because labels like HTML, CSS, and JavaScript need context. The legend can say Select your skills or Choose topics you want to follow.
Checkbox State in Edit Forms
Checkboxes are also common in edit forms where existing settings are loaded from a database. If the saved value is true, render the checkbox with the checked attribute. If the saved value is false, render it unchecked. This makes the form show the current state before the user changes anything.
For example, a profile settings page may show Email Notifications already checked because the user enabled it earlier. When the user submits the form again, the backend updates the stored preference based on whether the checkbox value is present or missing. This is why backend handling should be designed around checkbox behavior from the start.
For admin panels, this behavior is especially important. Permission checkboxes, feature toggles, and visibility settings should always show the saved state clearly before the user edits them. A mismatch between saved data and checked state can cause serious configuration mistakes.
Clear labels and saved-state accuracy make checkbox-based settings safer to review before clicking Save.
This is a small detail, but it prevents avoidable user mistakes.
Does an unchecked checkbox submit a value?
Usually no. If a checkbox is unchecked, the browser normally does not submit that checkbox name at all.
Can multiple checkboxes have the same name?
Yes. This is common when multiple selected values belong to the same field, such as selected skills or selected filters.
Continue learning HTML in order
Follow the topic sequence with the previous and next lesson.