Skip to main content

HTML Input Types

This chapter describes the different types for the HTML <input> element. The type you choose changes how the field looks and behaves.

The available types are: button, checkbox, color, date, datetime-local, email, file, hidden, image, month, number, password, radio, range, reset, search, submit, tel, text, time, url, and week.

Tip: The default value of the type attribute is text.


Input Type Text

<input type="text"> makes a single-line text input field.

Example

A form with text fields for a first and last name.

Prompt
A form with two single-line text input fields (type="text") labeled "First name" and "Last name", each with a matching label and a line break.
Output: complete HTML page

Input Type Password

<input type="password"> makes a password field. The characters typed into it are masked, shown as dots or asterisks.

Example

A form with a username text field and a password field.

Prompt
A form with a text field for "Username" and a password field (type="password") for "Password", each with a label. The password characters should appear masked.
Output: complete HTML page

Input Type Submit

<input type="submit"> makes a button that sends the form data to a form-handler. The handler is usually a server page with a script, set in the form's action attribute.

If you leave out the submit button's value attribute, the button shows the browser's default text.

With a value

A submit button with the label "Submit".

Prompt
A form with First name and Last name text fields pre-filled with John and Doe, plus a submit button (type="submit") with value "Submit". Form action is /action_page.php.
Output: complete HTML page

Default button text

The same form, but the submit button has no value, so it shows the default text.

Prompt
The same first/last name form, but the submit button has no value attribute so the browser shows its default button text. Form action is /action_page.php.
Output: complete HTML page

Input Type Reset

<input type="reset"> makes a reset button that sets all form fields back to their default values.

If you change the values and then click "Reset", the form data goes back to the defaults.

Example

A form with a submit button and a reset button.

Prompt
A form with First name and Last name fields (John, Doe), a submit button, and a reset button (type="reset", value "Reset") that returns the fields to their default values. Form action is /action_page.php.
Output: complete HTML page

Input Type Radio

<input type="radio"> makes a radio button. Radio buttons let the user pick only ONE of a small set of choices.

Example

Radio buttons for choosing one favorite web language.

Prompt
A form of radio buttons (type="radio") letting the user pick one favorite web language: HTML, CSS, or JavaScript. They share the name "fav_language".
Output: complete HTML page

Input Type Checkbox

<input type="checkbox"> makes a checkbox. Checkboxes let the user pick zero or more of a small set of choices.

Example

Three checkboxes the user can tick independently.

Prompt
A form with three checkboxes (type="checkbox") the user can tick independently: "I have a bike", "I have a car", "I have a boat".
Output: complete HTML page

Input Type Button

<input type="button"> makes a clickable button.

Example

A button that shows an alert when clicked.

Prompt
A clickable button made with input type="button", value "Click Me!", that shows an alert saying "Hello World!" when clicked.
Output: complete HTML page

Input Type Color

<input type="color"> is for fields that should hold a color. Depending on the browser, a color picker may appear.

Example

A color picker field.

Prompt
A form with a color picker (input type="color") labeled "Select your favorite color:".
Output: complete HTML page

Input Type Date

<input type="date"> is for fields that should hold a date. Depending on the browser, a date picker may appear.

You can use the min and max attributes to limit which dates are allowed.

Basic date

A simple date field.

Prompt
A form with a date input (input type="date") labeled "Birthday:".
Output: complete HTML page

Date restrictions

Date fields limited with min and max.

Prompt
A form with two date inputs using min and max restrictions: one that only accepts a date before 1980-01-01 (max="1979-12-31") and one that only accepts a date after 2000-01-01 (min="2000-01-02").
Output: complete HTML page

Input Type Datetime-local

<input type="datetime-local"> is a date-and-time field with no time zone. Depending on the browser, a date picker may appear.

Example

A date-and-time field.

Prompt
A form with a date-and-time input (input type="datetime-local") labeled "Birthday (date and time):".
Output: complete HTML page

Input Type Email

<input type="email"> is for fields that should hold an email address. Depending on the browser, the address can be checked automatically when the form is submitted. Some phones add ".com" to the keyboard for email fields.

Example

An email field.

Prompt
A form with an email input (input type="email") labeled "Enter your email:".
Output: complete HTML page

Input Type Image

<input type="image"> uses an image as a submit button. The path to the image goes in the src attribute.

Example

An image acting as a submit button.

Prompt
A form with an image used as a submit button (input type="image"), src "img_submit.gif", alt "Submit", width and height 48.
Output: complete HTML page

Input Type File

<input type="file"> makes a file-select field with a "Browse" button for uploading files.

Example

A file-select field.

Prompt
A form with a file-select field (input type="file") and Browse button, labeled "Select a file:".
Output: complete HTML page

Input Type Hidden

<input type="hidden"> makes a hidden field that the user does not see. It lets developers send data with the form that the user cannot see or change, such as which database record to update.

Note: The value is hidden from the page, but it can still be seen and edited with the browser's developer tools or "View Source". Do not use hidden inputs for security.

Example

A form with a visible field plus a hidden field carrying a customer id.

Prompt
A form with a visible First name text field, a hidden input (input type="hidden") carrying a customer id value of 3487, and a submit button.
Output: complete HTML page

Input Type Month

<input type="month"> lets the user pick a month and year. Depending on the browser, a date picker may appear.

Example

A month-and-year field.

Prompt
A form with a month-and-year input (input type="month") labeled "Birthday (month and year):".
Output: complete HTML page

Input Type Number

<input type="number"> makes a numeric input field. You can limit which numbers are allowed with attributes like min and max.

Example

A numeric field that only accepts values from 1 to 5.

Prompt
A form with a numeric input (input type="number") labeled "Quantity (between 1 and 5):" that only accepts values from 1 to 5 using min and max.
Output: complete HTML page

Input Restrictions

Here are some common attributes for limiting what can be entered in an input:

  • checked — the field is pre-selected when the page loads (for checkbox or radio).
  • disabled — the field is disabled.
  • max — the maximum value allowed.
  • maxlength — the maximum number of characters.
  • min — the minimum value allowed.
  • pattern — a regular expression the value must match.
  • readonly — the field cannot be changed.
  • required — the field must be filled in.
  • size — the width of the field in characters.
  • step — the allowed number intervals.
  • value — the default value.

You will learn more about input restrictions in the next chapter.

Example

A numeric field from 0 to 100 in steps of 10, with a default value of 30.

Prompt
A form with a numeric input from 0 to 100 in steps of 10, with a default value of 30, using min, max, step, and value attributes. Label it "Quantity:".
Output: complete HTML page

Input Type Range

<input type="range"> makes a slider for a number whose exact value is not important. The default range is 0 to 100, and you can change it with min, max, and step.

Example

A slider for a volume value from 0 to 50.

Prompt
A form with a slider control (input type="range") labeled "Volume (between 0 and 50):" using min="0" and max="50".
Output: complete HTML page

Input Type Search

<input type="search"> is for search fields. A search field works like a normal text field.

Example

A search field.

Prompt
A form with a search field (input type="search") labeled "Search Google:".
Output: complete HTML page

Input Type Tel

<input type="tel"> is for fields that should hold a telephone number. You can use the pattern attribute to set the expected format.

Example

A phone-number field with a pattern.

Prompt
A form with a telephone input (input type="tel") labeled "Enter your phone number:" using the pattern [0-9]{3}-[0-9]{2}-[0-9]{3}.
Output: complete HTML page

Input Type Time

<input type="time"> lets the user pick a time (no time zone). Depending on the browser, a time picker may appear.

Example

A time field.

Prompt
A form with a time input (input type="time") labeled "Select a time:".
Output: complete HTML page

Input Type Url

<input type="url"> is for fields that should hold a web address. Depending on the browser, the value can be checked automatically when submitted. Some phones add ".com" to the keyboard for url fields.

Example

A URL field.

Prompt
A form with a URL input (input type="url") labeled "Add your homepage:".
Output: complete HTML page

Input Type Week

<input type="week"> lets the user pick a week and year. Depending on the browser, a date picker may appear.

Example

A week field.

Prompt
A form with a week input (input type="week") labeled "Select a week:".
Output: complete HTML page