Search Box – CodingNepal https://www.codingnepalweb.com CodingNepal is a blog dedicated to providing valuable and informative content about web development technologies such as HTML, CSS, JavaScript, and PHP. Sun, 14 May 2023 17:18:54 +0000 en-US hourly 1 https://wordpress.org/?v=6.4.2 Search Bar in HTML CSS & JavaScript https://www.codingnepalweb.com/search-bar-html-css-javascript-2/ https://www.codingnepalweb.com/search-bar-html-css-javascript-2/#respond Fri, 15 Jul 2022 21:11:20 +0000 https://www.codingnepalweb.com/?p=4169 Search Bar in HTML CSS & JavaScript

Hello friend, I hope you are doing awesome. Today you will learn to create a Search Bar in HTML CSS & JavaScript I will add some animation to our search bar also. Earlier I created a Seach Box using HTML and CSS only. But in today’s search box I will add a few JavaScript.

Search Bars are the section where users can search for the things that they want to. Search Bar can be in different designs but the main objective of the search bar is to search.

Have a quick look on the given image of our search bar on the webpage. As you can see from the image of our search box. There is a search box, inside the search box there are two icons and a search section. Actually, in our real project, this search bar will be in closed form and when we click on the search icon then the search bar will open, and close the search bar we need to click on the cross icon. I have added beautiful cubic animation too.

To see the real demo of this search bar and its animation, you need to watch the give video tutorial of our search bar. Also by watching the video tutorial, you will get an idea about HTML CSS, and JavaScript codes that I have used to create this search bar.

Search Bar in HTML CSS & JavaScript | Video Tutorial

I have provided all the HTML CSS and JavaScript code that I have used to create this Search Bar. Before getting into the source code file, I would like to explain the given video tutorial briefly.

As you have seen in the video tutorial. At first, we saw one square shape box with a search icon. When I clicked on that square box, the search box opened and cross icons also appeared. When I clicked on that cross icon then the search bar closed and the cross icon disappeared. The search bar is created by HTML and CSS, for the animation, I have used CSS cubic bezier and to open and close the search bar I have used some JavaScript code.

I hope now you can create this search bar using HTML CSS and JavaScript. If you are feeling difficulty creating this sidebar, I have provided all the source code below.

You Might Like This:

Search Bar [Source Code]

To get the following HTML and CSS code for the Search Bar you need to create two files one is an HTML file and another is a CSS file. After creating these two files then you can copy-paste the given codes on your document. You can also download all source code files from the given download button.

 

<!DOCTYPE html>
<!-- Coding By CodingNepal - codingnepalweb.com -->
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title> Animated Search Bar </title>

        <!-- CSS -->
        <link rel="stylesheet" href="css/style.css">
                
        <!-- Unicons CSS -->
        <link rel="stylesheet" href="https://unicons.iconscout.com/release/v4.0.0/css/line.css">
                        
    </head>
    <body>
        <div class="input-box">
            <input type="text" placeholder="Search...">
            <span class="icon">
                <i class="uil uil-search search-icon"></i>
            </span>
            <i class="uil uil-times close-icon"></i>
        </div>

        <script>
            let inputBox = document.querySelector(".input-box"),
                searchIcon = document.querySelector(".icon"),
                closeIcon = document.querySelector(".close-icon");

            searchIcon.addEventListener("click", () => inputBox.classList.add("open"));
            closeIcon.addEventListener("click", () => inputBox.classList.remove("open"));
        </script>
        
    </body>
</html>
/* Google Fonts - Poppins */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap');

*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}
body{
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #4070f4;
  overflow: hidden;
}
.input-box{
  position: relative;
  height: 55px;
  max-width: 55px;
  width: 100%;
  margin: 0 40px;
  border-radius: 6px;
  background-color: #fff;
  transition: all 0.6s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
.input-box.open{
  max-width: 350px;
}
input{
  position: relative;
  outline: none;
  border: none;
  height: 100%;
  width: 100%;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 400;
  color: #333;
  background-color: #fff;
}
.input-box.open{
  padding: 0 15px 0 65px;
}
.icon{
  position: absolute;
  height: 100%;
  top: 0;
  left: 0;
  width: 60px;
  border-radius: 6px;
  display: flex;
  justify-content: center;
  background-color: #fff;
}
.search-icon,
.close-icon{
  position: absolute;
  top: 50%;
  font-size: 30px;
  transition: all 0.6s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
.search-icon{
  color: #4070f4;
  
  transform: translateY(-50%) rotate(90deg);
}
.input-box.open .search-icon{
  transform: translateY(-50%) rotate(0);
}
.close-icon{
  right: -45px;
  color: #fff;
  padding: 5px;
  opacity: 0;
  pointer-events: none;
  transform: translateY(-50%);
}
.input-box.open .close-icon{
  opacity: 1;
  pointer-events: auto;
  transform: translateY(-50%) rotate(180deg);
}

 

 

]]>
https://www.codingnepalweb.com/search-bar-html-css-javascript-2/feed/ 0
How To Make Search Box Using HTML & CSS https://www.codingnepalweb.com/how-to-make-search-box-using-html-css/ https://www.codingnepalweb.com/how-to-make-search-box-using-html-css/#respond Mon, 15 Feb 2021 21:09:48 +0000 https://www.codingnepalweb.com/?p=4236 Search Box Design Using HTML CSS

Hello Readers, today in this blog I’m going to create a Search Box Design Using HTML CSS, and this search box will fully responsive. Earlier I have shared How To Create Login Form in HTML CSS, now we will go for search bar design.

Simply, A search box is an input field where the user has to type what they wanted from the specific webpages. Nowadays there are various types of search boxes on the internet but the main motive of a search bar is the same.

As you can see on the given image of this program [Responsive Search Box], This is the real example of a search bar. At first, this search bar is in small form literally we can see only the search icon and when we clicked on that search icon smoothly search box appears with an input field.

If you want to see the essential code behind this program [Responsive Search Bar Design], and all animation that I have added to this program please do watch the full video tutorial of this program [Animated Search Box Design] that I have provided below.

Full Video Tutorial Search Box Design Using HTML & CSS

As you have seen in the given tutorial of this program [Search Box Design ], At first there is only a search box icon on the screen in round shape, when we clicked on that search icon search’s input field smoothly and the round shape of the icon turns into rectangle shape in the left part and round shape in the right side. To make this animation I have used HTML input’s checkbox.

If you are familiar with HTML & CSS then you can easily create this program [Responsive Search Box Design], those friends who are feeling difficulty to build this program, don’t worry i have provided full source code below.

You Might Like This:

Search Box [Source Code]

To copy-paste the given codes of this program [Animated Search Bar Design], at first you need to create two files, one is an HTML file and another is a CSS file, after creating these two files you can easily copy-paste the given codes in your document.

<!DOCTYPE html>
<!-- Created By CodingLab - www.codinglabweb.com -->
<html lang="en" dir="ltr">
  <head>
    <meta charset="UTF-8">
    <title> Responsive Search Box | CodingLab </title>
    <link rel="stylesheet" href="style.css">
    <!-- Fontawesome CDN Link -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
   </head>
<body>
  <div class="box">
    <input type="checkbox" id="check">
    <div class="search-box">
      <input type="text" placeholder="Type here...">
      <label for="check" class="icon">
        <i class="fas fa-search"></i>
      </label>
    </div>
  </div>

</body>
</html>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap');
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Poppins',sans-serif;
}
body{
  height: 100vh;
  width: 100%;
  background: #FF676D;
  display: flex;
  align-items: center;
  justify-content: center;
}
.box{
  max-width: 400px;
  width: 100%;

}
.box .search-box{
  position: relative;
  height: 50px;
  max-width: 50px;
  margin: auto;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.25);
  border-radius: 25px;
  transition: all 0.3s ease;
}
#check:checked ~ .search-box{
  max-width: 380px;
}
.search-box input{
 position: absolute;
 height: 100%;
 width: 100%;
 border-radius: 25px;
 background: #fff;
 outline: none;
 border: none;
 padding-left: 20px;
 font-size: 18px;
}
.search-box .icon{
  position: absolute;
  right: -2px;
  top: 0;
  width: 50px;
  background: #FFF;
  height: 100%;
  text-align: center;
  line-height: 50px;
  color: #FF676D;
  font-size: 20px;
  border-radius: 25px;
}
#check:checked ~ .search-box .icon{
  background: #FF676D;
  color: #FFF;
  width: 60px;
  border-radius: 0 25px 25px 0;
}
#check{
  display: none;
}

If you face any difficulties while creating your Animated Search Box or your code is not working as expected, you can download the source code files for this CSS Search Bar for free by clicking on the download button, and you can also view a live demo of this card slider by clicking on the view live button.

]]>
https://www.codingnepalweb.com/how-to-make-search-box-using-html-css/feed/ 0
Search Bar with Autocomplete Search Suggestions in JavaScript https://www.codingnepalweb.com/search-bar-autocomplete-search-suggestions-javascript/ https://www.codingnepalweb.com/search-bar-autocomplete-search-suggestions-javascript/#comments Sat, 17 Oct 2020 10:04:00 +0000 https://codingnepalweb.com/2020/10/17/search-bar-with-autocomplete-search-suggestions-in-javascript/ Search Bar with Autocomplete Search Suggestions using HTML CSS & JavaScriptHello readers, Today in this blog you’ll learn how to create a Simple Search Bar with Autocomplete Search Suggestions using HTML CSS & JavaScript. Earlier I’ve shared a blog on how to create an Animated Search Bar using only HTML & CSS and now it’s time to create Autocomplete Textbox or Searchbox.

 
A search box is a graphical UI element present in many websites. It works as the field for a query input or search term from the user to search and retrieve related information from the database. Autocomplete is a pattern or feature used to display query suggestions and predict the rest of a word a user is typing.

In this program [Search Bar with Auto-complete Search Suggestions], on the webpage, there is a search bar and when you type something, there is shown a autocomplete box that suggests several predictions of how your query could be completed means there are shown several suggestions related your query. If you’re feeling difficult to understand what I am saying. You can watch a full video tutorial on this program [Autocomplete Search Bar].

Video Tutorial of Search Bar with Autocomplete Search

 
In the video, you have seen the Search Bar with Autocomplete Search Suggestions and there is no content-related user query so I redirected the user query to Google but you can show your content and I hope you have understood the basic codes or concepts behind creating this autocomplete textbox or search box.

If you know PHP & MySQL then you can use this search bar to retrieve or shown several search suggestions related to the user queries from your database. If you like this program and want to get source files or codes of this program then you need to do a little bit of scroll down.

You might like this:

Search Bar with Auto-complete Search [Source Codes]

To create this program (Autocomplete Search Suggestions). First, you need to create four Files one HTML File, CSS File and two are JavaScript Files. After creating these files just paste the following codes into your file.

First, create an HTML file with the name of index.html and paste the given codes in your HTML file. Remember, you’ve to create a file with .html extension.

<!DOCTYPE html>
<!-- Created By CodingNepal - www.codingnepalweb.com -->
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Autocomplete Search Box | CodingNepal</title>
    <link rel="stylesheet" href="style.css">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"/>
  </head>
  <body>
    <div class="wrapper">
      <div class="search-input">
        <a href="" target="_blank" hidden></a>
        <input type="text" placeholder="Type to search..">
        <div class="autocom-box">
          <!-- here list are inserted from javascript -->
        </div>
        <div class="icon"><i class="fas fa-search"></i></div>
      </div>
    </div>

     <script src="js/suggestions.js"></script> 
     <script src="js/script.js"></script> 

  </body>
</html>

Second, create a CSS file with the name of style.css and paste the given codes in your CSS file. Remember, you’ve to create a file with .css extension.

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap');
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

body{
  background: #644bff;
  padding: 0 20px;
}

::selection{
  color: #fff;
  background: #664AFF;
}

.wrapper{
  max-width: 450px;
  margin: 150px auto;
}

.wrapper .search-input{
  background: #fff;
  width: 100%;
  border-radius: 5px;
  position: relative;
  box-shadow: 0px 1px 5px 3px rgba(0,0,0,0.12);
}

.search-input input{
  height: 55px;
  width: 100%;
  outline: none;
  border: none;
  border-radius: 5px;
  padding: 0 60px 0 20px;
  font-size: 18px;
  box-shadow: 0px 1px 5px rgba(0,0,0,0.1);
}

.search-input.active input{
  border-radius: 5px 5px 0 0;
}

.search-input .autocom-box{
  padding: 0;
  opacity: 0;
  pointer-events: none;
  max-height: 280px;
  overflow-y: auto;
}

.search-input.active .autocom-box{
  padding: 10px 8px;
  opacity: 1;
  pointer-events: auto;
}

.autocom-box li{
  list-style: none;
  padding: 8px 12px;
  display: none;
  width: 100%;
  cursor: default;
  border-radius: 3px;
}

.search-input.active .autocom-box li{
  display: block;
}
.autocom-box li:hover{
  background: #efefef;
}

.search-input .icon{
  position: absolute;
  right: 0px;
  top: 0px;
  height: 55px;
  width: 55px;
  text-align: center;
  line-height: 55px;
  font-size: 20px;
  color: #644bff;
  cursor: pointer;
}

Third, create a JavaScript file with the name of suggestions.js and paste the given codes in your JavaScript file. Remember, you’ve to create a file with .js extension. We create this file to store all suggestion keywords.

let suggestions = [
    "Channel",
    "CodingLab",
    "CodingNepal",
    "YouTube",
    "YouTuber",
    "YouTube Channel",
    "Blogger",
    "Bollywood",
    "Vlogger",
    "Vechiles",
    "Facebook",
    "Freelancer",
    "Facebook Page",
    "Designer",
    "Developer",
    "Web Designer",
    "Web Developer",
    "Login Form in HTML & CSS",
    "How to learn HTML & CSS",
    "How to learn JavaScript",
    "How to became Freelancer",
    "How to became Web Designer",
    "How to start Gaming Channel",
    "How to start YouTube Channel",
    "What does HTML stands for?",
    "What does CSS stands for?",
];

Last, create a JavaScript file with the name of script.js and paste the given codes in your JavaScript file. Remember, you’ve to create a file with .js extension.

// getting all required elements
const searchWrapper = document.querySelector(".search-input");
const inputBox = searchWrapper.querySelector("input");
const suggBox = searchWrapper.querySelector(".autocom-box");
const icon = searchWrapper.querySelector(".icon");
let linkTag = searchWrapper.querySelector("a");
let webLink;

// if user press any key and release
inputBox.onkeyup = (e)=>{
    let userData = e.target.value; //user enetered data
    let emptyArray = [];
    if(userData){
        icon.onclick = ()=>{
            webLink = `https://www.google.com/search?q=${userData}`;
            linkTag.setAttribute("href", webLink);
            linkTag.click();
        }
        emptyArray = suggestions.filter((data)=>{
            //filtering array value and user characters to lowercase and return only those words which are start with user enetered chars
            return data.toLocaleLowerCase().startsWith(userData.toLocaleLowerCase());
        });
        emptyArray = emptyArray.map((data)=>{
            // passing return data inside li tag
            return data = `
  • ${data}
  • `; }); searchWrapper.classList.add("active"); //show autocomplete box showSuggestions(emptyArray); let allList = suggBox.querySelectorAll("li"); for (let i = 0; i < allList.length; i++) { //adding onclick attribute in all li tag allList[i].setAttribute("onclick", "select(this)"); } }else{ searchWrapper.classList.remove("active"); //hide autocomplete box } } function select(element){ let selectData = element.textContent; inputBox.value = selectData; icon.onclick = ()=>{ webLink = `https://www.google.com/search?q=${selectData}`; linkTag.setAttribute("href", webLink); linkTag.click(); } searchWrapper.classList.remove("active"); } function showSuggestions(list){ let listData; if(!list.length){ userValue = inputBox.value; listData = `
  • ${userValue}
  • `; }else{ listData = list.join(''); } suggBox.innerHTML = listData; } }

    That’s all, now you’ve successfully created a Search Bar with Autocomplete Search Suggestions in JavaScript. If your code does not work or you’ve faced any error/problem, please download the source code files from the given download button. It’s free and a .zip file will be downloaded then you’ve to extract it.

     

    ]]>
    https://www.codingnepalweb.com/search-bar-autocomplete-search-suggestions-javascript/feed/ 53
    Animated Search Box using HTML CSS & JavaScript https://www.codingnepalweb.com/animated-search-box-javascript/ https://www.codingnepalweb.com/animated-search-box-javascript/#comments Fri, 04 Sep 2020 13:03:00 +0000 https://codingnepalweb.com/2020/09/04/animated-search-box-using-html-css-javascript/ Animated Search Box using HTML CSS & JavaScript Elastic Animation on Search Bar

    Hello readers, Today in this blog you’ll learn how to create an Animated Search Box/Bar using HTML CSS & JavaScript. Earlier I have shared a blog on how to create FullScreen Overlay Search Box Animation using only HTML & CSS and now it’s time to create an Elastic Animation on Search Bar with JavaScript.

    The search box is the most important graphical element present in every website. It works as the field for a query input or searches term from the user to search and retrieve related data from the database. A simple search bar can make using HTML, CSS, and JavaScript only.

    In this program [Animated Search Box], at first, on the webpage, there is only a search icon and when you click on that icon, then the search input appears with an elastic animation. When you type something and click on the search icon, your typed data is shown at the bottom of the search bar.  There is also shown a cancel or hide icon [cross sign icon] and when you click on that cancel icon, the visible search input and your typed data will be hidden.

    If you’re feeling difficult to understand what I am saying. You can watch a full video tutorial on this program [Elastic Animation on Search Bar].

    Video Tutorial of Elastic Animation on Search Bar

     
    In the video, you have seen an Animated Search Bar and I hope you’ve understood the basic codes behind creating this program. I used pure JavaScript to create this search bar so if you’re a beginner and you know a little bit of JavaScript, then you can easily create this type of search bar animation.

    If you have knowledge of backend languages like PHP then you can add PHP code and connect this search box with the database to retrieve data according to the user queries. I believe this small search bar program will help you a lot. If you like this program [Elastic Animation on Search Bar] and want to get source codes. You can easily get the source codes of this program.

    You might like this: 

    Animated Search Box [Source Codes]

    To create this program [Elastic Animation on Search Bar]. First, you need to create two Files one HTML File and another one is CSS File. After creating these files just paste the following codes in your file.

    First, create an HTML file with the name of index.html and paste the given codes in your HTML file. Remember, you’ve to create a file with .html extension.

    <!DOCTYPE html>
    <!-- Created By CodingNepal -->
    <html lang="en" dir="ltr">
      <head>
        <meta charset="utf-8">
        <title>Animated Search Box | CodingNepal</title>
        <link rel="stylesheet" href="style.css">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"/>
      </head>
      <body>
        <div class="search-box">
          <input type="text" placeholder="Type to search..">
          <div class="search-icon">
            <i class="fas fa-search"></i>
          </div>
    <div class="cancel-icon">
            <i class="fas fa-times"></i>
          </div>
    <div class="search-data">
    </div>
    </div>
    <script>
          const searchBox = document.querySelector(".search-box");
          const searchBtn = document.querySelector(".search-icon");
          const cancelBtn = document.querySelector(".cancel-icon");
          const searchInput = document.querySelector("input");
          const searchData = document.querySelector(".search-data");
          searchBtn.onclick =()=>{
            searchBox.classList.add("active");
            searchBtn.classList.add("active");
            searchInput.classList.add("active");
            cancelBtn.classList.add("active");
            searchInput.focus();
            if(searchInput.value != ""){
              var values = searchInput.value;
              searchData.classList.remove("active");
              searchData.innerHTML = "You just typed " + "<span style='font-weight: 500;'>" + values + "</span>";
            }else{
              searchData.textContent = "";
            }
          }
          cancelBtn.onclick =()=>{
            searchBox.classList.remove("active");
            searchBtn.classList.remove("active");
            searchInput.classList.remove("active");
            cancelBtn.classList.remove("active");
            searchData.classList.toggle("active");
            searchInput.value = "";
          }
        </script>
    
      </body>
    </html>

    Second, create a CSS file with the name of style.css and paste the given codes in your CSS file. Remember, you’ve to create a file with .css extension.

    @import url('https://fonts.googleapis.com/css?family=Poppins:400,500,600,700&display=swap');
    *{
      margin: 0;
      padding: 0;
      box-sizing: border-box;
      font-family: 'Poppins', sans-serif;
    }
    html,body{
      display: grid;
      height: 100%;
      place-items: center;
      background: #664AFF;
    }
    ::selection{
      color: #fff;
      background: #664AFF;
    }
    .search-box{
      position: relative;
      height: 60px;
      width: 60px;
      border-radius: 50%;
      box-shadow: 5px 5px 30px rgba(0,0,0,.2);
      transition: all 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55);
    }
    .search-box.active{
      width: 350px;
    }
    .search-box input{
      width: 100%;
      height: 100%;
      border: none;
      border-radius: 50px;
      background: #fff;
      outline: none;
      padding: 0 60px 0 20px;
      font-size: 18px;
      opacity: 0;
      transition: all 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55);
    }
    .search-box input.active{
      opacity: 1;
    }
    .search-box input::placeholder{
      color: #a6a6a6;
    }
    .search-box .search-icon{
      position: absolute;
      right: 0px;
      top: 50%;
      transform: translateY(-50%);
      height: 60px;
      width: 60px;
      background: #fff;
      border-radius: 50%;
      text-align: center;
      line-height: 60px;
      font-size: 22px;
      color: #664AFF;
      cursor: pointer;
      z-index: 1;
      transition: all 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55);
    }
    .search-box .search-icon.active{
      right: 5px;
      height: 50px;
      line-height: 50px;
      width: 50px;
      font-size: 20px;
      background: #664AFF;
      color: #fff;
      transform: translateY(-50%) rotate(360deg);
    }
    .search-box .cancel-icon{
      position: absolute;
      right: 20px;
      top: 50%;
      transform: translateY(-50%);
      font-size: 25px;
      color: #fff;
      cursor: pointer;
      transition: all 0.5s 0.2s cubic-bezier(0.68, -0.55, 0.265, 1.55);
    }
    .search-box .cancel-icon.active{
      right: -40px;
      transform: translateY(-50%) rotate(360deg);
    }
    .search-box .search-data{
      text-align: center;
      padding-top: 7px;
      color: #fff;
      font-size: 18px;
      word-wrap: break-word;
    }
    .search-box .search-data.active{
      display: none;
    }

    That’s all, now you’ve successfully created an Animated Search Box using HTML CSS & JavaScript. If your code doesn’t work or you’ve faced any error/problem then please comment down or contact us from the contact page.

    ]]>
    https://www.codingnepalweb.com/animated-search-box-javascript/feed/ 19
    Responsive Navbar with Search Box using HTML CSS & JavaScript https://www.codingnepalweb.com/responsive-navbar-with-search-box/ https://www.codingnepalweb.com/responsive-navbar-with-search-box/#comments Fri, 19 Jun 2020 12:44:00 +0000 https://codingnepalweb.com/2020/06/19/responsive-navbar-with-search-box-using-html-css-javascript/ Responsive Navbar with Search Box using HTML CSS & JavaScript

    Hello readers, Today in this blog, you’ll learn how to create a Fully Responsive Navbar with Search using HTML CSS & JavaScript. Earlier I have shared many blogs about Responsive Navigation Bar and Responsive Sidebar Menu but still, I have not shared any blog on Responsive Navbar with Search Box. So now, it’s time to create a Responsive Navbar with a Search Box.

    A navigation bar is a user interface element within a webpage that contains links to other parts of the website. A website navigation bar is most usually displayed as a horizontal list of links at the top of each page.

    Today in this blog, I’ll share with you this program (Responsive Navbar with Search Box). This navbar is fully responsive to any device. On mobile devices, this navbar will adjust its height-width automatically according to the device’s height-width. I used CSS Flex and @media property to make this Navbar Fully Responsive. The search box of this navbar is to create only for design purposes so, when you entered any info in the search bar and click on the search button, it won’t show you any results.

    If you’re feeling difficult to understand what I am saying. You can watch a full video tutorial on this program (Responsive Navbar with Search Box).

    Video Tutorial of Responsive Navbar with Search Box

     
    As you have seen in the video, this Navbar is fully Responsive for any device. If you’re a beginner in Web Designing then this tutorial helped you to understood the basic codes and concepts behind creating the Responsive Navbar and their Search Box.

    If you like this program (Responsive Navbar with Search Box) and want to get source codes. You can easily get the source codes of this program. To get the source codes you just need to scroll down. You can use this program on your projects and websites. If you know JavaScript perfectly then you can easily make this search box workable by adding some JavaScript codes.

    You might like this:

    Responsive Navbar with Search Box [Source Codes]

    To create this program (Responsive Navbar with Search Box). First, you need to create two Files one HTML File and another one is CSS File. After creating these files just paste the following codes in your file. First, create an HTML file with the name of index.html and paste the given codes in your HTML file. Remember, you’ve to create a file with .html extension.

    <!DOCTYPE html>
    <!-- Created By CodingNepal -->
    <html lang="en" dir="ltr">
       <head>
          <meta charset="utf-8">
          <title>Responsive Navbar with Search Box | CodingNepal</title>
          <link rel="stylesheet" href="style.css">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"/>
       </head>
       <body>
          <nav>
             <div class="menu-icon">
                <span class="fas fa-bars"></span>
             </div>
             <div class="logo">
                CodingNepal
             </div>
             <div class="nav-items">
                <li><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Blogs</a></li>
                <li><a href="#">Contact</a></li>
                <li><a href="#">Feedback</a></li>
             </div>
             <div class="search-icon">
                <span class="fas fa-search"></span>
             </div>
             <div class="cancel-icon">
                <span class="fas fa-times"></span>
             </div>
             <form action="#">
                <input type="search" class="search-data" placeholder="Search" required>
                <button type="submit" class="fas fa-search"></button>
             </form>
          </nav>
          <div class="content">
             <header class="space">Responsive Navbar with Search Box</header>
             <div class="space text">
                using HTML CSS & JavaScript
             </div>
          </div>
          <script>
             const menuBtn = document.querySelector(".menu-icon span");
             const searchBtn = document.querySelector(".search-icon");
             const cancelBtn = document.querySelector(".cancel-icon");
             const items = document.querySelector(".nav-items");
             const form = document.querySelector("form");
             menuBtn.onclick = ()=>{
               items.classList.add("active");
               menuBtn.classList.add("hide");
               searchBtn.classList.add("hide");
               cancelBtn.classList.add("show");
             }
             cancelBtn.onclick = ()=>{
               items.classList.remove("active");
               menuBtn.classList.remove("hide");
               searchBtn.classList.remove("hide");
               cancelBtn.classList.remove("show");
               form.classList.remove("active");
               cancelBtn.style.color = "#ff3d00";
             }
             searchBtn.onclick = ()=>{
               form.classList.add("active");
               searchBtn.classList.add("hide");
               cancelBtn.classList.add("show");
             }
          </script>
       </body>
    </html>

    Second, create a CSS file with the name of style.css and paste the given codes in your CSS file. Remember, you’ve to create a file with .css extension.

    @import url('https://fonts.googleapis.com/css?family=Montserrat:400,500,600,700&display=swap');
    *{
      margin: 0;
      padding: 0;
      outline: none;
      box-sizing: border-box;
      font-family: 'Montserrat', sans-serif;
    }
    body{
      background: #f2f2f2;
    }
    nav{
      background: #171c24;
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      justify-content: space-between;
      height: 70px;
      padding: 0 100px;
    }
    nav .logo{
      color: #fff;
      font-size: 30px;
      font-weight: 600;
      letter-spacing: -1px;
    }
    nav .nav-items{
      display: flex;
      flex: 1;
      padding: 0 0 0 40px;
    }
    nav .nav-items li{
      list-style: none;
      padding: 0 15px;
    }
    nav .nav-items li a{
      color: #fff;
      font-size: 18px;
      font-weight: 500;
      text-decoration: none;
    }
    nav .nav-items li a:hover{
      color: #ff3d00;
    }
    nav form{
      display: flex;
      height: 40px;
      padding: 2px;
      background: #1e232b;
      min-width: 18%!important;
      border-radius: 2px;
      border: 1px solid rgba(155,155,155,0.2);
    }
    nav form .search-data{
      width: 100%;
      height: 100%;
      padding: 0 10px;
      color: #fff;
      font-size: 17px;
      border: none;
      font-weight: 500;
      background: none;
    }
    nav form button{
      padding: 0 15px;
      color: #fff;
      font-size: 17px;
      background: #ff3d00;
      border: none;
      border-radius: 2px;
      cursor: pointer;
    }
    nav form button:hover{
      background: #e63600;
    }
    nav .menu-icon,
    nav .cancel-icon,
    nav .search-icon{
      width: 40px;
      text-align: center;
      margin: 0 50px;
      font-size: 18px;
      color: #fff;
      cursor: pointer;
      display: none;
    }
    nav .menu-icon span,
    nav .cancel-icon,
    nav .search-icon{
      display: none;
    }
    @media (max-width: 1245px) {
      nav{
        padding: 0 50px;
      }
    }
    @media (max-width: 1140px){
      nav{
        padding: 0px;
      }
      nav .logo{
        flex: 2;
        text-align: center;
      }
      nav .nav-items{
        position: fixed;
        z-index: 99;
        top: 70px;
        width: 100%;
        left: -100%;
        height: 100%;
        padding: 10px 50px 0 50px;
        text-align: center;
        background: #14181f;
        display: inline-block;
        transition: left 0.3s ease;
      }
      nav .nav-items.active{
        left: 0px;
      }
      nav .nav-items li{
        line-height: 40px;
        margin: 30px 0;
      }
      nav .nav-items li a{
        font-size: 20px;
      }
      nav form{
        position: absolute;
        top: 80px;
        right: 50px;
        opacity: 0;
        pointer-events: none;
        transition: top 0.3s ease, opacity 0.1s ease;
      }
      nav form.active{
        top: 95px;
        opacity: 1;
        pointer-events: auto;
      }
      nav form:before{
        position: absolute;
        content: "";
        top: -13px;
        right: 0px;
        width: 0;
        height: 0;
        z-index: -1;
        border: 10px solid transparent;
        border-bottom-color: #1e232b;
        margin: -20px 0 0;
      }
      nav form:after{
        position: absolute;
        content: '';
        height: 60px;
        padding: 2px;
        background: #1e232b;
        border-radius: 2px;
        min-width: calc(100% + 20px);
        z-index: -2;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
      }
      nav .menu-icon{
        display: block;
      }
      nav .search-icon,
      nav .menu-icon span{
        display: block;
      }
      nav .menu-icon span.hide,
      nav .search-icon.hide{
        display: none;
      }
      nav .cancel-icon.show{
        display: block;
      }
    }
    .content{
      position: absolute;
      top: 50%;
      left: 50%;
      text-align: center;
      transform: translate(-50%, -50%);
    }
    .content header{
      font-size: 30px;
      font-weight: 700;
    }
    .content .text{
      font-size: 30px;
      font-weight: 700;
    }
    .space{
      margin: 10px 0;
    }
    nav .logo.space{
      color: red;
      padding: 0 5px 0 0;
    }
    @media (max-width: 980px){
      nav .menu-icon,
      nav .cancel-icon,
      nav .search-icon{
        margin: 0 20px;
      }
      nav form{
        right: 30px;
      }
    }
    @media (max-width: 350px){
      nav .menu-icon,
      nav .cancel-icon,
      nav .search-icon{
        margin: 0 10px;
        font-size: 16px;
      }
    }
    .content{
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
    .content header{
      font-size: 30px;
      font-weight: 700;
    }
    .content .text{
      font-size: 30px;
      font-weight: 700;
    }
    .content .space{
      margin: 10px 0;
    }

    That’s all, now you’ve successfully created a Responsive Navbar with Search Box using HTML CSS & JavaScript. If your code doesn’t work or you’ve faced any error/problem then please comment down or contact us from the contact page.

    ]]>
    https://www.codingnepalweb.com/responsive-navbar-with-search-box/feed/ 50