Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A search bar in HTML allows users to input queries and find relevant results on a webpage. It is commonly used in websites to enhance navigation and user experience. A basic search bar can be created using the <input> element with the type="search" attribute. Additional styling and functionality can be added using CSS and JavaScript.
In this article, you will learn how to create a search bar in HTML, style it with CSS, and add functionality using JavaScript.
Approach
To create a search bar in HTML, we will follow these steps:
Basic Search Bar with HTML – Using only the <input> and <button> elements.
Styled Search Bar with CSS – Enhancing the appearance with CSS.
Search Bar with JavaScript Functionality – Adding dynamic search capabilities.
Search Bar with Auto-Suggestions – Implementing an advanced feature.
Let's look at each approach with examples.
Example 1: Basic Search Bar with HTML
This is a simple search bar using only HTML elements.
Filters and displays suggestions based on user input.
Clicking a suggestion fills the search box.
Example with Submit Button
A search bar with a submit button is one of the most common designs you’ll see on websites. It allows users to type their query into a text box & then click the button to initiate the search. Below, we’ll learn with the help of an example how to create this using HTML.
To start, open your code editor & create a new file named `index.html`. Copy the following code into the file:
The `<input>` tag creates the text box where users can type their queries. The `placeholder` attribute provides a hint about what to enter.
The `<button>` tag adds the submit button next to the text box.
2. CSS Styling:
The `.search-container` uses `display: flex` to align the text box & button horizontally.
The `.search-box` has padding, a border, & rounded corners for better aesthetics.
The `.search-button` is styled with a blue background, white text, & hover effects to make it visually appealing.
3. How It Works:
When a user types something into the text box & clicks the "Search" button, the browser sends the input data to the server (if connected to a backend). For now, this example focuses only on the front-end design.
This setup is beginner-friendly & works well for static websites or prototypes.
Example with Submit Icon
A search bar with a submit icon is another popular design choice. Instead of a traditional button with text, this version uses an icon (like a magnifying glass) to make the interface look cleaner & more modern. Below, we’ll show you how to create this using HTML & CSS.
To begin, open your code editor & create a new file named `search-icon.html`. Copy the following code into the file:
The `<input>` tag creates the text box where users can type their queries.
The `<span>` tag with the class `search-icon` displays the magnifying glass icon. The `&128269;` is a Unicode character for the magnifying glass symbol.
2. CSS Styling:
The `.search-container` uses `position: relative` to allow precise placement of the icon within the container.
The `.search-box` has padding, a border, & rounded corners for better aesthetics.
The `.search-icon` is positioned absolutely within the container, aligned to the right side of the text box. The `transform: translateY(-50%)` ensures the icon is vertically centered.
3. How It Works:
When a user types something into the text box & clicks the magnifying glass icon, the browser can send the input data to the server (if connected to a backend). For now, this example focuses only on the front-end design.
This design is sleek & works well for websites aiming for a minimalist or modern look.
Frequently Asked Questions
How do I make the search bar responsive?
Use CSS media queries to adjust styles for different screen sizes.
Can I connect the search bar to a database?
Yes, using server-side languages like PHP and SQL.
How can I add a search icon?
Use an <i> tag with FontAwesome or an <img> tag inside the button.
Conclusion
In this article, we covered how to create a search bar in HTML using the <input> element with the type="search" attribute. By combining it with CSS for styling and JavaScript for functionality, we can enhance user experience. Understanding how to build a search bar helps in improving website navigation and user interaction efficiently.