Copied to clipboard

Creating a Beautiful and Responsive Search Input Field

A well-designed search bar is essential for improving user experience on any website. This tutorial will walk you through a modern, minimalist, and fully responsive search input field using HTML, CSS, and Google Fonts.

<html lang="en">
<head>
    <meta charset="UTF-8" />
    <style>
        /* Google Fonts - Poppins */
        @import url("https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap");

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: "Poppins", sans-serif;
        }

        .input-box {
            position: relative;
            height: 76px;
            max-width: 900px;
            width: 100%;
            background: #fff;
            margin: 20px;
            border-radius: 8px;
            box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
        }

        .input-box i,
        .input-box .button {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
        }

        .input-box i {
            left: 20px;
            font-size: 30px;
            color: #707070;
        }

        .input-box input {
            height: 100%;
            width: 100%;
            outline: none;
            font-size: 18px;
            font-weight: 400;
            border: none;
            padding: 0 155px 0 65px;
            background-color: transparent;
        }

        .input-box button {
            right: 20px;
            font-size: 16px;
            font-weight: 400;
            color: #fff;
            border: none;
            padding: 12px 30px;
            border-radius: 6px;
            background-color: #4070f4;
            cursor: pointer;
        }

        .input-box button:active {
            transform: translateY(-50%) scale(0.98);
        }

        /* Responsive */
        @media screen and (max-width: 500px) {
            .input-box {
                height: 66px;
                margin: 0 8px;
            }

            .input-box i {
                left: 12px;
                font-size: 25px;
            }

            .input-box input {
                padding: 0 112px 0 50px;
            }

            .input-box button {
                right: 12px;
                font-size: 14px;
                padding: 8px 18px;
            }
        }
    </style>

    <link rel="stylesheet" href="https://unicons.iconscout.com/release/v4.0.0/css/line.css" />
</head>
<body>
    <form action="https://www.hamrocolleges.com" method="get" class="search-bar">
        <div class="input-box">
            <i class="uil uil-search"></i>
            <input type="text" placeholder="Search" name="s" />
            <button class="button" id="searchButton">Search</button>
        </div>
    </form>
</body>
</html>

Overview of the Code

The code consists of three main parts:

  1. HTML: Provides the structure of the search bar.
  2. CSS: Styles the input box for aesthetics and responsiveness.
  3. Google Fonts and Icons: Enhances the design with professional typography and icons.

HTML Breakdown

The HTML structure creates a form with a search input box and a button.

<form action="https://www.hamrocolleges.com" method="get" class="search-bar">
    <div class="input-box">
        <i class="uil uil-search"></i>
        <input type="text" placeholder="Search" name="s" />
        <button class="button" id="searchButton">Search</button>
    </div>
</form>
  • The <form> tag specifies the action URL and the GET method for passing query parameters.
  • <div class="input-box"> wraps the input field, search icon, and button.
  • <i class="uil uil-search"></i> is the icon for the search bar, provided by the Unicons library.
  • <input> creates the text field for search queries.
  • <button> provides the clickable “Search” functionality.

Styling with CSS

The CSS brings the design to life with a clean, responsive layout.

Font and Basic Styling

@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap");

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: "Poppins", sans-serif;
}
  • The Google Font “Poppins” ensures a modern and professional appearance.
  • The universal selector * resets margins, paddings, and enables consistent sizing.

Input Box Design

.input-box {
    position: relative;
    height: 76px;
    max-width: 900px;
    width: 100%;
    background: #fff;
    margin: 20px;
    border-radius: 8px;
    box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
}

.input-box i {
    position: absolute;
    left: 20px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 30px;
    color: #707070;
}

.input-box input {
    height: 100%;
    width: 100%;
    outline: none;
    font-size: 18px;
    font-weight: 400;
    border: none;
    padding: 0 155px 0 65px;
    background-color: transparent;
}
  • .input-box creates a visually appealing container with rounded corners and a shadow for depth.
  • Icons and text are positioned with precision for a seamless experience.

Button Styling

.input-box button {
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 16px;
    font-weight: 400;
    color: #fff;
    border: none;
    padding: 12px 30px;
    border-radius: 6px;
    background-color: #4070f4;
    cursor: pointer;
}

.input-box button:active {
    transform: translateY(-50%) scale(0.98);
}
  • Buttons have a clean blue background and a hover effect for interactivity.
  • A slight scaling animation on click enhances user feedback.

Responsiveness

The responsive design ensures usability on smaller devices.

@media screen and (max-width: 500px) {
    .input-box {
        height: 66px;
        margin: 0 8px;
    }

    .input-box i {
        left: 12px;
        font-size: 25px;
    }

    .input-box input {
        padding: 0 112px 0 50px;
    }

    .input-box button {
        right: 12px;
        font-size: 14px;
        padding: 8px 18px;
    }
}
  • Adjustments to height, padding, and font size ensure that the design adapts to smaller screens.

Enhancements with External Libraries

  1. Google Fonts: Adds the “Poppins” font for a polished look.
  2. Unicons Icons: Simplifies icon management for modern designs.

How to Use

  1. Copy the code and save it as an HTML file.
  2. Open the file in a browser to see the responsive search bar in action.
  3. Customize styles (e.g., colors, shadows, or dimensions) to fit your project.

Leave a reply