Input Add-ons

Because of the way input sizing works in CSS, it’s almost impossible to append or prepend another element to it and have the input field behave fluidly and take up the remaining space.

The only existing way to do this is to either know the exact width of the input, or to use something like display:table-cell, which has its own set of problems, most notably the difficulty with positioning anything absolutely inside of the add-on in certain browsers.

With Flexbox, all these problems go away, and the code is trivially simple. In addition, you get the input field and the input add-on to be the same height for free.

Add-on Prepended

Amount

Add-on Appended

Appended and Prepended Add-ons

The HTML

<!-- appending -->
<div class="InputAddOn">
  <input class="InputAddOn-field">
  <button class="InputAddOn-item"></button>
</div>

<!-- prepending -->
<div class="InputAddOn">
  <span class="InputAddOn-item"></span>
  <input class="InputAddOn-field">
</div>

<!-- both -->
<div class="InputAddOn">
  <span class="InputAddOn-item"></span>
  <input class="InputAddOn-field">
  <button class="InputAddOn-item"></button>
</div>

The CSS

.InputAddOn {
  display: flex;
}

.InputAddOn-field {
  flex: 1;
  /* field styles */
}

.InputAddOn-item {
  /* item styles */
}

View the full source for the InputAddOn component used in this demo on Github.