How to create a full screen search box with CSS and JavaScript?

A full screen search box is a popular UI pattern that overlays the entire viewport, providing a focused search experience. This implementation uses CSS for styling and positioning, with JavaScript to control the show/hide functionality.

Key Components

The full screen search box consists of three main parts −

  • Trigger Button: Opens the search overlay
  • Overlay Container: Full screen backdrop with search form
  • Close Button: Hides the search overlay

Example

Here's how to create a complete full screen search box −

<!DOCTYPE html>
<html>
<head>
<style>
body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 20px;
}

* {
    box-sizing: border-box;
}

.showBtn {
    background: #008b0c;
    border: none;
    color: white;
    padding: 12px 20px;
    font-size: 18px;
    cursor: pointer;
    border-radius: 5px;
    transition: opacity 0.3s;
}

.showBtn:hover {
    opacity: 0.8;
}

.overlaySearch {
    height: 100%;
    width: 100%;
    display: none;
    position: fixed;
    z-index: 1000;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.8);
    animation: fadeIn 0.3s;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.searchBar {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 80%;
    max-width: 600px;
}

.overlaySearch .hideBtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 50px;
    cursor: pointer;
    color: white;
    transition: color 0.3s;
}

.overlaySearch .hideBtn:hover {
    color: #ff4444;
}

.search-form {
    display: flex;
    background: white;
    border-radius: 5px;
    overflow: hidden;
}

.overlaySearch input[type="text"] {
    flex: 1;
    padding: 15px 20px;
    font-size: 18px;
    border: none;
    outline: none;
}

.overlaySearch input[type="text"]:focus {
    background: #f9f9f9;
}

.overlaySearch button {
    padding: 15px 25px;
    background: #007bff;
    font-size: 18px;
    border: none;
    color: white;
    cursor: pointer;
    transition: background 0.3s;
}

.overlaySearch button:hover {
    background: #0056b3;
}

h1 {
    color: #333;
    margin-bottom: 20px;
}

.description {
    color: #666;
    margin-top: 15px;
}
</style>
</head>
<body>
    <div class="overlaySearch">
        <span class="hideBtn">×</span>
        <div class="searchBar">
            <form class="search-form">
                <input type="text" placeholder="Search anything..." autofocus>
                <button type="submit">Search</button>
            </form>
        </div>
    </div>

    <h1>Full Screen Search Example</h1>
    <button class="showBtn">Open Search Box</button>
    <p class="description">Click the button above to open the full screen search overlay.</p>

    <script>
        document.querySelector('.hideBtn').addEventListener('click', hideSearch);
        document.querySelector('.showBtn').addEventListener('click', showSearch);

        function showSearch() {
            document.querySelector('.overlaySearch').style.display = "block";
            document.querySelector('.overlaySearch input').focus();
        }

        function hideSearch() {
            document.querySelector('.overlaySearch').style.display = "none";
        }

        // Close on Escape key
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape') {
                hideSearch();
            }
        });
    </script>
</body>
</html>
A page with a green "Open Search Box" button appears. When clicked, a dark overlay covers the entire screen with a centered search form containing a text input and blue search button. A white × close button appears in the top-right corner. The overlay can be closed by clicking the × or pressing Escape.

Key Features

  • Full Screen Overlay: Uses position: fixed with 100% width/height
  • Centered Search Form: Positioned using CSS transforms for perfect centering
  • Smooth Animations: CSS transitions and keyframes for professional appearance
  • Keyboard Support: Auto-focus on input and Escape key to close
  • Responsive Design: Works on all screen sizes with max-width constraint

Conclusion

This full screen search box provides an immersive search experience with smooth animations and keyboard accessibility. The overlay pattern is ideal for drawing user attention while maintaining a clean, modern interface.

Updated on: 2026-03-15T14:39:35+05:30

671 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements