How to make a search bar in HTML
The search input and its label, a search box with a button, a search bar that searches your site through Google, a filter that hides list items as you type, and fixes for a search bar that does nothing.
Have the file already? Upload it and get a link anyone can open. Free plan, no card.
Get a link freeA search bar is a text box with a button. HTML draws the box in one line; the part people get stuck on is what happens when someone types and presses Enter. A plain HTML page has no search engine behind it, so you pick one of two jobs: send the search to Google for your site, or filter what is already on the page.
The search box
<label for="q">Search</label> <input type="search" id="q" name="q" placeholder="Search recipes">
type="search" gives a text box that phones open with a Search key on the keyboard, and Chrome and Safari add a small ร to clear it. The label tells screen readers what the box is for. To hide the visible word and keep it for screen readers, drop the label and add aria-label="Search" to the input instead.
A search bar with a button
Put the input and a button in a form, then line them up with CSS in a <style> block in the <head>:
<form class="search" role="search"> <input type="search" name="q" aria-label="Search" placeholder="Search"> <button type="submit">Search</button> </form>
.search { display: flex; max-width: 420px; } .search input { flex: 1; padding: 10px 14px; border: 1px solid #c9ced6; border-radius: 8px 0 0 8px; font-size: 16px; } .search button { padding: 10px 16px; border: 0; border-radius: 0 8px 8px 0; background: #1f2430; color: #fff; font-size: 16px; cursor: pointer; }
display: flex keeps the button on the same line and flex: 1 lets the box take the rest of the width. Keep font-size at 16px or more on the input: iPhones zoom the page in when you tap a smaller text box. For a magnifying glass instead of the word, put ๐ inside the button and keep aria-label="Search" on it.
Search your site through Google
Point the form at Google and add your domain in a hidden field:
<form action="https://www.google.com/search" method="get" role="search"> <input type="search" name="q" aria-label="Search this site"> <input type="hidden" name="as_sitesearch" value="example.com"> <button type="submit">Search</button> </form>
Pressing Enter opens Google with results from example.com only. The input must be named q, because that is the field Google reads. Two limits: Google only returns pages it has already indexed, which can take days for a new site, and visitors leave your page for Google's results page.
Filter a list on the page
For a menu, a price list or a page of links, a filter works better: matching items stay and the rest hide as you type, with nothing sent anywhere.
<input type="search" id="filter" aria-label="Filter the list" placeholder="Type to filter"> <ul id="items"> <li>Banana bread</li> <li>Carrot cake</li> <li>Lemon tart</li> </ul>
<script> document.getElementById("filter").addEventListener("input", (e) => { const term = e.target.value.toLowerCase(); document.querySelectorAll("#items li").forEach((li) => { li.hidden = !li.textContent.toLowerCase().includes(term); }); }); </script>
Put the script just before </body>, after the list, so the list exists when the script runs. To filter table rows instead, change "#items li" to "#items tr" and put id="items" on the <tbody>. AI tools such as Claude and ChatGPT will write a longer version that also highlights matches or shows a "no results" line if you ask.
When the search bar does nothing
- โPressing Enter does nothing: the input is not inside a <form>, or the form has no action. Add one, or use the filter above, which reacts to typing instead of Enter.
- โGoogle opens but searches the whole web: the hidden field is misspelled or the domain includes https://. Use as_sitesearch with the bare domain, such as example.com.
- โThe filter hides everything: the selector does not match your markup. Check that "#items li" matches the id on your list.
- โThe filter works on your computer but not on the shared page: the script sits above the list, or it was saved in a separate .js file that was not uploaded. Move it below the list, inside the same HTML file.
- โThe button sits under the box on phones: the form has no display: flex, or the input has a fixed width wider than the screen. Use flex: 1 instead of a width.
If the page is private
Google site search only finds pages Google has indexed. A preview or client draft that you keep out of search results will never show up there, so use the on-page filter for it. More on keeping a page out of Google: share a preview without Google finding it.
Share the page
Open the file in a browser, type a few searches, and check it on a narrow window. Then share it: with FileShare Pro you upload the HTML file from the dashboard, copy the link and send it. It opens in any browser and the person opening it needs no account. Upload a new version over the old one and the link stays the same. Sign up free to share your first page.
Frequently asked
Use <input type="search" name="q"> inside a <form> with a <button type="submit">. Style the form with display: flex so the button sits beside the box.
Set the form action to https://www.google.com/search, name the input q, and add <input type="hidden" name="as_sitesearch" value="yourdomain.com">. It only finds pages Google has already indexed.
Yes. A form that sends the search to Google needs no JavaScript. Filtering items on the same page as you type does need a short script.
Safari on iPhone zooms in on text boxes with a font size under 16px. Set font-size: 16px or larger on the input.
Drop the file, copy the link โ live in seconds, free plan included.
Start sharing free