HTML & CSS References

HTML Forms

An HTML Form is used to collect input from a user. The information collected can then be sent to a server, or accessed by a Developer, or more.

HTML Form Elements

The <form> element is used to create an HTML form for user input. Within the <form> tags, we can nest different types of input elements like text fields, checkboxes, radio buttons, etc. 

The <input> element is the most used form element. The reason for that is because the <input> element can be displayed in many different ways, depending on the type attribute.

Code Example
<!-- Display a single-line text input field -->
<input type=“text”>

<!-- Display a checkbox (for selecting zero or more of many choices) -->
<input type=“checkbox”>

<!-- Display a radio button (for selecting one of many choices) -->
<input type=“radio”>

The <label> element is used to define a label for a given input field. We use specific attributes in order to connect the label to an input field. 

  • There will be an id attribute on the input
  • There will be a for attribute on the label, with the same value as the id attribute on the input

This will connect the label and the input together, allowing better accessibility and use for our users.


HTML Form Attributes

The HTML <form> tag has specific attributes that define the behavior of the form itself.

The action attribute on the form defines what happens when the form is submitted. 

The method attribute defines which HTTP method to use when submitting the form data

The target attribute on the form specifies where to display the form response.


HTML Form Validation

HTML form validation is used to validate form data before the data is forwarded or  saved in any format. Automatic validation can be as easy as adding a required attribute to any given form input, requiring the input to be filled before submission. Unfortunately, with just HTML there is no real way to validate the data itself, just that it exists. 

In order to validate the data to ensure that the user input is clean, correct and useful, we need to employ the use of JavaScript.

Learn HTML & CSS Today

Get hands-on experience writing real code while you learn with interactive tutorials in our free online learning platform – no downloads or complicated setup required.

  • Free and fun
  • Designed for beginners
  • No download or setup required