Wednesday, June 23, 2021

How to Recreate Material Design Floating Labels

How to Recreate Material Design Floating Labels

When it comes to creating forms on webpages, accessibility and design have to go hand in hand. Ideally, every input field should have an associated label and a placeholder, if needed. However, from a design perspective, this can cause a form to look rather text-heavy.

Labels-as-placeholders, sometimes known as floating labels, are a popular design choice for creating minimalist and accessible forms. This method is also commonly used in popular design systems such as Bootstrap and Material Design (shown below):

Bootstrap floating labelsBootstrap floating labelsBootstrap floating labels
Bootstrap floating labels
Material Design floating label optionsMaterial Design floating label optionsMaterial Design floating label options
Floating label options on a Material Design form

In this tutorial, we’ll recreate the floating label used in Material Design text fields using CSS and vanilla JavaScript.

Material Design Floating Label

Here’s what the final result looks like:

Begin With Form HTML Markup

First, we create the markup for our Material Design form including a label and an input text field. 

We won’t actually be using the Material Design library for markup or styling, rather we’ll be recreating the appearance and behavior ourselves.

It's important to associate labels to their related input fields by using the for attribute and the id of the input. This is used by screen readers to communicate the details of the form.

Using the DevTools Accessibility pane, we can see how labels and inputs are linked in the form markup above:

Accessibility panel in Dev tools showing the textbox "First Name"Accessibility panel in Dev tools showing the textbox "First Name"Accessibility panel in Dev tools showing the textbox "First Name"

Material Design Form CSS

The most important styling for a floating label is to make the label absolutely positioned inside a relative parent element. We want to be able to move our label around the input container without it disrupting the flow of elements.

Float Label on Focus

We also want to float the label whenever the user clicks the input. We can do this using the :focus and + (plus) selector. When the input is focused on, we change the position, size and color of the label.

Note: This works because the label is the next element immediately after the input field. The plus selector targets the next immediate element so it won't work if the label is placed before the input or another element is placed in between the two.

Float Label with User Input

Another feature of the floating label is that it remains floated if the input has a value, even if the user is no longer focusing on the input.

We can achieve this by using the CSS :valid selector and the required attribute in the HTML markup

We use the valid selector for required inputs to detect if the input has a value. 

With this, the label remains floated when the input is focused or has a value.

Unfortunately, there are some drawbacks to using the valid selector. For example, if the valid selector is used on an email input, the input is considered invalid if the value is not in email format. 

invalid email inputinvalid email inputinvalid email input
Ouch

We can solve this with a little JavaScript.

Add JavaScript Event Listener

We use JavaScript to detect if the input has a value whenever the user navigates away from it.

Whenever the input loses focus, we check if it contains a value. If it does, we add the is-valid class, if not we remove the valid class. We can update our CSS styling to float the label with the is-valid class.

With this we’ve implemented the floating label design!

Here’s the complete CSS to recreate the Material Design form look.

Your Material Design Floating Labels Are Complete!

As with most design patterns, floating labels have triggered their fair share of debates. So long as you make your forms accessible and easy to use, you can use any design pattern you like. Hopefully, this tutorial serves as a useful guide should you choose to use floating labels.

More Form Design Tutorials

Learn more about form design patterns, floating labels, Material Design forms, and form accessibility here on Tuts+:


No comments:

Post a Comment