The Spirit Bear

Written by Gray Gilmore

I make websites for a living. I love CSS and building simple and modular stylesheets. Somewhere along the way I also turned into a Ruby developer. Life comes at you fast I guess. You can read my resume to learn about my journey so far.

You can find me as @graygilmore on several platforms:

Good ol' fashion email works, too:

You can subscribe to this blog via RSS: XML, JSON

Simple inline signup forms with flexbox

One of the most requested features from designers is a text input that has a submit button beside it. It’s a simple request but has always been a pain to implement.

Designs like this are great in a pixel-perfect world of a visual editor but when it comes to the web there are too many variables at play. Has the user increased their font size? Decreased it? What device are they using? Does our design consider screen width?

Getting the input and the button to appear side by side is trivial. You can float them. You can inline-block them both. You can absolutely position them. There are lots of different paths but they all have the same fallback: magic numbers. At some point you’re going to have to hardcode a special number in your CSS to make it all work (and if your site supports internationalization you’re going to have many magic numbers).

Avoiding Magic Numbers

Have no fear, our friend flexbox is here. Flexbox is a new(ish) CSS tool that allows us to hand over some layout components to the browser. In simple English, what we want to tell the browser is: “Put these two items side by side. Let the button be as big as it needs to be to fit the call to action and let the text input fill the remaining space.” Demo time!

Let’s say we have some markup that looks like this:

<div class="mini-signup-form">
  <input type="email">

We can achieve our goal with a few simple lines of CSS:

.mini-signup-form {
  display: flex;

input[type="text"] {
  flex-grow: 1;

Bam. Done. Over. Inspect the example embedded below and change the submit button to a longer word and you’ll see everything automagically adjust.