A site search form needs a clear label, a search field, and a destination that can handle the query. The search element marks the purpose of the area.
What You Need to Know
The name attribute controls the submitted query key. The form alone does not produce search results; a server or client script must implement them.
The goal is not to collect as many tags as possible. Pick the element that best describes what the content really is.
Example: See It in Action
Here is a simple example to experiment with. Type it, save the file, and check what the browser displays.
This is a short HTML fragment. To try it as a webpage, put it inside the body element of a complete HTML document.
<search>
<form action="/search" method="get">
<label for="site-search">Find an article</label>
<input type="search" id="site-search" name="q" required>
<button type="submit">Search</button>
</form>
</search>
Let’s Understand the Example
- Search marks an area used for searching or filtering, making its purpose clearer to assistive technology.
- Form groups controls that send named values to an endpoint when submitted.
- Label tells people what a control is for. For connects the label to the control with the matching id.
- Action names the destination where the browser sends a form submission. A real server handler must exist there.
- Method controls how a form submits; GET makes values part of the URL, while POST sends them in the request body.
- For links a label to the input whose id matches, so clicking the label can focus the control.
The browser can submit values with keys such as q. A control without a name normally contributes no entry. Whether the submission produces a result depends on the action URL and the server behind it.
What Should You See?
The page should include “Find an article”. If it looks different from what you expected, open developer tools and compare the source with the elements the browser created.
Try removing one piece of markup in a scratch copy, refresh the page, and compare the behavior. This is often the fastest way to understand what that piece contributes.
Try It Yourself
- Type a sample search and predict the resulting GET URL.
- Save your changes, open or refresh the page, and compare the result with the original example.
- Tell a friend what your change did. If you are not sure, undo it, refresh the page, and compare.
Common Beginner Mistakes
- Forgetting name="q" and getting no query value in the request.
- Avoid picking tags just for their default bold, italic or spaced appearance. Use CSS for presentation.
Key Takeaways
- A site search form needs a clear label, a search field, and a destination that can handle the query.
- Search marks an area used for searching or filtering, making its purpose clearer to assistive technology.
- The name attribute controls the submitted query key. The form alone does not produce search results; a server or client script must implement them.