Menu
Coddy logo textTech

Checkboxes

Part of the Fundamentals section of Coddy's HTML journey — lesson 42 of 60.

In HTML, checkboxes are used to create a set of options where the user can select multiple options from the group. Checkboxes are created using the <input> tag with the type attribute set to "checkbox".

Here's the basic syntax for creating a checkbox in HTML:

<input type="checkbox" id="option1" name="checkboxgroup" value="option1">
<label for="option1">Option 1</label>
  • <input>: The tag that defines the input field.
  • type="checkbox": An attribute that specifies the input type as a checkbox.
  • id: An attribute that gives the checkbox a unique identifier.
  • name: An attribute that groups checkboxes together. Unlike radio buttons, multiple checkboxes with the same name can be selected at the same time.
  • value: An attribute that specifies the value associated with the checkbox. This value is sent to the server when the form is submitted.
  • <label>: A tag that provides a clickable label for the checkbox. The for attribute of the label should match the id attribute of the checkbox.

Here's an example of how to create a group of checkboxes:

<form>
	<input type="checkbox" id="option1" name="mygroup" value="option1">
	<label for="option1">Option 1</label><br>
	
	<input type="checkbox" id="option2" name="mygroup" value="option2">
	<label for="option2">Option 2</label><br>
	
	<input type="checkbox" id="option3" name="mygroup" value="option3">
	<label for="option3">Option 3</label><br>
</form>

In this example, we have a form with three checkboxes, all belonging to the same group because they share the same name attribute ("mygroup"). Each checkbox has a unique id and value, and a corresponding label. When the user selects one or more of these options and submits the form, the values of the selected checkboxes will be sent to the server.

challenge icon

Challenge

Easy

Create an HTML document that includes a form with a group of checkboxes. The form should have the following elements:

  1. A <form> tag.
  2. Inside the form, create a group of three checkboxes with the name "toppings".
  3. The first checkbox should have the id "cheese", the value "cheese", and a label with the text "Cheese".
  4. The second checkbox should have the id "pepperoni", the value "pepperoni", and a label with the text "Pepperoni".
  5. The third checkbox should have the id "mushrooms", the value "mushrooms", and a label with the text "Mushrooms".
  6. Add a <br> tag after each label to create a line break.

Cheat sheet

Checkboxes allow users to select multiple options from a group. They are created using the <input> tag with type="checkbox".

Basic checkbox syntax:

<input type="checkbox" id="option1" name="checkboxgroup" value="option1">
<label for="option1">Option 1</label>

Key attributes:

  • type="checkbox": Specifies the input type as a checkbox
  • id: Unique identifier for the checkbox
  • name: Groups checkboxes together (multiple can be selected)
  • value: Value sent to server when form is submitted
  • <label for="">: Provides clickable label (for attribute matches checkbox id)

Example of multiple checkboxes:

<form>
	<input type="checkbox" id="option1" name="mygroup" value="option1">
	<label for="option1">Option 1</label><br>
	
	<input type="checkbox" id="option2" name="mygroup" value="option2">
	<label for="option2">Option 2</label><br>
	
	<input type="checkbox" id="option3" name="mygroup" value="option3">
	<label for="option3">Option 3</label><br>
</form>

Try it yourself

<!DOCTYPE html>
<html>
    <body>
        <!-- Write code here -->
    </body>
</html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals