HTML Forms: Label Element Without Text

Do we still need a label element for an input element if we don’t want a text associated with the latter?

I’ve added my code without a label element.

<section id="insider-program">
  <h2>Keep up with History!</h2>
  <div>Join the Age Insider Program to keep up on all Age of Empires titles.</div>

  <form action="https://www.freecodecamp.com/email-submit" method="post"bid="form">
    <input type="email" name="email" id="email" placeholder="Your email" required />
    <input type="submit" value="Sign Up" id="submit" />
  </form>
</section>

Yes, that needs a label. What is the user supposed to input? A username? Email? Phone number?

Perhaps placeholder is the compromise.

@lionel-rowe would I still need a label even if I added a placeholder text that describes what the input field is?

Already added a placeholder. I just wanna know the ‘best practice’ regarding the use of label elements without a text.

Your image doesn’t have a placeholder.

There’s really no best practice., it’s really a design thing. You never want your user to be guessing what to insert, otherwise you risk them just leaving page.

One of the main benefits for labels are for checkboxes and radio buttons. Labels also allow a user to click the label themselves which places the cursor inside their respective input box.

Use them - labels greatly help accessibility

Don’t use placeholders as replacements for labels. A placeholder should only ever contain supplementary (nonessential) information, such as format. For example:

<label for="email">
  Your email address
</label>
<input
  type="email"
  name="email"
  id="email" 
  placeholder="yourname@example.com"
>

I completely disagree with @lionel-rowe .

Go log into gmail.

Instagram

Twitter

Need more examples?

I’m pretty close to building (if not already) over 100 websites professionally. Trust me on this., it’s completely a design choice.

1 Like

Doing something a certain way 100 times doesn’t make it the best way to do it. For that matter, even big companies doing something doesn’t make it the best way to do it.

With that said… have you tried examining the user interactions and viewing the DOM implementation for all of the examples you posted? In the Gmail one for example, no placeholder attribute is used at all. No label element is used either, but they use the aria-label attribute as a substitute (which is valid). Finally, what looks like a placeholder is actually implemented as a div with aria-hidden.

The interaction from the user point of view is also different: the text remains visible (moves above the input) when focused or when user input is present.

See this article for more details about why the naive placeholder implementation is bad from a UX/accessibility point of view:

@lionel-rowe

So you’re comparing “Nielsen Norman Group”'s suggestions to what Google, Twitter, Instagram, Yahoo, Reddit, LinkedIn, Ebay, Netflix, PayPal implement on their own sites?

Come on dude. I’m not even naming all of them…

My entire argument is that it’s a design choice.

How can something be confusing if most people are already using it?

Yes. None of those companies are immune to criticism. Nor am I saying that all of them do it wrong (e.g. in the Gmail case, it looks like the implementation has no problems).

As I mentioned, I’m talking about markup/DOM implementation and user interactions rather than the color or placement of text. Color/placement is generally a pure design choice, though it can also have UX/a11y considerations (e.g. if insufficient contrast is used).

Poor design can propagate due to becoming enshrined in convention. Just look at the US date format (MM/DD/YYYY), or to a lesser extent, any other date format that isn’t YYYY-MM-DD,

1 Like

And if you return to the original discussion., the OP has one-time field.

So short-term memory (described in your article) is not a problem. The user wouldn’t be updating the field at a later date in time. It’s simply a signup form.

I see nothing wrong with the US date format. Everyone says our country’s formats, measurements are all wrong, yet we have the strongest military, and largest economy in the world.

If you knew US history behind date formats., you might get why the USA stuck with MM/DD/YYYY.

That’s a fair point, but using a label instead of a placeholder is still better for accessibility, and you don’t lose anything by using accessible markup. Note that your Gmail example is also a single field, and they do follow the best practice as far as markup/interaction goes.

1 Like

Interesting debate going on here :slight_smile:
Thanks for examples, @kerafyrm02.

@lionel-rowe, so with accessibility in mind, what would be the correct way to do it without a label text? I don’t see the label text serving any real purpose here:

<section id="insider-program">
  <h2>Keep up with History!</h2>
  <div>Join the Age Insider Program to keep up on all Age of Empires titles.</div>

  <form action="https://www.freecodecamp.com/email-submit" method="post"bid="form">
    <label for="email"></label>
    <input type="email" name="email" id="email" placeholder="Your email" required />
    <input type="submit" value="Sign Up" id="submit" />
  </form>
</section>

Probably the simplest way would be to add an aria-label attribute to the input element, with the same value as the placeholder. With that, you wouldn’t need a separate label element at all (for this specific use case of a single field).

1 Like