Web Design Tutorials

How to Add a Search Box to Your Website

Written by Muhammad Naeem17 min readUpdated for 2026
A laptop showing the Google search box with suggestions as you type
A laptop showing the Google search box with suggestions as you type. Stock photo by Lisa Fotios on Pexels (photo 16368540, free to use under the Pexels licence).

Visitors who cannot find what they need leave. They do not browse your menu a second time, and they do not scroll through every page hoping the right one appears. They either search, or they go back to Google and find someone else. A search box is the shortcut that keeps them on your site — one small field, usually in the header, that lets anyone type a word and jump straight to the page, product, or article they want.

Adding a search box is not a big project. On a small site it can be a short form and a little JavaScript. On a larger site it can be a hosted search service that handles the indexing for you. This guide walks through all three routes step by step, shows you how to style the box so it fits your design, how to add live suggestions, and how to fix the problems that leave visitors staring at an empty results page.

Why a Search Box Is Worth Adding

A clear menu works well when a site has ten pages. Once it has fifty pages, a blog, a shop, or a help section, the menu alone is not enough. Visitors arrive with a specific thing in mind — a price, a model name, a service in their city — and the fastest way to serve them is to let them ask for it directly. That is what a search box does, and it pays for itself in three ways.

  • Visitors find things faster. Someone looking for a single product or article does not have to guess which menu item hides it. They type, they click, they are there. Fewer clicks means fewer people giving up halfway.
  • You learn what people actually want. Search logs show the exact words visitors type. If twenty people search for a service you offer but never mention by name, you have just found a page worth writing — and a menu label worth fixing.
  • Support questions drop. Many of the messages a small business receives are people asking where something is. A working search box answers those questions before they become an email or a WhatsApp message.

Search also helps on mobile, where menus are hidden behind a button and scrolling through long pages is slow. A search icon in the header is one tap away from any page on the site, which is exactly what a visitor on a phone needs.

Before You Start: Decide What the Search Should Cover

Before writing any code, decide what the search box should search. This one decision shapes everything that follows, and it is the step most people skip — then wonder why the results feel wrong.

  • Your whole site, or one section? A blog search should search articles. A shop search should search products first, then pages. A support site might search help articles only. Searching everything sounds generous, but on a mixed site it buries the result the visitor actually wants under pages they will never click.
  • Titles only, or full page text? Searching titles is fast and the results are usually accurate. Searching full text catches more, but it also returns pages where the word appears once in a passing sentence. For most small sites, titles plus a short description is the right balance.
  • What happens when there are no results? Plan this now, not later. A good empty state says so plainly, suggests the closest pages, and offers a way to contact you. A blank page with the words “no results” is a dead end.
  • How many pages are we talking about? A site with thirty pages can search entirely in the browser. A site with three thousand pages needs a real search index, which means a hosted service or a server-side solution. Pick the method that fits your size — the three methods below are ordered from smallest to largest.

Method 1: A Simple HTML Search Form

The simplest search box is an HTML form. It is a text field and a button, and when the visitor submits it, the browser sends their words to a results page. The form itself does no searching — something has to receive the query and return results. That something can be your own results page, your platform's built-in search, or even Google restricted to your site. The form is the same in every case:

<form action="/search" method="get" role="search">
  <label for="site-search" class="visually-hidden">Search this website</label>
  <input type="search" id="site-search" name="q"
    placeholder="Search this website..." aria-label="Search this website">
  <button type="submit" aria-label="Search">
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
      <circle cx="11" cy="11" r="8"></circle>
      <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
    </svg>
  </button>
</form>

A few details in that snippet matter. The type="search" input gives mobile visitors a search keyboard and a clear button. The name="q" attribute is what puts the query in the address bar as ?q=your+words, so results pages can be shared and bookmarked. The role="search" and the label tell screen readers what the form is for — search is one of the most-used features by visitors who rely on assistive technology, so do not leave it unlabelled.

If your platform already has a search results page — WordPress, Shopify, Wix, and most builders do — point the form's action at it and you are done. WordPress uses /?s=, Shopify uses /search?q=, and your platform's documentation will state its own address. If you have no results page at all, this form alone will not work, and you need one of the next two methods.

Method 2: Client-Side Search with JavaScript

If your site is small and static — a business site, a portfolio, a documentation site with a few dozen pages — you can search entirely in the browser. The idea is simple: keep a small list of your pages (title, address, and a one-line description) in a JavaScript file, and filter that list as the visitor types. Nothing is sent to a server, so results appear instantly, and there is no service to pay for or keep running.

Start with the index. It is just an array of the pages you want searchable:

const siteIndex = [
  { title: "Web Design Services", url: "/services/web-design",
    text: "Custom business websites built with React and Next.js" },
  { title: "How to Add a Contact Form", url: "/blog/how-to-add-contact-form-website",
    text: "Add a working contact form to your website step by step" },
  { title: "SEO Services", url: "/services/seo-services",
    text: "On-page SEO, local SEO, and technical audits for small businesses" }
];

Then filter it on every keystroke and show the matches under the box:

const input = document.getElementById("site-search");
const results = document.getElementById("search-results");

input.addEventListener("input", () => {
  const query = input.value.trim().toLowerCase();
  if (query.length < 2) { results.innerHTML = ""; return; }
  const matches = siteIndex.filter((page) =>
    (page.title + " " + page.text).toLowerCase().includes(query)
  ).slice(0, 8);
  results.innerHTML = matches.length
    ? matches.map((page) =>
        `<li><a href="${page.url}">${page.title}</a></li>`).join("")
    : "<li>No pages matched. Try a different word, or contact us.</li>";
});

This method has honest limits, and you should know them before choosing it. The index is manual, so a page you forget to add is a page that can never be found — rebuild the list whenever you publish. It searches titles and descriptions only, not full page text. And it stops being practical somewhere around a few hundred pages, when the index file itself becomes heavy to download. Inside those limits, though, it is fast, free, private, and completely under your control.

Method 3: A Hosted Search Service

When your site is large, changes often, or needs features like typo tolerance and filters, a hosted search service does the heavy work. You connect it to your site, it crawls your pages and builds the index on its own servers, and you paste a snippet into your pages to show the box and the results. You never maintain an index by hand, and new pages appear in search automatically after the next crawl.

  • Google Programmable Search Engine. Google's own site search. You define which sites it searches, Google indexes them, and you paste the provided snippet where the box should appear. There is a free option that shows ads alongside results, and a paid option that removes them. It is the quickest route if you are happy with Google's ranking and styling options.
  • Algolia and similar dedicated search services. Built specifically for site search, with typo tolerance, instant results as you type, and filters by category or price. They suit shops and large content sites where search quality directly affects sales. Most offer a free tier for small sites, with paid plans as traffic grows.
  • Your platform's own search. Shopify, WordPress with a search plugin, and many builders already include search — sometimes basic, sometimes with filters and suggestions. Check what your platform already offers before paying for an outside service; the built-in option is often enough.

The setup pattern is the same for all of them: create the search engine in the service's dashboard, tell it which pages to index, copy the snippet it gives you, and paste it into your site's header or a search page. Then test it with real words your visitors would use — including misspellings — because a service that only finds exact matches is barely better than no search at all.

Styling the Search Box to Match Your Website

A search box that looks pasted-on gets ignored. One that matches your design gets used. The box should feel like part of the header, not an afterthought, and the details below make the difference between the two.

  • Placement. The top-right of the header on desktop is where visitors look first — it is the convention on most of the web. On mobile, a magnifying-glass icon that expands into a full-width field works better than a shrunken box nobody can tap accurately.
  • Size and touch targets. Make the field at least 44 pixels tall so it is easy to tap on a phone, and wide enough for a real query — around 200 to 280 pixels on desktop. A box that fits only three letters tells visitors you did not expect them to search.
  • Placeholder text that helps. “Search...” says nothing. “Search services, guides, and prices...” tells the visitor what they can find. Write the placeholder for your own site, not a generic one.
  • A visible focus state. When the field is active, show a clear outline or border colour change. Keyboard users navigate by the focus ring, and a box with no visible focus is unusable for them.
  • Match your fonts and radius. Use the same font, the same border radius, and the same button style as the rest of your site. If your buttons are rounded and indigo, a square grey search button looks like it belongs to a different website.

Keep the styling in your stylesheet rather than inline, so the box looks the same on every page. A search box that changes shape from one page to the next feels broken even when it works.

Adding Live Suggestions as Visitors Type

Live suggestions — the small list that appears under the box while you type — are the single biggest upgrade you can make to a search box. They guide visitors toward pages that exist, catch typos before they become empty results, and make the site feel fast and considered. Most hosted services include them; with client-side search you can add them with the same filter function from Method 2.

Three rules keep suggestions helpful instead of annoying. First, wait until the visitor has typed at least two or three characters — suggesting after one letter produces a list of everything. Second, limit the list to five to eight items; a dropdown longer than the screen is worse than none. Third, make the suggestions work with the keyboard: arrow keys to move, Enter to go, Escape to close. Many visitors navigate by keyboard, and a suggestions list that only works with a mouse locks them out.

If your suggestions come from a server, add a short delay — around 200 to 300 milliseconds after the visitor stops typing — before sending the request. Without that pause, every keystroke fires a request, which is slow for the visitor and wasteful for your server. This technique is called debouncing, and every search library supports it with a few lines of code.

Search on WordPress, Wix, and Other Builders

If your site runs on a builder or a content platform, you may already have search and only need to switch it on. WordPress includes a search block you can add to a header or a sidebar, and its built-in search covers posts and pages; search plugins add filters, suggestions, and the ability to search products or custom content. Wix, Squarespace, and Shopify all include a search field you can enable from the editor and place in the header — Shopify's search also covers products, collections, and blog articles by default.

The trade-off is control. Built-in search is quick to enable but ranks results by its own rules, and its styling options are limited to what the platform allows. If the built-in results feel wrong — the wrong pages on top, products buried under blog posts — that is the point where a hosted service or a search plugin earns its place. Start with what you have, test it with real queries, and only replace it when you can name a specific problem it causes.

Common Problems and How to Fix Them

Most search boxes fail in the same handful of ways. If yours is not working, check this list before rebuilding anything:

  • The box shows, but every search returns nothing. The form is pointing at a results page that does not exist, or the query parameter name does not match what the results page expects. Check the address bar after a search: if it says ?s= but your results page reads ?q=, nothing will ever match.
  • New pages never appear in results. With client-side search, the index list was not updated when the page was published. With a hosted service, the crawler has not visited the new page yet — check the service's dashboard for the last crawl date.
  • Search finds old, deleted pages. The index is stale. Rebuild a manual index, or trigger a fresh crawl in the hosted service, and remove deleted pages from your sitemap so crawlers stop finding them.
  • Suggestions are slow or flicker. Requests are firing on every keystroke. Add the short delay described above, and cache recent queries in the browser so repeated searches answer instantly.
  • The box is unusable on mobile. It is too small, hidden behind the menu, or covered by a sticky header. Test it on a real phone: if you cannot tap it comfortably with a thumb, widen it or move it into the mobile menu.
  • Results look broken. The results page was never styled. Give it the same header, fonts, and spacing as the rest of the site, and show each result's title plus one line of description — a bare list of blue links feels like an error page.

Making Search Help Your SEO and Accessibility

A search box is a feature for visitors, but a few choices around it affect how search engines and assistive technology treat your site. First, label the field properly, as in the Method 1 example — a search box with no accessible name is invisible to screen reader users. Second, make sure the results are real links that work without JavaScript where possible, so every visitor can reach the pages search finds.

Third, keep your internal search result pages out of Google's index. A results page for a query like “price” is a thin, ever-changing page that adds nothing to search results, and a site can accumulate thousands of them. The standard advice is to block them from indexing — with a noindex tag or a robots rule — so Google spends its crawl time on your real pages instead. And finally, read your search logs once a month. The words visitors type are the clearest list you will ever get of the content your site is missing, the products people expect you to sell, and the wording your customers actually use — which is often different from the wording on your pages.

Conclusion

A search box is a small feature with an outsized effect: visitors find what they came for, you learn what they want, and fewer questions reach your inbox. Start with the method that fits your site — a simple form if your platform already searches, client-side search if your site is small and static, a hosted service if it is large or growing fast. Style the box to match your design, add live suggestions when you can, and test it with the words real visitors would type, including the misspelled ones. Then check the search logs every month and let them tell you what to write next.

Frequently Asked Questions

Do I need a database to add a search box?

No. Small sites can search entirely in the browser with a list of pages in JavaScript, and hosted services keep the index on their own servers. You only need a database-backed search if your content already lives in a database and you want to search it directly.

Can I add a search box to a static website?

Yes. Client-side search is made for static sites: a small index of your pages lives in a JavaScript file, and the browser filters it as the visitor types. It works well up to a few hundred pages, needs no server, and returns results instantly.

Will a search box slow down my website?

Barely, if it is built well. A plain form adds almost nothing. Client-side search adds one small file. Hosted services load their code separately, so the page can render first. The main risk is live suggestions firing a request on every keystroke — a short delay after typing fixes that.

Where should I put the search box on my website?

In the header, top-right on desktop, where visitors expect it. On mobile, use a magnifying-glass icon that opens a full-width field. Also consider a larger search field on the homepage and on the 404 page, where visitors have already told you they are lost.

Why does my search box return no results?

The usual causes are a form pointing at a results page that does not exist, a query parameter name that does not match (for example ?s= versus ?q=), or an index that was never rebuilt after new pages were published. Check the address bar after a search first — it shows exactly what was sent.

Is Google Programmable Search free?

Google Programmable Search Engine has a free option that shows ads alongside the results, and a paid option that removes the ads. For a small business site the free option is usually enough to start with, and you can upgrade later if the ads bother you.

Need a Search Box on Your Website?

If you want search that fits your design and actually finds your pages — from a simple form to live suggestions — I can add it for you. Message me on WhatsApp or send an email inquiry and I will take a look at your site.

WhatsApp: 03207140898Send Email Inquiry

Explore More Resources & Services:

How to Add a Sitemap to Your WebsiteWebsite Navigation Best PracticesHow to Add Live Chat to Your WebsiteHow to Add a Blog to Your WebsiteOn-Page SEO GuideHow to Use Google Search Console
Chat with Naeem