Dropdown Search Box with HTML, CSS & JavaScript

03/05/2021

Contents

Demo

Full Screen

Code

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Dropdown Search Box</title>
    <link rel="stylesheet" type="text/css" href="https://demo.plantpot.works/assets/css/normalize.css">
    <link rel="stylesheet" href="https://use.typekit.net/opg3wle.css">
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>
    <div id="container">
      <div class="search-box">
        <input class="search-form-input" type="search" name="search" placeholder="Search...">
        <details id="dropdown" class="dropdown">
          <summary class="dropdown-button">
            <span id="selected-item" class="selected-item">All</span>
          </summary>
          <details-menu class="dropdown-menu">
            <div class="dropdown-menu-header">Select language</div>
            <div class="dropdown-menu-list">
              <label class="dropdown-menu-item">
                <input type="radio" name="item" value="All" checked>
                <span>All</span>
              </label>
              <label class="dropdown-menu-item">
                <input type="radio" name="item" value="HTML">
                <span>HTML</span>
              </label>
              <label class="dropdown-menu-item">
                <input type="radio" name="item" value="CSS">
                <span>CSS</span>
              </label>
              <label class="dropdown-menu-item">
                <input type="radio" name="item" value="JavaScript">
                <span>JavaScript</span>
              </label>
            </div>
          </details-menu>
        </details>
      </div>
    </div>
  </body>
</html>

CSS

@charset "utf-8";
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
html {
  font-size: 16px;
}
body {
  background-color: #cff3f3;
  font-family: futura-pt, sans-serif;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
}
#container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  width: 100%;
  height: 100vh;
  padding: 15px;
}
.search-box {
  display: flex;
  width: 450px;
}
.search-form-input {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  width: 75%;
  height: 46px;
  padding: 5px 20px 5px 40px;
  border: none;
  border-radius: 23px 0 0 23px;
  background-image: url(ic_search.svg);
  background-position: 15px center;
  background-size: 18px;
  background-repeat: no-repeat;
  background-color: #fff;
  color: #3d3935;
  font-size: 1.25rem;
  outline: none;
}
.dropdown {
  position: relative;
  width: 25%;
  height: 46px;
}
.dropdown::before {
  position: absolute;
  top: 5px;
  left: 0;
  z-index: 999;
  width: 1px;
  height: 36px;
  background-color: #ccc;
  content: '';
}
.dropdown-button {
  list-style: none;
  position: relative;
  width: 100%;
  height: 100%;
  padding: 15px;
  border-radius: 0 23px 23px 0;
  background-color: #fff;
  color: transparent;
  outline: none;
  cursor: pointer;
}
.dropdown-button::-webkit-details-marker {
  display:none;
}
.dropdown-button .selected-item {
  color: #3d3935;
  font-size: 1rem;
}
.dropdown[open] .dropdown-button::before {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  content: '';
}
.dropdown-button:after {
  position: absolute;
  top: 17px;
  right: 20px;
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
  width: 6px;
  height: 6px;
  border-right: 2px solid #aaa;
  border-bottom: 2px solid #aaa;
  content: '';
}
.dropdown-button:hover:after {
  border-right: 2px solid #22A7B4;
  border-bottom: 2px solid #22A7B4;
}
.dropdown-menu {
  position: absolute;
  top: 55px;
  right: 5px;
  width: 160px;
  padding: 10px;
  font-size: 1rem;
  border-radius: 4px;
  box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, .1);
  background-color: #fff;
}
.dropdown-menu-header {
  padding-bottom: 4px;
  border-bottom: 1px solid #ccc;
}
.dropdown-menu-list {
  display: flex;
  flex-direction: column;
  margin-top: 5px;
}
.dropdown-menu-item {
  width: 100%;
  padding: 5px;
  cursor: pointer;
}
.dropdown-menu-item input:checked + span,
.dropdown-menu-item:hover {
  color: #22a7b4;
}
.dropdown-menu-item input {
  display: none;
}
@media screen and (max-width: 480px) {
  #container {
    height: 400px;
    align-items: flex-start;
  }
  .box {
    width: 280px;
  }
  .search-box {
    display: flex;
    flex-direction: column;
  }
  .search-form-input {
    width: 100%;
    border-radius: 23px;
  }
  .dropdown {
    width: 100%;
    margin-top: 10px;
  }
  .dropdown::before {
    display: none;
  }
  .dropdown-button {
    border-radius: 23px;
  }
  .dropdown-menu {
    left: 5px;
    width: calc(100% - 10px);
  }
}

JavaScript


var items = document.getElementsByName('item');
var selectedItem = document.getElementById('selected-item');
var dropdown = document.getElementById('dropdown');

items.forEach(item => {
  item.addEventListener('change', () => {
    if (item.checked) {
      selectedItem.innerHTML = item.value;
      dropdown.open = false;
    }
  });
});