<div class="search-wrapper">
<input type="search" placeholder="Search" />
<button class="search-btn">
<i id="search-icon" class="fa-solid fa-magnifying-glass"></i>
</button>
</div>
body {
display: flex;
align-items: center;
justify-content: center;
background: #fff;
min-height: 100vh;
}
.search-wrapper {
width: 50px;
height: 50px;
display: flex;
border-radius: 4rem;
background: #fff;
align-items: center;
justify-content: center;
outline: none;
border: 3px solid #888;
overflow: hidden;
transition: 400ms ease-in-out;
position: relative;
}
.active {
width: 350px;
}
.search-wrapper input {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
margin-left: 40px;
margin-right: 2px;
outline: none;
border: none;
font-size: 1.2rem;
box-sizing: border-box;
}
.search-btn {
display: flex;
align-items: center;
justify-content: center;
font-size: 2rem;
border-radius: 50%;
border: 2px solid white;
width: 50px;
height: 50px;
cursor: pointer;
background: #e66610;
margin-left: auto;
z-index: 1;
}
.search-btn i {
color: #fff;
font-size: 1rem;
}
const search = document.querySelector(".search-wrapper");
const input = search.querySelector("input");
search.addEventListener("mouseenter", () => {
if (!input.matches(":focus")) {
search.classList.add("active");
}
});
search.addEventListener("mouseleave", () => {
if (!input.matches(":focus") && !input.value.trim()) {
search.classList.remove("active");
}
});
This Pen doesn't use any external CSS resources.