Floating Search Bar

How to create a Floating Search Bar

Rate this article

Share this article

How to Add a Floating Search Bar to Your WordPress Website

A floating search bar makes it easier for visitors to find what they are looking for on your website. Instead of navigating through multiple pages, users can click the search bar and start searching immediately.

With a full-screen search overlay, the background becomes slightly darker, allowing visitors to focus on searching without distractions.

You can create this experience using HTML, CSS, and JavaScript. If you prefer a simpler solution without coding, you can use the ExpertRec WordPress Search Plugin to add search functionality to your website.

What Does a Floating Search Bar Look Like?

A floating search bar appears in your website’s navigation area. When a visitor clicks it, a full-screen search overlay opens.


Floating search bar with a full-screen search overlay

The main purpose of this design is to give visitors a simple way to search your website while reducing distractions.

How to Create a Floating Search Bar Using HTML, CSS, and JavaScript

In this example, we will create a floating search bar that opens a full-screen search overlay when a visitor clicks the search input or search icon.

The design includes:

  • A search bar inside the navigation area.
  • A full-screen overlay with a dark background.
  • A search input and search button.
  • A close button to exit the search overlay.
  • JavaScript to open and close the overlay.

Step 1: Add the HTML Code

First, create the navigation search bar and the full-screen search overlay using HTML.



The navigation bar contains the regular search input and search button.

When a visitor clicks either of these elements, the full-screen search overlay will open. The overlay contains another search input, a search button, and a close button.

Step 2: Add the CSS Code

Next, add CSS to style the navigation bar, search input, and full-screen overlay.

body {
  background: linear-gradient(to right, #292E49, #536976, #BBD2C5);
  margin: 0;
  font-family: Arial, sans-serif;
}

.navbar {
  height: 70px;
  width: 100%;
  background: #111;
  box-shadow: -2px -2px 12px 3px;
}

.search {
  padding-top: 20px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-right: 25px;
}

.search-text {
  height: 30px;
  border: none;
  border-bottom: 1px solid white;
  width: 200px;
  background: #000;
  color: white;
  font-size: 16px;
}

.search-icon {
  height: 30px;
  border: none;
  border-bottom: 1px solid white;
  background: #000;
  color: white;
  cursor: pointer;
}

.full-screen-search {
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;

  background: rgba(0, 0, 0, 0);
  display: none;

  justify-content: center;
  align-items: center;

  animation: expandSearch 1s forwards;
}

.full-screen-search .close {
  position: fixed;
  top: 20px;
  right: 20px;

  color: white;
  font-family: sans-serif;
  font-size: 36px;

  background: transparent;
  border: none;
  cursor: pointer;
}

.search-container {
  display: flex;
  gap: 10px;
  padding: 20px;
}

.search-container input {
  background: transparent;
  width: 600px;
  max-width: 100%;

  border: none;
  border-bottom: 1px solid white;

  font-size: 24px;
  height: 40px;
  color: white;
}

.search-container button {
  height: 40px;
  border: 2px solid white;

  background: transparent;
  color: white;

  width: 200px;
  font-size: 16px;
  cursor: pointer;
}

@keyframes expandSearch {
  from {
    background: rgba(0, 0, 0, 0);
  }

  to {
    background: rgba(0, 0, 0, 0.8);
  }
}

@media (max-width: 768px) {
  .search-container {
    width: 90%;
    flex-direction: column;
  }

  .search-container input {
    width: 100%;
    box-sizing: border-box;
  }

  .search-container button {
    width: 100%;
  }
}

The important CSS property is position: fixed.

By setting the top, bottom, left, and right properties to zero, the search overlay covers the entire screen.

The overlay is initially hidden using display: none. JavaScript will change it to display: flex when the visitor clicks the search bar.

The CSS animation gradually changes the background to a dark, transparent overlay so that visitors can focus on the search experience.

Step 3: Add the JavaScript Code

Finally, add JavaScript to open and close the full-screen search overlay.

var searchText = document.querySelector('.search-text');
var searchButton = document.querySelector('.search-icon');

var fullScreenDiv = document.querySelector('.full-screen-search');
var closeButton = document.querySelector('.close');

var overlayInput = document.querySelector('.search-container input');

function showScreen() {
  searchText.blur();

  fullScreenDiv.style.display = 'flex';

  overlayInput.focus();
}

function hideScreen() {
  fullScreenDiv.style.display = 'none';
}

searchButton.onclick = showScreen;
searchText.onclick = showScreen;

closeButton.onclick = hideScreen;

The showScreen() function opens the full-screen search overlay.

The hideScreen() function closes the overlay when the visitor clicks the close button.

Once the JavaScript is added, clicking the search input or search button will open the floating search interface.

You can also explore the original example on CodePen.

How to Add a Floating Search Bar to WordPress Without Coding

The HTML, CSS, and JavaScript method is useful if you want to build a custom search interface. However, it requires coding and additional work to connect the search input to your website’s search functionality.

If you are using WordPress, the ExpertRec WordPress Search Plugin provides an alternative way to add search to your website without manually building the entire search interface.

With ExpertRec, you can add website search functionality and customize the search experience for your visitors.

Instead of creating the search interface from scratch, you can use the plugin to get started with search on your WordPress website.

Why Use ExpertRec for WordPress Search?

  • Add search functionality to your WordPress website.
  • Provide visitors with a dedicated search experience.
  • Customize the search interface to suit your website.
  • Reduce the need to build search functionality manually.
  • Make it easier for visitors to discover content on your website.

Get Started With ExpertRec WordPress Search

Want to add search functionality to your WordPress website without writing HTML, CSS, and JavaScript?

Try ExpertRec and create a search experience that helps visitors find relevant content on your website.

Add Search to Your Website

Are you showing the right products, to the right shoppers, at the right time? Contact us to know more.
You may also like