Definition and Usage
The type="search" attribute of the <input> tag represents a single-line search input field.
This field is very similar to the basic single-line text field type, type="text", but differs in that it explicitly indicates that the input is intended for a search query.
On some devices with a dynamic virtual keypad (such as mobile devices), a search icon may be displayed on the Enter key.
Depending on the browser, a clear icon may also be included to quickly remove the entered text.
Basic Example
<search>
<form method="get" action="/search">
<label for="user-search">Search This Site</label>
<input
type="search"
id="user-search"
name="query">
<button type="submit">Search</button>
</form>
</search>
Using Related Attributes
Let's learn about the attributes related to <input type="search">.
placeholder Attribute
The placeholder attribute specifies the text displayed in a text input control when no value is set. This is called placeholder text.
This value should provide the user with a brief hint about the expected type of data to enter into the control.
When the user starts entering text, the placeholder text disappears from the text input field.
<search>
<form method="get" action="/search">
<label for="user-search">Search This Site</label>
<input
type="search"
id="user-search"
name="query"
placeholder="Enter a search term.">
<button type="submit">Search</button>
</form>
</search>
For information on styling placeholder text, see Styling CSS Placeholder Text.
value Attribute
The value attribute sets the initial value.
This value specifies the search keyword displayed in the element when the page loads.
<input type="search" value="search keyword">
disabled Attribute
The disabled attribute is used to disable the element. A disabled element does not function even if the user tries to enter or change its value, because its inherent functionality is disabled. In addition, its value is not submitted when the form is submitted.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<input type="search" value="search keyword" disabled>
However, JavaScript, which can set the value attribute directly, can still change the value of the control.
readonly Attribute
The readonly attribute prevents the control from being changed or edited.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
It is mainly used with the value attribute to display the value entered or stored in the control to the user while preventing it from being edited.
<input type="search" value="search keyword" readonly>
However, JavaScript, which can set the value attribute directly, can still change the value of the control.
maxlength Attribute
The maxlength attribute limits the maximum number of characters (maximum length) that the user can enter. When the maxlength attribute is specified for the element, it prevents the user from entering more characters than allowed.
- The attribute value must be a non-negative integer (0 or greater).
- If an invalid value is specified, the maximum length restriction is not applied.
- The maximum number of characters is calculated in standard character units (UTF-16 code units rather than bytes), so the same restriction applies regardless of the language, including English, numbers, and Korean. However, some emoji, such as 😀, are represented by two code units in UTF-16, so the maximum number of characters may not correspond to the number of characters perceived by the user.
Note
If a valid minlength attribute is specified for the element, the value of the maxlength attribute must be greater than or equal to that value.
<!-- Limit input to a maximum of 10 characters with maxlength="10" -->
<input type="search" maxlength="10" placeholder="10 characters max">
minlength Attribute
The minlength attribute specifies the minimum number of characters (minimum length) that the user must enter. When the minlength attribute is specified for the element, the user must enter at least the specified minimum number of characters.
Note
The minlength attribute only checks the minimum number of characters that must be entered. If the user does not enter anything in the element, an empty value is allowed. If you do not want to allow an empty value, use the required attribute as well.
Additional Explanation
The required attribute indicates that the control is required, meaning that the user must enter a value before submitting the form.
- The attribute value must be a non-negative integer (0 or greater).
- If an invalid value is specified, the minimum length restriction is not applied.
- The minimum number of characters is calculated in standard character units (UTF-16 code units rather than bytes), so the same restriction applies regardless of the language, including English, numbers, and Korean. However, some emoji, such as 😀, are represented by two code units in UTF-16, so the minimum number of characters may not correspond to the number of characters perceived by the user.
Note
If a valid maxlength attribute is specified for the element, the value of the minlength attribute must be less than or equal to that value.
<form>
<label for="search">Search Keyword</label>
<!-- Require a minimum of 2 characters with minlength="2" -->
<input type="search" id="search" minlength="2" placeholder="Enter 2+ characters..." required>
<button type="submit">Search</button>
</form>
- Check for Errors: Leave the input field empty or enter only 1 character, then click the [Submit] button. Confirm that the browser displays an error message indicating that the minimum input requirement (2 characters) has not been met.
- Check for Success: Enter 2 or more characters in the field, then click the [Submit] button again. Confirm that the form is submitted without an error.
required Attribute
The required attribute indicates that the control is required, meaning that the user must enter a value before submitting the form. It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<form>
<fieldset>
<legend>Search Keyword is required</legend>
<p>
<label for="usersearch">Search Keyword (*required)</label>
<!-- Specify as a required input field -->
<input id="usersearch" type="search" required>
</p>
<p>
<button type="submit">Search</button>
</p>
</fieldset>
</form>
- Check for Errors: Without entering anything in the Search Keyword (*required) field, click the [Submit] button. Confirm that the browser displays an error message for the required field.
- Check for Success: Enter a value in the Search Keyword (*required) field, then click the [Submit] button again. Confirm that the form is submitted without an error.
pattern Attribute
The pattern attribute specifies the regular expression used to validate the input value.
<input type="search" pattern="Specify a regular expression">
name Attribute
The name attribute specifies the name of the element.
When the name attribute is specified, its value is used as the name of the data sent when the form is submitted. In JavaScript, the value of the name attribute is also used as the name of the element in the form.elements API.
size Attribute
The size attribute specifies the width of the element based on the number of characters.
The value specified by the size attribute specifies the visual width of the input field based on the number of characters. Therefore, the actual displayed width may vary depending on the font size (font-size) that is set.
The value of the size attribute must be specified as an integer greater than 0.
If the size attribute is not specified or an invalid value is set, most browsers use 20 as the default value.
The size attribute only sets the visual width of the input field and does not affect validation for limiting the number of characters entered when the form is submitted.
<p>
<label>
No size attribute
<input type="search">
</label>
</p>
<p>
<label>
Short input
<input type="search" size="5">
</label>
</p>
<p>
<label>
Long input
<input type="search" size="50">
</label>
</p>
If you specify a width for an element with the size attribute using CSS, the CSS width takes precedence.
list Attribute
The list attribute specifies the value of the id attribute of the <datalist> tag that provides a list of predefined options for the <input type="search">.
The user can select an option from this predefined list or enter a different value. When the user clicks or double-clicks the control, the corresponding suggested values are displayed in a drop-down menu. For example, in the Chrome browser, an arrow (▼) is also displayed on the right side of the control to indicate that predefined values are available.
<label for="user-search">Search</label>
<input type="search" list="keywords" id="user-search">
<!-- Display in the drop-down menu -->
<datalist id="keywords">
<option value="Company History"></option>
<option value="Directions"></option>
<option value="Contact Information"></option>
</datalist>
Accessibility Considerations
Accessibility is quite important for search forms.
Because the purpose of a search form is generally quite obvious to sighted users due to its placement, it may seem like a common design practice not to provide a label for the search field.
However, this can cause confusion for screen reader users because they cannot audibly determine the purpose of the search input. This is especially true when the placeholder attribute is used as a substitute for the label.
Ultimately, the placeholder attribute cannot serve as a substitute for a label.
placeholder attribute cannot replace the label of the control.
<form method="get" action="/search">
<input type="search" name="query" placeholder="Enter a search term.">
<button type="submit">Search</button>
</form>
For more information, see Accessibility Considerations for the <input> Tag.
Browser Compatibility
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<input type="search">
|
5 | 12 | 4 | 10.6 |
list
|
22 | 12 | 4 | 12.1 |
Specifications
| Specification | |
|---|---|
<input type="search">
|
HTML Standard #text-(type=text)-state-and-search-state-(type=search) |