Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New page: placeholder attr description #28511

Merged
merged 5 commits into from
Aug 26, 2023

Conversation

Arisch24
Copy link
Contributor

Description

The placeholder attribute is fundamental for input and textarea types in html form elements.

This PR provides documentation for the placeholder attribute and its use cases with relevant examples.

Motivation

To help readers understand placeholder attribute better and saving readers time as well.

Additional details

W3 placeholder attr
W3 schools placeholder attr

Related issues and pull requests

Relates to #28375

@Arisch24 Arisch24 requested a review from a team as a code owner August 11, 2023 03:56
@Arisch24 Arisch24 requested review from estelle and removed request for a team August 11, 2023 03:56
@github-actions github-actions bot added the Content:HTML Hypertext Markup Language docs label Aug 11, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Aug 11, 2023

Preview URLs

Flaws (1)

URL: /en-US/docs/Web/HTML/Attributes/placeholder
Title: HTML attribute: placeholder
Flaw count: 1

  • broken_links:
    • Can't resolve /en-US/docs/Web/CSS/_colon_placeholder-shown

(comment last updated: 2023-08-26 01:09:46)

Copy link
Member

@estelle estelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 26 to 29
<div class="group">
<label>Without placeholder:</label>
<input type="text" />
</div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use implicit (nested) or explicit (for attribute) labels. The way this is marked up is not valid.

Also, favor semantic HTML, like a paragraph, over non-semantic elements with a class name, which doesn't provide semantics. The added bonus is a paragraph will add margins between adjacent examples.


### Result

{{EmbedLiveSample('Placeholder')}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first parameter must match the a parent header, in this case the # examples. The two other parameters are width and height. I guessed at 150px.

Suggested change
{{EmbedLiveSample('Placeholder')}}
{{EmbedLiveSample("Example", "", "150")}}

</div>
<div class="group">
<label>With placeholder:</label>
<input type="text" placeholder="Enter your name" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is the exact opposite of what developers should be doing. A placeholder should be a short hint, not the label. It disappears on data entry. This example is inaccessible (and it's not associated with its label)


## See also

- [HTML attribute: title](/en-US/docs/Web/HTML/Global_attributes/title)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- [HTML attribute: title](/en-US/docs/Web/HTML/Global_attributes/title)
- HTML [`title`](/en-US/docs/Web/HTML/Global_attributes/title) attribute

## See also

- [HTML attribute: title](/en-US/docs/Web/HTML/Global_attributes/title)
- [CSS selector `::placeholder`](/en-US/docs/Web/CSS/::placeholder)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- [CSS selector `::placeholder`](/en-US/docs/Web/CSS/::placeholder)
- CSS {{cssxref(":placeholder-shown")}} pseudo-class selector
- CSS {{cssxref("::placeholder")}} pseudo-element selector

Copy link
Contributor Author

@Arisch24 Arisch24 Aug 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I know why are the links as {{cssxref(":placeholder-shown")}} preferred?


{{HTMLSidebar}}

The **`placeholder`** attribute specifies a short hint ,a word or phrase, that describes the expected value of an input. A hint could be a sample value or a short description containing the format of the expected value. The attribute, if specified, must have a value that contains no U+000A LINE FEED (LF) or U+000D CARRIAGE RETURN (CR) characters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like this:

The **`placeholder`** defines the text displayed in a form control when the control has no value. The placeholder text provides a brief hint to the user as to the expected type of data that should be entered into the control.

The placeholder should be a word or short phrase that hints at the expected data type, not an explanation or prompt. The placeholder should not be used instead of a {{HTMLElement("label")}}. As the placeholder is not visible if the value of the form control is not null, using `placeholder` instead of a `<label>` for a prompt harms usability and accessibility.

and then moving the whitespace information to a note. the parts about it only appearing when there is no value and disappearing when there is, and not using it to replace a label are more important than the LF or CR. The LF/CR note should not be a copy paste from the spec. instead, explain what happens (the placeholder value gets clipped)


The **`placeholder`** attribute is supported by the following input types: `{{HTMLElement("input/text", "text")}}`, `{{HTMLElement("input/search", "search")}}`, `{{HTMLElement("input/url", "url")}}`, `{{HTMLElement("input/tel", "tel")}}`, `{{HTMLElement("input/email", "email")}}`, and `{{HTMLElement("input/password", "password")}}`. It is also supported by the `{{HTMLElement("textarea")}}` element.

> **Note:** For a longer hint, or other advisory text, the title attribute is more appropriate.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

title attributes are not accessible and not a replacement.They should be using a label.

These mechanisms are very similar but subtly different: the hint given by the control's label is shown at all times; the short hint given in the placeholder attribute is shown before the user enters a value; and the hint in the title attribute is shown when the user requests further help.

@Arisch24
Copy link
Contributor Author

Thank you for the suggestions @estelle. I will proceed to fix them.

Fix HTML semantics, EmbedLiveSample, link formatting and added further explanations regarding placeholder attributes
Comment on lines 29 to 35
<form action="/form_action.php">
<label for="name">Enter your name:</label>
<br>
<input type="tel" id="name" name="name" placeholder="e.g. John Doe">
<br><br>
<input type="submit">
</form
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • the action should be the page itself, as it creates an iframe, and we don't want an error.
  • All MDN examples need to show best practices. Therefore the HTML should be semantic. ( <br> is for poetry or addresses),
  • On MDN, we add a slash at the end of void elements . https://developer.mozilla.org/en-US/docs/MDN/Writing_guidelines/Writing_style_guide/Code_style_guide/HTML
  • type="tel" produces a numeric keypad on devices with dynamic keyboards
  • opt for <button> for buttons (i don't think we have a rule on that, but buttons are easier to style so seem to be the standard)
  • John Doe? Consider all our readers when choosing examples; this example is over used and not inclusive.


{{HTMLSidebar}}

The **`placeholder`** defines the text displayed in a form control when the control has no value. The placeholder text provides a brief hint to the user as to the expected type of data that should be entered into the control.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The **`placeholder`** defines the text displayed in a form control when the control has no value. The placeholder text provides a brief hint to the user as to the expected type of data that should be entered into the control.
The **`placeholder`** defines the text displayed in a form control when the control has no value. The placeholder text should provide a brief hint to the user as to the expected type of data that should be entered into the control.


The **`placeholder`** defines the text displayed in a form control when the control has no value. The placeholder text provides a brief hint to the user as to the expected type of data that should be entered into the control.

The placeholder should be a word or short phrase that hints at the expected data type, not an explanation or prompt. The placeholder should not be used instead of a {{HTMLElement("label")}}. As the placeholder is not visible if the value of the form control is not null, using `placeholder` instead of a `<label>` for a prompt harms usability and accessibility.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The placeholder should be a word or short phrase that hints at the expected data type, not an explanation or prompt. The placeholder should not be used instead of a {{HTMLElement("label")}}. As the placeholder is not visible if the value of the form control is not null, using `placeholder` instead of a `<label>` for a prompt harms usability and accessibility.
Effective placeholder text includes a word or short phrase that hints at the expected data type, not an explanation or prompt. The placeholder must not be used instead of a {{HTMLElement("label")}}. As the placeholder is not visible if the value of the form control is not null, using `placeholder` instead of a `<label>` for a prompt harms usability and accessibility.

Comment on lines 18 to 22
> **Note:** The attribute, if specified, must have a value that contains no line feeds (LF) or carriage returns (CR) characters. If the value contains either LF or CR characters, the value will be clipped.

> **Note:** For a longer hint, or other advisory text, the {{HTMLElement("small")}} should be used.

> **Warning:** Placeholders should only be used to show an example of the type of data that should be entered into a form; they don't replace a proper label as they are not semantically useful. This harms many users who are either older or people with disabilities. For example, older users may not understand that the placeholder text disappears when they start typing, and users with cognitive impairments may not understand that the placeholder text provides a hint.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> **Note:** The attribute, if specified, must have a value that contains no line feeds (LF) or carriage returns (CR) characters. If the value contains either LF or CR characters, the value will be clipped.
> **Note:** For a longer hint, or other advisory text, the {{HTMLElement("small")}} should be used.
> **Warning:** Placeholders should only be used to show an example of the type of data that should be entered into a form; they don't replace a proper label as they are not semantically useful. This harms many users who are either older or people with disabilities. For example, older users may not understand that the placeholder text disappears when they start typing, and users with cognitive impairments may not understand that the placeholder text provides a hint.
The `placeholder` attribute can't include any line feeds (LF) or carriage returns (CR). If either is included in the value, the placeholder text will be clipped.
## Accessibility concerns
Placeholders should only be used to show an example of the type of data that should be entered into a form; never as a replacement for a `<label>` element; doing so harms accessibility and user experience.
The `<label>` text is visually and programmatically associated with its corresponding form control. Screen readers don't announce placeholder content by default, but they do announce label content; it's the label that informs assistive technology users what data should be entered in the control. Labels also improve user experience for users of pointing devices: When a user clicks, touches, or taps a `<label>`, focus is moved to the the label's associated form control.
Placeholders can not be relied upon as a replacement for a label even for those not relying on assistive technology. Placeholder text is displayed at lower color contrast than the default form control text. This is by design, as you don't want users to be confused by what is placeholder text versus what is a filled-in form field. However, this lack of contrast can cause issues for low-vision users. Additionally, placeholder text disappears from form fields when users start entering text. If the placeholder text contains instructional information or examples that disappear, it can be confusing to users with cognitive issues and can make the form inaccessible if the placeholder contained the label.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To include the small text comment, we should link to the resource explaining how to write effective longer hints and advisory text

Copy link
Member

@estelle estelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay! We did it! Thank you for all your work.

For future PR reviews, let us know when you are ready for a re-review.

To send us a notification, click on the icon next to the reviewer's name in the top right of the page (widescreen) or after the last comment (narrow screen). (The id of the section is #reviewers-select-menu if you can't find it). When the icon is clicked, it changes from an icon with two arrows to a brown dot and the reviewer gets a notification.

files/en-us/web/html/attributes/placeholder/index.md Outdated Show resolved Hide resolved
files/en-us/web/html/attributes/placeholder/index.md Outdated Show resolved Hide resolved
@estelle estelle enabled auto-merge (squash) August 26, 2023 01:07
@estelle estelle merged commit 1d18b93 into mdn:main Aug 26, 2023
6 checks passed
@Arisch24 Arisch24 deleted the arisch24-placeholder-attr branch August 26, 2023 05:30
@Arisch24
Copy link
Contributor Author

Thank for your support and guidance @estelle. Yeah next time I'll be sure to send the notification when I'm ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:HTML Hypertext Markup Language docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants