×

HTML Search Box

The HTML search box is also known as the search bar. It is an input text field that is used to search the content. If the users want to explore something, they write the relevant keyword in the search box and search for it.

We can create a search box using <input> tag. We also add a type attribute, which takes the value of text or search to create a search bar. Below is the syntax of the search box:

Syntax: 

<input type="text" >

We will use HTML code to create an HTML search bar. We will use CSS properties to make the web page look interesting.

Examples of HTML Search Box:

Example 1:

This example shows a simple search box created using HTML and CSS.

<!DOCTYPE html>   
<html lang="en">   
<head>  
<meta name="viewport" content="width=device-width, initial-scale=1">  
    <title>
        Example 3 of HTML Search Box
    </title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <style>
        *{
            margin: 0px;
            padding: 0px;
        }
        .main{
            width: 350px;
            border: 2.5px solid brown;
            padding: 10px;
            border-radius: 30px;
        }
        input{
            background: transparent;
            border: 0px;
            outline: 0px;
            width: 320px;
        }
        body{
            display:flex;
            justify-content: center;
            margin-top: 150px;
        }
        .search-icon{
            color: brown;
            font-size: 20px;
        }
    </style>   
</head>    
<body>    
    <div class="main">
        <input type="search" name="search" id="" placeholder="Type to search">
        <a href="#"><i class="fa fa-search search-icon"></i></a>
    </div>  
</body>     
</html>

Output:

The output shows a search box with a red border and a search icon in the center of the web page.

HTML Search Box

Example 2:

This example demonstrates a full-width HTML search button created using HTML and CSS.

<!DOCTYPE html>
<html>
<head>
    <title>
        Example 2 of HTML Search Button
    </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
* {
  box-sizing: border-box;
}
body {
  font-family: sans-serif;
  margin-top: 200px;
}
h1{
  color: #f37c9d;
}
form.bar input[type=text] {
    font-size: 15px;
    padding: 12px;
    border: 1px solid rgb(205, 174, 174);
    width: 80%;
    float: left;
    background: #fad2d2;
}


form.bar button {
  padding: 12px;
  float: left;
  width: 20%;
  color: white;
  background: #f38fab;
  font-size: 15px;
  border-left: none;
  border: 1px solid rgb(205, 174, 174);
  cursor: pointer;
}


form.bar button:hover {
  background: #f37c9d;
}


form.bar::after {
  content: "";
  display: table;
  clear: both;


}
</style>
</head>
<body>
<h1> Full width HTML search box </h1>
<form class="bar" action="/action_page.php">
  <input type="text" placeholder="Search here" name="search">
  <button type="submit"> <i class="fa fa-search"></i> </button>
</form>


</body>
</html>
</html>

Output:

The output shows a search button with 100% width. We have used CSS to style the box to make it look attractive.

HTML Search Box

Example 3:

This example shows an HTML search button in the center of a page with a maximum width using HTML and CSS.

<!DOCTYPE html>
<html>
<head>
    <title>
        Example 3 of HTML Search Button
    </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
* {
  box-sizing: border-box;
}
body {
  font-family: sans-serif;
  margin-top: 150px;
}
h2{
  color: #f37c9d;
  text-align: center;
}
form.bar input[type=text] {
    font-size: 15px;
    padding: 12px;
    border: 1px solid rgb(205, 174, 174);
    width: 80%;
    float: left;
    background: #fad2d2;
}


form.bar button {
  padding: 12px;
  float: left;
  width: 20%;
  color: white;
  background: #f38fab;
  font-size: 15px;
  border-left: none;
  border: 1px solid rgb(205, 174, 174);
  cursor: pointer;
}


form.bar button:hover {
  background: #f37c9d;
}


form.bar::after {
  content: "";
  display: table;
  clear: both;


}
</style>
</head>
<body>


<h2> HTML Search Bar with max-width </h2>


<form class="bar" action="#" style="margin:auto;max-width:300px">
  <input type="text" placeholder="Search here" name="search2">
  <button type="submit"><i class="fa fa-search"></i></button>
</form>


</body>
</html>

Output:

The output shows the search button in the center with the maximum width.

HTML Search Box

Example 4:

The example shows the search box in the navigation bar.

<!DOCTYPE html>
<html>


<head>
	<title>
		Example 4 of HTML Search Box
	</title>
	    <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <style>
		
            #nav {
                background-color: #7e129f;
                position: absolute;
                width: 100%;
            }
                
          #nav a {
                float:left;
                color: #ecd7ed;
                text-align: center;
                display: block;
                font-size: 16px;
                padding: 14px;
                text-decoration: none;
            }
            
            .nav-right{
                float:right;
            }
        
            #nav a:hover {
                background-color: rgb(215, 163, 241);
                color: rgb(111, 44, 111);
            }
                
            .search input[type=text]{
                height:26px;
                width:280px;
                border-radius:24px;
                border: none;
            }
                
            .search{
                float:right;
                margin:8px;
            }
                
            .search button{
                background-color: #7e129f;
                color: #f9e7fa;
                float: right;
                margin-right: 16px;
                padding: 5px 10px;
                font-size: 12px;
                cursor: pointer;
                border: none;


            }
        </style>
        
</head>


<body>


	<div id="nav">
		<a href="#"> Home </a>
		<a href="#"> About Us </a>
		<a href="#"> Contact Us </a>
        <a href="#"> Careers </a>
		<a href="#"> Courses </a>
		
		<div class="search">
			<form action="#">
				<input type="text"
					placeholder="Search courses"
					name="search">
				<button>
					<i class="fa fa-search"
						style="font-size: 15px;">
					</i>
				</button>
			</form>
		</div>
    </div>


</body>


</html>

Output:

The result shows the search bar inside the navigation bar.

HTML Search Box

Example 5:

In this example, we are creating a responsive search box in the navigation bar.

<!DOCTYPE html>
<html>
<head>
  <title>
    Example 5 of HTML Search Box
  </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box;}


body {
  margin: 0;
  font-family: system-ui, 'Helvetica Neue', Helvetica, sans-serif;
}


.nav {
  overflow: hidden;
  background-color: #b8daef;
}


.nav a {
  float: left;
  text-align: center;
  color: blue;
  display: block;
  text-decoration: none;
  padding: 14px 16px;
  font-size: 16px;
}


.nav a:hover {
  background-color: rgb(127, 213, 239);
  color: rgb(217, 61, 22);
}


.nav .search-container {
  float: right;
}


.nav input[type=text] {
  padding: 6px;
  margin-top: 8px;
  font-size: 17px;
  border: none;
}


.nav .search-container button {
  float: right;
  background: rgb(188, 196, 214);
  margin-top: 7px;
  margin-right: 15px;
  padding: 5px;
  font-size: 18px;
  cursor: pointer;
  border: none;


}


.nav .search-container button:hover {
  background: rgb(175, 187, 188);
}


@media screen and (max-width: 600px) {
  .nav .search-container {
    float: none;
  }
  .nav a, .nav input[type=text], .topnav .search-container button {
    float: none;
    text-align: left;
    display: block;
    width: 100%;
    padding: 14px;
    margin: 0;


  }
  .nav input[type=text] {
    border: 1px solid rgb(237, 218, 218);  
  }
}
.content{
  padding-left:18px;
  color: blue;
}
</style>
</head>
<body>


<div class="nav">
  <a class="active" href="#home"> Home </a>
  <a href="#about"> About </a>
  <a href="#contact"> Contact </a>
  <div class="search-container">
    <form action="#">
      <input type="text" placeholder="Type to search" name="Search">
      <button type="Submit"> Submit </button>
    </form>
  </div>
</div>


<div class="content">
  <h2> Responsive Search Bar </h2>
</body>
</html>

Output:

You can see the responsive search bar in the navigation bar.

HTML Search Box

When you adjust the page below 600px, you will see the responsiveness of the web page. The form elements will be stacked on the top of each other, as shown in the result of following snapsot:

HTML Search Box

Example 6:

In this example, we will create a working search bar. For that, we have to use JavaScript, so we will use HTML, CSS, and JavaScript to create a functional search bar.
We will create a search bar which examine for substrings in a string. We will create a list of items that include different foods so that we can search for food by typing the word in the search bar. We will also give the style to the search bar using padding, margin, border radius, etc. Then finally, we will add JavaScript to add functionality to the search bar.

<!DOCTYPE html>
<html>
	
<head>
	<title>
		Example 6 of HTML Search Box
	</title>


 <script>
  function search_food() {
    let input = document.getElementById('searchbar').value
    input=input.toLowerCase();
    let x = document.getElementsByClassName('food');
    
    for (i = 0; i < x.length; i++) {
      if (!x[i].innerHTML.toLowerCase().includes(input)) {
        x[i].style.display="none";
      }
      else {
        x[i].style.display="list-item";				
      }
    }
  }
 </script>
 
<style>
  #searchbar{
	margin-left: 15%;
	border-radius: 12px;
  padding:16px;
}


input[type=text] {
	width: 40%;
	-webkit-transition: width 0.15s ease-in-out;
	transition: width 0.15s ease-in-out;
}


input[type=text]:focus {
	width: 60%;
}


#list{
	font-size: 25px;
	margin-left: 100px;
}


.food{
display: list-item;	
}


</style>
</head>
<body>
	
	<input id="searchbar" onkeyup="search_food()" type="text"
		name="search" placeholder="Search food..">
	<ol id='list'>
		<li class="food"> Burger </li>
		<li class="food"> Pizza </li>
		<li class="food"> Chilly Potato </li>
		<li class="food"> Tomato Soup </li>
		<li class="food"> Manchurian </li>
		<li class="food"> Garlic Bread </li>
		<li class="food"> Fried Rice </li>
		<li class="food"> Noodles </li>
		<li class="food"> Spring Roll </li>
		<li class="food"> Momos </li>
		<li class="food"> French Fries </li>
	</ol>
</body>
</html>

Output:

The output shows a search bar followed by a list of foods. When you type something in the search bar, the width of the search bar increases by 60%, and the border gets highlighted.

HTML Search Box

When you write any food name like we have searched Pizza. You will see the word appearing on the screen as shown below in the result.

HTML Search Box

Browser Support:

It supports the following browsers:

  1. Google Chrome
  2. Internet Explorer
  3. Mozilla Firefox
  4. Microsoft Edge
  5. Safari
  6. Opera

Related Topics

HTML5 Tags

HTML5 Tags A list of newly included tags is provided in HTML5. These HTML 5 tags (elements) provide a better structure for the document. This list shows description of all HTML...

3 minutes read.

Design a tribute page using HTML and CSS

A tribute page is a webpage based on a person who inspired us in our life. In this article, we will create a webpage of Mahatma Gandhi. We are going...

5 minutes read.

HTML Layouts

HTML Layouts HTML templates provide a way for well-managed, well-structured responsive web pages to be organized. We could say that the HTML layout specifies how web pages can be set up....

4 minutes read.

HTML Anchor

HTML Anchor The HTML anchor tags define a hyperlink that links to other pages on one page. It can create hyperlinks to any web page and files, locations or any other...

3 minutes read.

Html &lt;Col> Tag

You can write the col tag inside the table tag after the thead, tbody, foot, and tr tags. You can also apply it after the caption tag (in which the...

4 minutes read.

HTML <strike> tag

The HTML <strike> tag is used to specifies strikethrough text. The tag is deprecated now and <del> tag is used in HTML files instead of it. All the global and...

2 minutes read.

HTML <samp> tag

The Samp tag in HTML is used for defining the sample output from the computer program. This tag is a phrase type tag and a container tag that means the...

2 minutes read.

HTML Forms

Forms are required when we want to collect data from the user. Html forms consists of different fields to take user inputs such as: input, text area, checkbox, password, radio...

4 minutes read.

HTML <sub> tag

The sub element in HTML is used to describe one or more subscript texts. If The sub tag shows the text which is presented in a smaller font and below...

2 minutes read.

HTML <span> tag

The span tag in HTML is used as an inline container element to mark up a specific or special chunk of a text or a section of a page. If you...

2 minutes read.

HTML Form Action

The <form> tag has an attribute named action that contains a URL. The HTML action attribute defines the webpage URL on which you want to send the data after submitting...

4 minutes read.

How to add JavaScript to HTML

JavaScript is one of the most important languages today. It is a high-level, interpreted, lightweight programming language that makes web pages more interactive. It is a language of the web....

5 minutes read.

HTML 5 Tutorial

What is HTML5? HTML5 is a programming language which stands for the acronym Hyper Text Markup Language 5. It is program that enables the appearance of web pages to be changed, as well as changes...

5 minutes read.

Html &lt;datalist >Tag

The autocomplete feature in HTML files is provided by the <datalist> tag. It can be used in conjunction with an input tag to enable users to quickly fill out form...

4 minutes read.

HTML SVG

HTML SVG: SVG stands for Scalable Vector Graphics which defines the graphics for the web. It is a language for describing 2-D graphics in XML. Each drawn shape in SVG is...

1 minute read.

HTML Audio

HTML AUDIO: HTML5 provides a method to embed the audio file into a webpage by using HTML5. Before HTML5 audio files is used to play on the browser with the help of...

1 minute read.

HTML Reset Button

The HTML reset button is used to reset all values in the form. It clears all input fields. Reset is the value of the type attribute of the button element. The...

6 minutes read.

HTML Unordered List

In HTML, we have <ul> tag which defines the unordered list. And it is also known as bulleted list, because as we know it is not ordered list therefore it...

2 minutes read.

HTML <source> tag

For media attributes, such as <video> tag, <audio> tag, and <picture> tag, we use the <source> tag in html for adding such type of multiple media html elements in our...

4 minutes read.

HTML <u> tag

Underline Element/Tag: The u tag in html is used to specify the important text or phrase by underline. In earlier HTML versions, this element was known as the "Underline" element, and...

5 minutes read.