---
title: "Building a Sleek, Animated Form with CSS (Tutorial)"
summary: "A step-by-step guide to building an animated form with simple CSS."
date: 2015-09-01
authors: [robert-campion]
topics: [Frontend]
canonical: https://product.hubspot.com/blog/how-to-build-a-sleek-animated-input-form-with-css
cover:
  scene: how-to-build-a-sleek-animated-input-form-with-css
  alt: "Two cardboard boxes with a cat asleep in each; as an address goes onto the first box, its cat hops up onto the shelf above and curls up in orange, flops back down over the address once, then hops up for good."
---

![The Website Grader homepage asking “How strong is your website?”, with two underlined fields, Website and Email, and a Get your answer button.](/assets/blog/how-to-build-a-sleek-animated-input-form-with-css/wsg-screenshot.png)

When we set out to build the new and improved [Website Grader](https://website.grader.com), we wanted something that would be slick, speedy, and powerful. First impressions are everything so creating that look and feel on the homepage was paramount. A key component in making that happen was building an engaging form (the two input fields where users enter their website and email address). This form is the only interactive component on the homepage, and more importantly, the only way users can start using the tool.

So, to get that quick and clean feel, we ended up building an animated form. Here's why. Most forms look like the one below with blank input fields and corresponding labels like 'First Name' and 'Last Name'. These work well but the fields and labels remain pretty static as users enter their information and navigate through the form.&#x20;

![A conventional web form: five empty fields, First name, Last name, Email, Website URL and Phone number, each with its label above it.](/assets/blog/how-to-build-a-sleek-animated-input-form-with-css/hubspot-form.webp)

Using placeholder text instead of labels (like 'Email address' and 'Password' in the example below) creates a slightly more dynamic experience since the prompts disappear when users start typing. But this approach still isn't ideal. Even though using placeholder text instead of labels makes forms shorter, it can also make it harder for users to navigate through the form.&#x20;

![Animated GIF of a sign-in form whose Email address and Password placeholders vanish as someone types, leaving the fields with no labels.](/assets/blog/how-to-build-a-sleek-animated-input-form-with-css/placeholder-input.gif)

We considered the benefits of both approaches and decided to build our own animated form for Website Grader (below). In this form, the input fields contain text placeholders at first and then when a user clicks on a field, that text transforms into a label.&#x20;

![Animated GIF of the floating-label form: clicking a field slides its placeholder up into a small label, which stays put as a name is typed.](/assets/blog/how-to-build-a-sleek-animated-input-form-with-css/wsg-input-form.gif)

## Building Your Own Animated Form

We used an animated label to first act as a placeholder and then transition into a label when required (e.g. when the input element contains a value). Here's a step-by-step tutorial so you can create a dynamic form like the one above:&#x20;

## Step 1

First thing's first: we need a label and an input field (let's worry about styling later):

[see it on CodePen](https://codepen.io/robcampo/pen/EjqaLR/)

Notice how the label actually wraps the input field. With this method, we no longer have to rely on the HTML ‘for’ attribute. If we click anywhere inside the label (specifically, on the div element that contains the label text), focus will switch to the input field automatically. You'll also notice how the label-text element is after the input field in the DOM. This is so we can take advantage of the [CSS adjacent sibling combinator](https://css-tricks.com/almanac/selectors/a/adjacent-sibling/) to select our new “label” (this will be useful when the input field is active).

## Step 2

Next, let's move the label text over the input field and then move it above when the input has focus:

[see it on CodePen](https://codepen.io/robcampo/pen/OVKPrM/)

At first, the label looks like a standard text placeholder. But when focus shifts to the input field, the text moves above it and acts as a label. Simply put, we use the CSS style '`transform: translateY`' to move the position of the text vertically.

## Step 3

Font plays an important role in making any application sleek, so let’s clean that up a bit:

[see it on CodePen](https://codepen.io/robcampo/pen/jPgPba/)

Now, the font size will change from large placeholder text to small label text when the input has focus. We’ve also increased the letter spacing and forced uppercase on the label. Let's use Google Font’s [Open Sans](https://www.google.com/fonts#UsePlace:use/Collection:Open+Sans) to punch up the text a bit, too.&#x20;

## Step 4

Now, the input field border interferes with the label which looks a little odd. Let’s spruce that up:

[see it on CodePen](https://codepen.io/robcampo/pen/GJVJoZ/)

Most of the changes here are style related. To summarize, we:

- Added SASS color variables (see why the naming convention was chosen [here](http://davidwalsh.name/sass-color-variables-dont-suck))
- Darkened the body background color&#x20;
- Added ‘`cursor:text`’ to the label to show that clicking it will allow the user to enter text
- Removed the input border except for at the bottom, where we increased it
- Increased overall input size using padding and font
- Expanded the width and changed the label color of the input field when it has focus

## Step 5

You might notice that the label jumps during transformation from one state to another, and so does the input width. We can fix that by using CSS transitions:

[see it on CodePen](https://codepen.io/robcampo/pen/doxoVY/)

The interaction with the input field should feel a lot smoother now. For simplicity, I applied ‘`transition: all 0.3s`’ to both the input and the label text, but you can target specific properties and change the timings however you please ([example](https://developer.mozilla.org/en/docs/Web/CSS/transition)).

## Step 6

We're almost there. One issue still remains, though. If a value is entered and focus is taken off the input field, the label returns to its original position covering the inputted text:

![A First name field with the typed name Rob printed over its First name label.](/assets/blog/how-to-build-a-sleek-animated-input-form-with-css/wsg-label-over-input.png)

We need a way to detect if a value exists and if it does, stop the label from moving over the input field. Unfortunately there isn't a clean CSS solution; there is, however, a solution. By adding a ‘required’ attribute to the input field, we can utilize the CSS ‘`:valid`’ pseudo class. That way, whenever a value exists in the text field, it will appear valid and we can trigger some different styling:

[see it on CodePen](https://codepen.io/robcampo/pen/WvVvdM/)

There are cases where this solution doesn’t work and in those cases you can use JavaScript to listen to certain events (e.g. onblur) and act accordingly (by adding specific CSS classes - see an example [here](http://codepen.io/robcampo/pen/XmrEJQ)).

## Step 7

To complete the component, let’s wrap everything up in a form, add another labeled input field, and include a clean call-to-action:

[see it on CodePen](https://codepen.io/robcampo/pen/ZGgGow/)

Now we have a sleek, animated form with responsive fields. If you have any questions about the tutorial, let us know in the comments and if you want to read more about [Website Grader](https://website.grader.com/#/) and why we built it, check out [this post](http://blog.hubspot.com/marketing/website-grader-relaunch).

_*Thank you to Brad Frost (whose [floating label design](http://bradfrost.com/blog/post/float-label-pattern/) was a big inspiration), Hugo Welke (the talented HubSpot designer who brought Website Grader to life), and Francisco Dias (our resident CSS maestro and the peer-reviewer on this post).&#x20;*_
