Course contentsShow
← Web Development
Lesson 308 of 2,9825. FlexboxFree lesson

Input Groups

Combine inputs with buttons using flexbox for seamless input groups.

Input Groups

What You'll Learn

You'll learn how to combine text inputs with buttons or icons into a single, cohesive visual unit using flexbox.

What Are Input Groups?

An input group is a common UI pattern where an input field and one or more buttons (or icons) are displayed together as if they're a single component. Think of a search bar with a magnifying glass button attached, or an email subscription field with a "Subscribe" button right next to it—no gap, no separation.

Without flexbox, creating seamless-looking input groups required tricky margins, floats, or inline-block elements that often broke at different screen sizes. Flexbox makes this pattern straightforward and reliable.

How It Works

Create a wrapper container and set it to display: flex. Place your <input> and <button> elements inside. They'll automatically sit side-by-side with no gaps (unless you add them). You can control how much space the input takes versus the button using flex-grow, or let the button size itself naturally while the input fills remaining space.

.input-group {
  display: flex;
}

.input-group input {
  flex: 1; /* Input takes all available space */
  border-right: none; /* Remove shared border */
}

.input-group button {
  /* Button sizes to its content */
}
<div class="input-group">
  <input type="text" placeholder="Enter email">
  <button>Subscribe</button>
</div>

The input stretches to fill space (flex: 1), while the button stays compact. You typically remove borders where elements meet and apply shared border-radius to the outer edges for a polished look.

Analogy

Think of puzzle pieces locking together: the input and button fit seamlessly without visible seams, creating one unified shape.

Key Takeaway: Input groups use flexbox to combine inputs and buttons into seamless, professional-looking components where the input grows to fill space and elements visually merge together.