JSFiddle - React, Tailwind, and code Playground
by Sebastian Kay
HTML
<div class="container">
<form action="https://www.google.com/search" class="search" id="search-bar">
<input type="search" placeholder="Type something..." name="q" class="search__input">
<div class="search__button" id="search-button">
<i class="ri-search-2-line search__icon"></i>
<i class="ri-close-line search__close"></i>
</div>
</form>
</div>
CSS
/*=============== GOOGLE FONTS ===============*/
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@400;500&display=swap");
/*=============== VARIABLES CSS ===============*/
:root {
/*========== Colors ==========*/
/*Color mode HSL(hue, saturation, lightness)*/
--first-color: hsl(222, 80%, 58%);
--dark-color: hsl(222, 24%, 8%);
--title-color: hsl(222, 8%, 8%);
--text-color-light: hsl(222, 8%, 65%);
--white-color: #fff;
--body-color: hsl(222, 100%, 99%);
/*========== Font and typography ==========*/
/*.5rem = 8px | 1rem = 16px ...*/
--body-font: "Poppins", sans-serif;
--small-font-size: .813rem;
}
@media screen and (min-width: 968px) {
:root {
--small-font-size: .875rem;
}
}
/*=============== BASE ===============*/
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
background-color: var(--body-color);
}
/*=============== SEARCH ===============*/
.container {
height: 100vh;
margin-inline: 1.5rem;
display: grid;
place-items: center;
}
.search {
position: relative;
width: 76px;
height: 76px;
background-color: var(--white-color);
box-shadow: 0 4px 24px hsla(222, 68%, 12%, 0.1);
border-radius: 4rem;
padding: 10px;
overflow: hidden;
transition: width 0.5s cubic-bezier(0.9, 0, 0.3, 0.9);
}
.search__input {
border: none;
outline: none;
width: calc(100% - 64px);
height: 100%;
border-radius: 4rem;
padding-left: 14px;
font-family: var(--body-font);
font-size: var(--small-font-size);
font-weight: 500;
opacity: 0;
pointer-events: none;
transition: opacity 1.5s;
}
.search__input:-webkit-autofill {
box-shadow: 0 0 0 100px var(--white-color) inset;
}
.search__button {
width: 56px;
height: 56px;
background-color: var(--dark-color);
border-radius: 50%;
position: absolute;
top: 0;
bottom: 0;
right: 10px;
margin: auto;
display: grid;
place-items: center;
cursor: pointer;
transition: transform 0.6s cubic-bezier(0.9, 0, 0.3,...
JavaScript
/*=============== SEARCH BAR JS ===============*/
const toggleSearch = (search, button) =>{
const searchBar = document.getElementById(search),
searchButton = document.getElementById(button)
searchButton.addEventListener('click', () =>{
// We add the show-search class, so that the search bar expands
searchBar.classList.toggle('show-search')
})
}
toggleSearch('search-bar', 'search-button')