×

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

Html Wbr Tag

The Line Break Opportunity element A word break opportunity is a place in text from where the browser breaks a line. This opportunity is represented by the element wbr in the...

2 minutes read.

HTML Form Input Types

HTML Form Input Types An important element of the HTML form is < input type= "> in HTML. The type attribute of the input element can be of different types, which...

8 minutes read.

HTML <sup> Tag

The HTML sup element is used to describe one or more superscript contents. The sup tag shows a text which is presented above the regular line and half the length...

2 minutes read.

HTML City tag

HTML <City> tag HTML < cite > tag specifies a citation, a reference or title to a creative work, quoted content, books, websites, a research paper, a blog post, a painting,...

3 minutes read.

Html &lt;center> Tag

The term HTML refers to Hyper Text Markup Language. The most widely used markup language for making web pages is HTML. A structure of web page is described in HTML....

3 minutes 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 <script> Tag

For embedding a client -side script, we use script tag in html tag. This <script> tag can be used for containing scripting statements. It can also point another external script file...

2 minutes read.

CSS Clearfix

CSS Clearfix: A clearfix (or clear float) in CSS is for a component to clear or fix the child component, so we don’t need to insert additional markup. The clear...

4 minutes read.

HTML Charsets

In this tutorial, we are going to discuss about the charsets in the HTML. The letters, numbers, and several other symbols are shown correctly by the web browser. All of this...

2 minutes read.

HTML <style> tag

The section of information that we want to style is contained in html style tag. It includes CSS, which is used to design the text and images in the web page. Syntax: <head> <title>…………....

3 minutes read.

HTML Frames

Frames in Html is used to divide the browser into different sections where each section can load different Html documents. Collection of frame is known as Frameset. Creating frames:We use frameset...

2 minutes read.

Difference between HTML and DHTML

Introduction to HTML HTML stands for Hypertext Markup Language. It is a standard web language used to create web pages and applications. HTML is used to define the structure and content...

4 minutes read.

HTML Code Tag

The code tag in HTML is a phrase tag used to insert a piece of computer programming code. In HTML there is a separate tag named as <code> tag. Sometimes,...

3 minutes read.

HTML Ordered List

We use <ol> tag to make an ordered list in html. An ordered list is made to make a list of items in ordered form, ordered list can be either...

3 minutes read.

What does HTML stand for?

HTML stands for Hyper Text Markup Language. We use this markup language to design a web page. Web browsers use this language to showcase text, videos, images, audio and other...

6 minutes read.

HTML5 Button Tag

HTML Button Tag The < button > tag is used on the webpage to construct a clickable button inside the HTML. Within the < button>....</button > tag, we can place content...

2 minutes read.

HTML <strong> tag

The strong element in HTML is used to display the real significant information in bold so that the user or reader will understand it easily. Syntax: <p> <strong>………… </strong> </p> Example Here, we will demonstrate how to...

2 minutes read.

Responsive HTML5 and CSS3 Tutorial

Introduction to HTML5 Tutorial HTML5 is the latest and upgraded version of HTML. HTML is a hypertext markup language. Technically, it is not a programming language. It is a markup language. HTML...

17 minutes read.

HTML Background Image

Inserting image in HTML An image (technically digital image) in HTML or even in general technical aspects, is a binary representation of visual information. By visual information we mean, in the...

8 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.