Introduction
How to Create a Simple Search Bar with CSS and HTML
A search bar is an essential component of most websites, allowing users to quickly find information or products. Creating a simple, stylish search bar is easy with just HTML and CSS. Whether you’re building a blog, e-commerce site, or portfolio, adding a search bar enhances user experience and navigation.
In this guide, we’ll walk through building a simple, customizable search bar using HTML and CSS.
Step-by-Step Guide to Building a Simple Search Bar
1. Setting Up the Basic HTML Structure
To start, we’ll create a basic HTML structure for the search bar. We’ll include an input field where users can type their search query and a button to submit the search.
Explanation:
- Search Container: The
.search-container
div wraps the input field and button, allowing us to style them together. - Search Input: The
.search-input
field is where users enter their search terms. The placeholder text is set to “Search…” to prompt users. - Search Button: The
.search-button
is a simple button that will allow users to submit their search query.
2. CSS for Styling the Search Bar
Next, we’ll use CSS to style the search bar, giving it a clean, modern look. This style will make the search bar stand out while keeping it minimal and user-friendly.
Explanation of the CSS:
Container Styling: The
.search-container
usesdisplay: flex
to align the input and button side by side. Theborder-radius
andbox-shadow
properties create rounded corners and a subtle shadow, giving it a modern look.Search Input Styling: The
.search-input
is set to take up all available space inside the container usingflex: 1
, with padding for comfortable typing.Button Styling: The
.search-button
has a blue background color with white text for contrast. Thetransition
onbackground-color
makes the button change smoothly to a darker shade on hover.
3. Adding Icons for a Professional Look (Optional)
For a more polished look, you can add a magnifying glass icon to the search button. Here, we’ll use Font Awesome to add the icon.
Adding Font Awesome:
To use Font Awesome, include the following <link>
tag in the <head>
section of your HTML:
Update HTML for the Icon:
Add the icon to the search button by updating the button’s HTML:
CSS for Icon Alignment:
No additional CSS is necessary, as Font Awesome icons will align automatically with the button text.
4. Making the Search Bar Responsive
To ensure the search bar is accessible on mobile devices, we’ll add a media query to adjust its size on smaller screens.
Explanation:
- Responsive Width: The
.search-container
will be limited to 280px on smaller screens (480px or smaller) to fit comfortably within mobile layouts.
5. Adding Additional Effects (Optional)
To make the search bar even more engaging, consider adding a focus effect that changes the border color when the user clicks into the input field.
CSS for Focus Effect:
Explanation:
- Focus Shadow: The
box-shadow
on focus gives a soft blue glow to the search bar, providing feedback to the user that they are in the input field.
Final HTML and CSS Code
Here’s the complete code with optional styling, icons, and responsive adjustments.
HTML:
CSS:
Preview
See the Pen How to Create a Simple Search Bar with CSS and HTML by codepen (@codepen-the-selector) on CodePen.
Conclusion
Creating a simple search bar with HTML and CSS is an effective way to enhance user experience and provide intuitive navigation on your website. By following this guide, you’ve built a customizable search bar with a sleek design, optional icon integration, and responsive adjustments for mobile.
With this design, you can easily incorporate a search bar into any webpage, whether it’s a blog, e-commerce site, or personal portfolio, making it both functional and aesthetically pleasing.