Part of our website setup guide series

website-setup

Add Google Custom Search Bar to Website: Step-by-Step CSS Guide

Praveen 5 min read
Illustration representing search bar code integration

Why Built-in Site Search is a Developer Trap

When my team and I first started building custom blogs and portfolio sites, we spent days coding custom search bars. We set up database indexers, wired up SQL LIKE queries, and attempted to rank relevance.

But as our articles grew, this custom search approach fell apart. It was slow, consumed database resources, and didn’t support basic search requirements like typos, synonyms, or relevance ranking.

Instead of over-engineering a local search backend, my friends and I decided to delegate search indexing to the company that does it best: Google. By embedding Google Programmable Search (formerly Custom Search Engine or CSE), we gained access to Google’s ranking algorithms with minimal setup.

Here is a step-by-step developer guide on how to configure Google Programmable Search, embed it securely, and use custom vanilla CSS overrides to style it for modern dark-mode themes.


1. Creating Your Google Programmable Search Engine

Before writing code, you need to register your search engine on the Google console:

  1. Go to the Google Programmable Search Console.
  2. Log in with your Google account and click Create a new search engine.
  3. Under Name your search engine, enter a clear title (e.g., “PraveenTechWorld Search”).
  4. In the What to search? section, enter your domain name (e.g., *.praveentechworld.com to search all pages and subdomains).
  5. Ensure the Search settings are configured to search only the specified sites.
  6. Click Create to generate your engine.
  7. Note down your unique Search Engine ID (CX ID). You will need this for the embed script.

2. Embedding the Search Box into Astro/HTML

Google provides a default JavaScript snippet that renders the search input box and results automatically. However, the default snippet is synchronous and can drag down your page load speed.

To prevent crawl delay flags (which we want to avoid as detailed in our indexing diagnostic logs), load the script asynchronously.

Here is the clean Astro component format we implemented:

---
// GoogleSearch.astro
---
<div class="search-container">
  <!-- Load the Google Custom Search script asynchronously -->
  <script async src="https://cse.google.com/cse.js?cx=YOUR_CX_ID_HERE"></script>
  
  <!-- The target element where the Google search box will render -->
  <div class="gcse-search" data-resultsUrl="/search" data-newWindow="false"></div>
</div>

<style>
  .search-container {
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
    padding: 1rem;
  }
</style>

Key Settings to Know:

  • data-resultsUrl: Tells Google where to show the search results. In our setup, we point this to a dedicated /search page so that search results do not clutter our homepage.
  • data-newWindow: Set to false so search links open in the same tab, keeping users on our site.

3. Styling Google Search with Vanilla CSS (Dark Mode overrides)

By default, Google renders a search box that looks like it belongs in 2008: white backgrounds, bright blue buttons, and heavy borders. To make it match a premium, dark-mode developer site, you must override Google’s default classes.

Since Google’s script injects an iframe, we have to target the CSS selectors aggressively. Add this vanilla CSS to your global stylesheet:

/* Global Google Programmable Search Box styling overrides */

/* 1. Main container background and borders */
.gsc-control-cse {
  background-color: #0f172a !important; /* slate-900 */
  border: 1px solid #1e293b !important; /* slate-800 */
  border-radius: 8px !important;
  font-family: 'Inter', sans-serif !important;
}

/* 2. Style the input field */
.gsc-input {
  background-color: #1e293b !important; /* slate-800 */
  border: 1px solid #334155 !important;
  color: #f8fafc !important;
  border-radius: 4px !important;
  padding: 8px !important;
}

/* 3. Customize the search button */
.gsc-search-button-v2 {
  background-color: #3b82f6 !important; /* Google Blue */
  border-radius: 4px !important;
  border: none !important;
  transition: background-color 0.2s ease-in-out;
}

.gsc-search-button-v2:hover {
  background-color: #2563eb !important; /* Hover blue */
}

/* 4. Hide branding (Only allowed if compliant with terms) */
.gsc-branding {
  display: none !important;
}

/* 5. Style the results layout */
.gsc-webResult.gsc-result {
  background-color: #0f172a !important;
  border-bottom: 1px solid #1e293b !important;
  padding: 1rem 0 !important;
}

.gs-title, .gs-title * {
  color: #60a5fa !important; /* blue-400 */
  text-decoration: none !important;
}

.gs-snippet {
  color: #94a3b8 !important; /* slate-400 */
}

By adding these rules to our global stylesheet, the Google Search iframe seamlessly blends into our website’s existing design system.


4. Setting Up the Results Page

If you set data-resultsUrl="/search" in your component, you need to create a /search route in your Astro project to render the results.

Create src/pages/search.astro with the following layout:

---
import Layout from '../layouts/Layout.astro';
---
<Layout title="Search Results">
  <main class="max-w-4xl mx-auto py-12 px-4">
    <h1 class="text-3xl font-bold text-slate-100 mb-8">Search Results</h1>
    
    <!-- Render the results container -->
    <div class="gcse-searchresults-only"></div>
  </main>
</Layout>

When a user types a query in the homepage header, Google redirects them to yourdomain.com/search?q=query and renders the search result listings in the container automatically.


Summary of Benefits

For our team, embedding Google Custom Search solved three major problems:

  1. Zero Maintenance: We don’t have to manage database search tables or scale CPU capacity as our post count grows.
  2. Top-Tier Accuracy: Google automatically handles spelling corrections, synonyms, and ranks results by relevance.
  3. Speed: By loading the script asynchronously, the page load time remains light.

If you are looking for a reliable, highly accurate site search solution that doesn’t drag down your database speed, Google Programmable Search is the cleanest path forward.

Frequently Asked Questions

Is Google Custom Search free?
Yes, Google Programmable Search Engine is free to use, but the free tier displays search ads alongside the results.
Can I remove ads from Google Custom Search?
Ads can be removed from Google Programmable Search if you integrate it with an AdSense account, or utilize paid API alternatives like Google Custom Search JSON API.
How do I restrict Google Custom Search to my domain?
During setup in the Programmable Search Console, specify your exact site URL (e.g., '*.yourdomain.com') under the 'Sites to search' configuration.
P

Praveen

Technology enthusiast helping people work smarter with practical guides and AI workflows.

Explore more: Browse all website setup guides or check related articles below.