Tuesday, 10 October 2017

How to make a dropdown button and search bar together.

In this blog we will see how to make a dropdown button and search bar together in html through css,you can do so by following the given codes below.

Step1 : Go to notepad and type the following given codes below.



 <!DOCTYPE html>
 <html>
 <head>
 <style>
 .dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
 }

 .dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
 }
 #mySearch {
    border-box: box-sizing;
    background-image: url('searchicon.png');
    background-position: 14px 12px;
    background-repeat: no-repeat;
    font-size: 16px;
    padding: 14px 20px 12px 45px;
    border: none;
 }

 .dropdown {
    position: relative;
    display: inline-block;
 }

 .dropdown-content {
    display: none;
    position: absolute;
    background-color: #f6f6f6;
    min-width: 230px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
 }

 .dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
 }

 .dropdown a:hover {background-color: #ddd}

 .show {display:block;}
 </style>
 </head>
 <body>

 <h2 text align="center">Search and Dropdown button</h2> 
 <p> Click the dropdown button to see the menu, and use the search   bar to filter. </p>

 <div class="dropdown">
 <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <input type="text" placeholder="Search.." id="mySearch"   onkeyup="filterFunction()">
    <a href="#about">About</a>
    <a href="#base">Base</a>
    <a href="#blog">Blog</a>
    <a href="#contact">Contact</a>
    <a href="#services">Service</a>
  </div>
 </div>

  <script>
  /* When the user clicks on the button,
  toggle between hiding and showing the dropdown content */
  function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
 }

 function filterFunction() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    div = document.getElementById("myDropdown");
    a = div.getElementsByTagName("a");
    for (i = 0; i < a.length; i++) {
        if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
            a[i].style.display = "";
        } else {
            a[i].style.display = "none";
         }
     }
 }
 </script>

 </body>
 </html>


Step 2: Save the file in .html extension and run.

Share:

0 comments:

Post a Comment