Where to place a label for text field?

Short answer: Top-left corner INSIDE the text field box

MasaKudamatsu
Web Dev Survey from Kyoto
8 min readSep 17, 2020

--

A screen shot of the text fields in Line-height Picker (an app I’ve created)

Where to put the label text for a text field is a hotly debated topic in UI design. After reading various articles on the topic, I’ve come to conclude that it’s best to place it at the top left corner inside the field box (as shown in the above screenshot of a webapp that I’ve made).

Let me explain why, and also how to code it with CSS.

TL;DR

UI design research suggests:

  • The label text inside the field box ensures that the user can easily recognize the box as a text field.
  • Placing the label text at the top left corner avoids the text field from looking like the one with a default value already filled in.

To place the label text at the top-left corner inside the field box, style the following boilerplate HTML form code:

with these CSS declarations:

Why top-left and inside the box?

Google’s user tests on text field design

The starting point is the famous user tests on text field design conducted by Google’s design team in 2016–2017 (see Zaraysky et al. 2019 for detail). One of the main conclusions that came out of these user tests was:

“Enclosed text fields with a rectangular (box) shape performed better than those with a line affordance” — Zaraysky et al. (2019)

Famously, this finding has changed the text field design used by Google as part of its Material Design:

Google’s text field design before the 2016–2017 user tests. Image source: Zaraysky et al. (2019)
Google’s text field design after the 2016–2017 user tests. Image source: Zaraysky et al. (2019)

And there is another important finding from these user tests:

Label text should be placed within the bounds of the text field box” — Zaraysky et al. (2019)

Consequently, Google puts the label inside the field box. As demonstrated in the above GIF image, when the user clicks the text field, the label shrinks and moves to the top-left corner — a UI design pattern known as floating label—but still inside the field box.

Problems with the label inside the field box

However, Google’s design met some criticism from UI design experts. Nielsen-Norman Group, a well-known UI design research institute, criticises the floating label pattern (or the label text inside the field box in general):

“… fields with text in them are less noticeable, and users might think there is already a default value entered in the field.” — Sherwin (2014–2018)

For these reasons, they advocate the label outside the field.

Solution: Label at the Top-left inside the field box

How can we come to terms with these two contradicting views on text field design? My solution is: place the label at the top-left inside the field box by default, not only when the text field is active as Material Design does:

Material Design’s text field when it is active. Image source: Zaraysky et al. (2019)

The label at the top-left corner does look like a text field (what else can it be?), and it won’t look like a default value already entered in the text field. At the same time, the label is still inside the field box, helping the user recognize the box as a text field to click/tap, according to the user test results by Google.

Now the question is: how can we style a text field like this with CSS?

How to style the top-left label text

Material Design’s website has a demo for its text field design (Google undated). By inspecting this demo with Chrome DevTools, I’ve learned that Google uses the “relatively-absolute positioning” technique to place the label at the top-left inside the field box. Let me explain this step by step.

Step 1: Boilerplate HTML form code

Let’s start with the boilerplate HTML code for a text field:

An input element (and its associated label element) should always be wrapped with a form element for accessibility purposes (MDN Contributors 2020).

The above HTML code with the browser’s default styling gives you something like this:

How Chrome 85 on Mac OS 10.14 renders the above boilerplate HTML form code.

Step 2: Relatively-absolute positioning

The key for placing the label anywhere you want around the text field box is the “relatively-absolute positioning” technique (James 2017):

This pulls up the label element above the input element, and both elements will be placed at the top-left corner of the form element:

How Chrome 85 on Mac OS 10.14 renders the boilerplate HTML form code with the above CSS code.

This relatively-absolute positioning technique is very useful in many occasions. The introductory CSS tutorial, however, fails to mention it when it introduces position: absolute even though that’s pretty much the only occasion we actually use position: absolute. I myself learned this technique from James (2017) and since then I’ve used it quite often.

Step 3: Placing the label text at the top left corner

Then you can enlarge the text field with font-size so that the label text appears at the top-left inside the text field box:

How Chrome 85 on Mac OS 10.14 renders the boilerplate HTML form code with the above CSS code.

Of course, the label should be positioned a bit more inside the field box for legibility. To do so, use the top and left properties for label:

How Chrome 85 on Mac OS 10.14 renders the boilerplate HTML form code with the above CSS code.

Step 4: Further styling

Then style the text field as you wish. But I believe that the following three considerations are the minimum requirements.

Border-radius: First, make the field box corners rounded by adding the border-radius property to the input element. Remember that yet another conclusion from Google’s user tests (mentioned above in this article) is:

“Text fields should have rounded corners.” — Zaraysky et al. (2019)

In our particular case, the value of 6px looks nice.

Padding-top: Second, add padding-top to the input element, to avoid the overlapping of the label with the user-entered text. In this particular case, 1.3rem makes the entered text center-aligned vertically between the bottom of the label text and the bottom border of the field box.

Padding-left: Third, add padding-left to the input element, to left-align the label with the user-entered text. Left alignment visually ties the label text with the user-entered text, creating a user experience that it feels like entering what the label text refers to indeed.

Note that the same value as the label element’sleft property does not work for two reasons.

The first reason is that the left property of the label element calculates the distance from the left edge of the form element while the padding-left of the input element measures the distance from the border (which is 2px wide by default with Chrome). So use thecalc() function to subtract 2px (or whatever border width you prefer) from the padding-left value.

The second reason for misalignment is that, since we set the input element’s font-size 3 times larger than the label element, the left-edge of text does not visually align with each other. The larger the text, the more whitespace in front of the first character. So we need to manually adjust the padding-left value. In this particular case, subtracting 1px more will work.

To summarize, we need to subtract 3px in total from the value of the left property.

So we have:

which gives us:

How Chrome 85 on Mac OS 10.14 renders the boilerplate HTML form code with the above CSS code, with my name Masa is entered in the field box

You can see the CodePen demo of this example.

Of course, this text field design can be improved further. But I believe that the above CSS code covers the absolutely minimum requirements for good UI design on text fields.

This article is part of Web Dev Survey from Kyoto, a series of my blog posts on web development. It intends to simulate that the reader is invited to Kyoto, Japan, to attend a web dev conference. So the article ends with a photo of Kyoto in the current season, as if you were sightseeing after the conference was over.

So let me take you to Jingo-ji Temple:

The 17th-century gate of Jingo-ji Temple in the middle of the mountain to the north-west of Kyoto City. Photographed by Masa Kudamatsu (the author of this article) on 18 September, 2016.

This Buddhist temple, originally built in the early 9th century, is located in the middle of the mountain. Visitors need to walk up the hill quite a bit before arriving the gate pictured above. It seems to me that the magnificent gate symbolizes (and rewards) the efforts you’ve made to reach the temple.

References

Google (undated) “Text fields”, Material Design, undated.

James, Oliver (2017) “Advanced Positioning”, Interneting Is Hard, 2017.

MDN Contributors (2020) “How to structure a web form”, MDN Web Docs, May 20, 2020.

Sherwin (2014–2018) “Placeholders in Form Fields Are Harmful”, Nielsen Norman Group, May 11, 2014 (and updated on Sep. 10, 2018).

Zaraysky, Susanna, Michael Gilbert, Dave Chiu, and Sameer Bansal (2019) “The Evolution of Material Design’s Text Fields”, Google Design, Nov. 1, 2019.

--

--

MasaKudamatsu
Web Dev Survey from Kyoto

Self-taught web developer (currently in search of a job) whose portfolio is available at masakudamatsu.dev