A text box in HTML is created using the <input> element with the type="text" attribute. This creates a single-line input field where users can enter text.
The simplest way to create a text box is with this HTML code:
<input type="text">
This will display a basic text input field. You can add more attributes to customize it.
Add attributes to your text box to control its appearance and behavior:
name: Identifies the input field when the form is submittedvalue: Sets default text in the boxplaceholder: Shows hint text when the box is emptysize: Controls the visible width in charactersmaxlength: Limits how many characters can be enteredHere's a full example showing a text box within an HTML form:
<form action="/submit" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" placeholder="Enter your username"><br>
<input type="submit" value="Submit">
</form>
| Input Type | Description | Example |
|---|---|---|
| text | Single-line text input | <input type="text"> |
| password | Single-line masked input | <input type="password"> |
| textarea | Multi-line text input | <textarea></textarea> |
When creating text boxes, avoid these common errors:
type="text" attribute (it defaults to "text" but being explicit is good practice)label element for accessibilitymaxlength lower than needed for the expected inputvalue and placeholder attributes together (they serve different purposes)According to the MDN Web Docs, the text input type is one of the most commonly used form elements. It's essential for creating user registration forms, search boxes, and other interactive web elements.