HTML Text Input: <input type="text">

An HTML text input collects a single line of text such as a name, city, search term or short code.

<label for="topic">Topic</label>
<input type="text" id="topic" name="topic">

The name identifies the submitted field, while the id connects the input to its label and uniquely identifies the element in the page.

Use a text input for short, single-line data. For longer multi-line content, use a textarea.

Text Input Inside a Form Top ↑

<form method="post" action="target_page.php">
  <label for="topic">Topic</label>
  <input type="text" id="topic" name="topic" value="SEO basic">
  <button type="submit">Submit</button>
</form>

When this form is submitted, the control contributes a name/value pair using topic as the field name.

Text Input Attributes Top ↑

AttributePurpose
type="text"Creates a single-line text control.
nameDefines the field name used when the form is submitted.
idUniquely identifies the element and connects a label through for.
valueSets the input's initial value.
maxlengthLimits the maximum number of characters accepted by the control.
minlengthSets the minimum number of characters for browser constraint validation.
sizeProvides an approximate visible width in characters; CSS is preferred for layout.
autocomplete HTML5 featureProvides the browser with autofill guidance.
autofocus HTML5 featureRequests focus when the page loads.
placeholder HTML5 featureDisplays a short hint while the control has no value.
readonlyPrevents editing while keeping the control available for normal submission.
disabledDisables interaction and normally excludes the control from form submission.
requiredRequires a non-empty value for browser constraint validation.

name and id Top ↑

The name attribute is important when the value must be submitted:

<label for="city">City</label>
<input type="text" id="city" name="city">

A normal successful form control without a name is not included in the submitted form data.

The visible <label> improves accessibility and makes the field purpose clear. A placeholder should not replace the label.

value Attribute Top ↑

Use value to supply an initial value:

<label for="txtbox">Text</label>
<input type="text" id="txtbox" name="txtbox" value="Simple Text">
When a server-side application inserts untrusted data into an HTML value attribute, the application must HTML-escape that value before output. HTML source alone does not make dynamic values safe.

maxlength and minlength Top ↑

Use maxlength to limit the number of characters a user can enter:

<input type="text" name="username" maxlength="20">

You can also set a minimum length:

<input type="text" name="username" minlength="3" maxlength="20" required>

These browser rules improve usability, but the receiving application must still validate the value.

size and CSS Width Top ↑

The size attribute gives an approximate visible width in characters:

<input type="text" name="code" size="12">

For responsive page layout, use CSS rather than relying on size as a precise pixel width.

autocomplete Top ↑

The autocomplete attribute can tell the browser what kind of information the field expects.

<label for="full_name">Full name</label>
<input type="text" id="full_name" name="full_name" autocomplete="name">

Using a meaningful autocomplete token can improve form completion and accessibility.

autofocus Top ↑

<input type="text" name="search" autofocus>

The browser attempts to focus that control when the page loads.

Demo of autofocus

Keeping the cursor at a textbox by default

Use autofocus selectively. Automatically moving focus can be disruptive when users expect to begin at the top of a page or use assistive technology.

placeholder Top ↑

A placeholder gives a short example or hint while the input is empty:

<label for="search_term">Search</label>
<input type="text" id="search_term" name="search_term" placeholder="Enter a tutorial name">

Demo of placeholder

Placeholder text is a hint, not a field label. It disappears after typing starts and should not be the only explanation of what the user must enter.

readonly vs disabled Top ↑

readonly Top ↑

<input type="text" name="member_id" value="A102" readonly>

A readonly text input cannot be edited by the user, but its value is normally included when the form is submitted.

disabled Top ↑

<input type="text" name="member_id" value="A102" disabled>

A disabled control cannot be edited or focused in the normal way and is not normally submitted with the form.

Neither readonly nor disabled makes a value trustworthy. A server must validate any submitted value that affects application behavior.

required Top ↑

Use required when the user must provide a value before the form can pass browser constraint validation:

<label for="first_name">First name</label>
<input type="text" id="first_name" name="first_name" required>

Client-side validation can be bypassed, so required data must also be checked by the receiving application.

Complete Example Top ↑

<form method="post" action="target_page.php">
  <label for="topic2">Topic</label>
  <input
    type="text"
    id="topic2"
    name="topic"
    placeholder="Example: HTML forms"
    minlength="3"
    maxlength="80"
    required
  >

  <button type="submit">Submit</button>
</form>

Video Tutorial Top ↑

HTML input textbox and attributes such as name, value, size, maxlength and autofocus

Frequently Asked Questions Top ↑

Q1: What does input type="text" create?

It creates a single-line text field for short user-entered text.

Q2: What is the difference between name and id?

The name identifies submitted form data. The id identifies the element in the document and can connect it to a label.

Q3: Is placeholder a replacement for a label?

No. Use a visible label to identify the field and use placeholder only for a short hint or example.

Q4: What is the difference between readonly and disabled?

A readonly text input cannot be edited but is normally submitted. A disabled input is normally excluded from form submission.

Q5: Should I use size to control the input width?

Size gives an approximate width in characters. Use CSS when you need layout or responsive width control.

Q6: Does maxlength replace server-side validation?

No. Browser restrictions improve usability but the server must still validate submitted data.


HTML Form Checkbox



plus2net.com










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer