Back to CSS Search Boxes Clear-on-Type Pure CSS
Share
HTML
<form class="csb-clear" role="search">
  <label class="csb-sr" for="csb-clear-i">Search</label>
  <input id="csb-clear-i" type="search" name="q" placeholder="Type something..." />
  <button type="reset" class="csb-clear-x" aria-label="Clear search"></button>
</form>
CSS
.csb-clear {
  display: inline-flex; align-items: center;
  padding: 4px 6px 4px 14px;
  background: #1a1a28;
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 10px;
}
.csb-clear:focus-within { border-color: #7c6cff; }
.csb-clear input {
  width: 220px; padding: 8px 0;
  border: 0; outline: none; background: transparent;
  color: #f0eeff; font: 500 14px/1 system-ui, sans-serif;
  -webkit-appearance: none; appearance: none;
}
.csb-clear input::-webkit-search-decoration,
.csb-clear input::-webkit-search-cancel-button { display: none; }
.csb-clear input::placeholder { color: #b8b6d4; }
.csb-clear-x {
  width: 24px; height: 24px;
  border: 0; cursor: pointer;
  background: rgba(255,255,255,0.06);
  border-radius: 50%;
  position: relative;
  margin-left: 6px;
  opacity: 0; transform: scale(0.6);
  transition: opacity 0.2s, transform 0.2s;
}
.csb-clear input:not(:placeholder-shown) ~ .csb-clear-x {
  opacity: 1; transform: scale(1);
}
.csb-clear-x::before, .csb-clear-x::after {
  content: ''; position: absolute;
  top: 50%; left: 50%;
  width: 9px; height: 1.5px;
  background: #f0eeff; border-radius: 1px;
}
.csb-clear-x::before { transform: translate(-50%,-50%) rotate(45deg); }
.csb-clear-x::after  { transform: translate(-50%,-50%) rotate(-45deg); }
.csb-clear-x:hover { background: rgba(255,108,138,0.2); }

.csb-sr {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}