Skip to main content

HTML Tags

HTML <input> Tag

Written by Updated HTML <input> Tag exercises

The input tag is the workhorse of forms: text boxes, email fields, passwords, numbers, checkboxes, radio buttons, file pickers, all from one tag. The type attribute decides which, and gets you the right keyboard on phones and free validation.

It is a void element, with no closing tag. Every input type is described in its own lesson, and attributes like min, pattern and required in HTML Input Attributes.

Syntax

html

<input type="text" name="username" value="" placeholder="Enter text" required>

The most used attributes

AttributeWhat it does
typeThe kind of field: text, email, password, number, checkbox, radio, file and more.
nameThe name the value is sent under. Without it, the field is not sent.
idConnects the field to its <label for>.
valueThe starting value, or the value sent for a checkbox or radio button.
placeholderA hint shown while the field is empty.
requiredThe form cannot be sent while it is empty.
min, max, stepLimits for numbers and dates.
autocompleteHelps browsers fill the right saved data, like email or current-password.

Example

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Input Tag Example</title>
</head>
<body>
  <h2>Input Types Example</h2>
  <form action="/samples/form_processor.php" method="post">
    <label for="username">Username:</label><br>
    <input type="text" id="username" name="username" placeholder="Enter your username" required><br><br>

    <label for="email">Email:</label><br>
    <input type="email" id="email" name="email" placeholder="Enter your email"><br><br>

    <label for="age">Age:</label><br>
    <input type="number" id="age" name="age" min="1" max="100"><br><br>

    <label for="gender">Gender:</label><br>
    <input type="radio" id="male" name="gender" value="male">
    <label for="male">Male</label>
    <input type="radio" id="female" name="gender" value="female">
    <label for="female">Female</label><br><br>

    <input type="submit" value="Submit">
  </form>
</body>
</html>

The form has a required username field, an email field, an age field limited to 1 to 100, and two radio buttons for gender. Radio buttons with the same name form one group, so only one can be chosen.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Good to know

  • Every input needs a label. A placeholder is not enough, because it disappears while typing.
  • Use the most specific type: email and tel give phone users the right keyboard.
  • type="number" is for quantities. For phone numbers, PINs and postcodes, use type="text" with inputmode="numeric".

HTML <input> Tag exercises

A text field

Add a text field named username. Write its type.

Result

Check your answer to see it here.