image

How to Create a Great Looking Search Box with CSS

The search box is one of the most crucial elements of a website or web application. It lets users quickly find what they're looking for, improving the overall user experience. While it may seem like a simple input field, creating a visually appealing and functional search box requires some CSS styling. In this article, we'll explore various techniques to enhance the appearance and functionality of your search box using CSS.

The Basic Structure

Before we dive into the CSS styling, let's start with the basic HTML structure for a search box:

<form>

  <input type="text" placeholder="Search...">

  <button type="submit">Search</button>

</form>

This structure consists of a <form> element containing an <input> field for user input and a <button> for submitting the search query.

Styling the Input Field

The input field is the central component of the search box, so let's start by styling it:

input[type="text"] {

  padding: 10px;

  border: 1px solid #ccc;

  border-radius: 4px;

  font-size: 16px;

  width: 300px;

}

This CSS code applies basic styling to the input field, including padding, border, border-radius, font size, and width. Feel free to adjust these values according to your design preferences.

Adding a Search Icon

To enhance the visual appeal of your search box, you can add a search icon inside the input field. This can be achieved using the background property in CSS:

input[type="text"] {

  padding: 10px 10px 10px 40px;

  background: url('search-icon.png') no-repeat 10px center;

  /* Other styles... */

}

In this example, we're using a background image (search-icon.png) and positioning it 10 pixels from the left and vertically centered within the input field. Make sure to replace search-icon.png with the actual path to your search icon image.

Styling the Submit Button

The submit button is another essential part of the search box. Let's style it to match the input field:

button[type="submit"] {

  padding: 10px 20px;

  background-color: #4CAF50;

  color: white;

  border: none;

  border-radius: 4px;

  cursor: pointer;

}

This CSS code sets the button's padding, background color, and text color, removes the default border, adds a border radius, and changes the cursor to a pointer when hovering over the button.

Positioning the Elements

Now that we've styled the individual components let's position them properly within the search box:

form {

  display: flex;

  align-items: center;

}

input[type="text"] {

  flex-grow: 1;

  margin-right: 10px;

}

Here, we're using CSS Flexbox to align the input field and submit button horizontally. The display flex on the <form> element enables the flexbox layout. The align-items: center vertically centers the elements within the flex container.

The flex-grow: 1 on the input field allows it to grow and take up the remaining space, while the margin-right: 10px adds some spacing between the input field and the submit button.

Adding Hover Effects

To enhance the user experience, you can add hover effects to both the input field and the submit button:

input[type="text"]:hover {

  border-color: #999;

}

button[type="submit"]:hover {

  background-color: #45a049;

}

When the user hovers over the input field, the border color changes to a darker shade of gray (#999). When hovering over the submit button, the background color shifts to a slightly darker shade of green (#45a049).

Responsive Design

In today's multi-device world, ensuring your search box looks great and functions properly on various screen sizes is essential. You can achieve this by using responsive CSS techniques:

@media (max-width: 600px) {

  form {

    flex-direction: column;

    align-items: stretch;

  }

  input[type="text"] {

    margin-right: 0;

    margin-bottom: 10px;

    width: 100%;

  }

  button[type="submit"] {

    width: 100%;

  }

}

This media query targets screens with a maximum width of 600 pixels (adjust as needed). Within this media query, we change the flex-direction to the column, aligning the input field and submit button vertically. We also removed the right margin from the input field, added a bottom margin, and set the width of both elements to 100% to make them stretch across the entire screen width.

Accessibility Considerations

While creating a visually appealing search box is important, it's equally crucial to ensure it's accessible to users with disabilities or those using assistive technologies. Here are some accessibility tips:

  • Use proper labels and associate them with the input field using the <label> element or the for and id attributes.
  • Ensure sufficient color contrast between the text and background for visually impaired users.
  • Provide clear instructions or placeholders to guide users on using the search box.
  • Consider implementing keyboard accessibility by allowing users to navigate and submit the search using the keyboard.

FAQs

How do I change the color of the input field's placeholder text? 

You can change the color of the placeholder text using the ::placeholder pseudo-element:
input[type="text"]::placeholder {

  color: #999;

}

Can I use a different icon instead of the search icon? 

Absolutely! You can replace the background image with any icon you prefer. Just update the background property with the correct file path for your desired icon.

How do I align the search box to the right side of the page? 

To align the search box to the right side of the page, you can use CSS positioning properties or Flexbox. For example, with Flexbox, you can apply margin-left: auto to the <form> element:
form {

  display: flex;

  align-items: center;

  margin-left: auto;

}

Can I add rounded corners to the submit button? 

Yes, you can add rounded corners to the submit button using the border-radius property:
button[type="submit"] {

  border-radius: 20px; /* Adjust the value as needed */

}

How do I change the size of the search icon? 

To change the size of the search icon, you'll need to modify the background-size property in the input field's CSS:
input[type="text"] {

  background: url('search-icon.png') no-repeat 10px center / 20px 20px;

  /* Other styles... */

}
In this example, 20px 20px sets the width and height of the background image to 20 pixels each.

Share On