Layout · 17 of 23
Search Expand
Width expansion on focus
<div class="search-box">
<button class="search-btn">🔍</button>
<input type="text" class="search-input" placeholder="Search...">
</div>
/* Collapsed width must equal padding + button, so the icon lands dead centre.
An unshrinkable margin on the input would still take part in the flex line and
push the button off-centre inside the 40px pill. */
.search-box {
display: flex;
align-items: center;
background: #222;
border-radius: 20px;
padding: 4px;
width: 40px; /* collapsed: just the icon */
transition: width 0.4s cubic-bezier(0.16, 1, 0.3, 1);
overflow: hidden;
}
/* :focus-within expands the box when the input (or button) has focus */
.search-box:focus-within {
width: 240px;
}
.search-btn {
flex: 0 0 32px;
width: 32px;
height: 32px;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
background: none;
border: none;
border-radius: 50%;
font-size: 1rem;
line-height: 1;
cursor: pointer;
}
.search-input {
/* flex-basis 0 + min-width 0 lets it collapse fully; padding replaces the margin
so nothing unshrinkable is left in the line when collapsed */
flex: 1 1 0;
min-width: 0;
background: transparent;
border: none;
color: #fff;
outline: none;
padding: 0 0.5rem;
opacity: 0;
transition: opacity 0.2s;
}
.search-box:focus-within .search-input {
opacity: 1;
}