使用占位符作为标签是否符合 WCAG 2? [英] Does using a placeholder as a label comply with WCAG 2?
问题描述
我知道使用占位符文本作为标签不是很容易访问,但它在技术上是否违反 WCAG 2?我找不到任何明确的内容,但我想知道对该标准进行更专业的阅读是否会在其中找到一些内容.
首先阅读:这不是建议您应该使用占位符而不是标签,而是关于占位符是否适用的思想实验在 WCAG 的指导下足够了.如果您确实使用占位符而不是标签,那么您的网站将无法访问,正如我之前在数百个答案中所述.如果您因歧视而被起诉,我怀疑此答案中的任何内容都不会对您有所帮助(也不应该).
要清楚 - 使用可见且正确关联的标签!/endRant hehe.
简答
只有占位符,没有标签很可能确实符合 WCAG 2.1,但谁知道所有相互矛盾的准则.
您会认为在数以万计的单词中会有一行写着使用标签";或仅占位符的输入将不符合此标准";但我找不到.
以下内容可能难以阅读,因为我阅读了我能找到的每一页以获得明确的答案,因此可能有点脱节,抱歉!
进入问题
好的,让我们假设我们暂时不关心可用性,而是专注于法律条文";关于 WCAG 2.1,因为这是一个有趣的问题.
现在让我们也假设我们纯粹是在谈论标准
和 s 来简化这一切(如复选框、选择等.需要标签),而且它们是唯一接受占位符值的标签(我认为?!).
哪些规则适用于可能相关的输入和标签?
此处相关的是1.1.1 Non-文本上下文,1.3.1 信息和关系,2.4.6 标题和标签、3.3.2 标签或说明 和 4.1.2 名称、角色、价值.后来我发现2.5.3: Label in Name.
1.1.1
<块引用>控件、输入:如果非文本内容是控件或接受用户输入,则它具有描述其用途的名称.
现在技术上标准 <input>
在这里还没有完全涵盖,这个标准更多地适用于用作按钮的图像、自定义控件等.
然而,如果我们假设我们可以扩展 1.1.1 以覆盖 在这个标准中是否有任何东西意味着我们不能使用占位符(或 有 具有关联标签).
如果您阅读了整个页面,则没有任何地方提到需要在表单控件上使用 <label>
元素.
结果:到目前为止都没有答案
1.3.1
检查 1.3.1 - 虽然它提到了标签,但它没有在任何地方特别提到 元素.它还指出这是一个判断电话".关系是否应该以编程方式确定或以文本形式呈现.
在某些情况下,可能需要判断关系是应该以编程方式确定还是以文本形式呈现.但是,当技术支持程序化关系时,强烈建议以程序化方式确定信息和关系,而不是在文本中描述.
结果:无论哪种方式都没有明确的答案
2.4.6
现在我们可以对 2.4.6 标题和标签打折扣,因为它只要求标签是描述性的,并没有特别说明必须提供标签.
结果:不相关
3.3.2
现在我们正在谈论标签或说明"一定要给我们一些具体的答案,对吗?
<块引用>此成功标准不要求正确标记、标识标签或说明或与其各自的控件相关联.
这很烦人,这里没有答案!
事实上,它让我们回到了 1.3.1,我们已经看过并确定没有明确的答案.
它也指向我们 4.1.2 - 也许我们会看看这个标准?
结果:不相关
4.1.2
对,一定是这样!至少它是在正确的轨道上开始的!
<块引用>对于所有用户界面组件(包括但不限于:表单元素、链接和脚本生成的组件),名称和角色都可以通过编程方式确定;
现在的关键部分是以编程方式确定".
好吧,的定义是什么以编程方式确定"?
<块引用>由软件根据作者提供的数据确定,以不同的用户代理(包括辅助技术)可以提取并以不同方式向用户呈现此信息
好吧,现在我们可能正在做一些事情.不同的用户代理和辅助技术能否提取占位符信息?它是一种有效的标记技术吗?
结果:仍然没有确定的答案
这是我们最终找到相关 WCAG 推荐的地方(或者我们是?)
成功标准 2.5.3:名称中的标签
2.5.3 标签名称肯定是我们正在寻找的确认.
<块引用>请注意,输入字段中的占位符文本不被视为提供标签的适当方式.HTML5 规范规定占位符属性不应用作 .然而,值得注意的是标签"在该 HTML5 语句中,代码括号和标签元素的链接. 就名称成功标准中的此标签而言,标签"不是在这种编程意义上使用,而是简单地指代与组件在视觉上非常接近的文本字符串.因此,在附近没有任何其他文本字符串的情况下(如前面的列表中所述),如果输入包含占位符文本,则此类文本可能是 Label in Name 的候选.通过可访问名称计算(稍后讨论)以及从实际意义来看,如果没有以其他方式提供可见标签,语音输入用户可能会尝试使用占位符文本值作为与输入交互的手段.
在所有这些杂乱无章的内容中(正如你所知道的,我厌倦了阅读那些令人费解且不明确的指南.)它说了一些有趣的事情.
如果输入包含占位符文本,则此类文本可能是 Label in Name 的候选."
有了这个,我们就会有一个以编程方式确定"的控件的名称.(见下一个标题)
上一段的旁注:注意输入字段内的占位符文本不被视为提供标签的适当方式"部分.会让您相信占位符不能作为标签.
它还指出,HTML5 规范规定,占位符属性不应用作 的替代品."
现在对我来说,这足以说服你使用标签.
然而,WCAG 再次表示网页应该是有效的 HTML";(根据 SC 4.1.1 - 解析).
那么带有占位符的 是有效的 HTML 吗?那么答案(根据 W3C 验证器是肯定的!以下是有效的 HTML!
<头><title>test</title>头部><身体><表格><输入占位符=测试"/></表单></html>
所以如果它是有效的,我会说这是技术上可以接受的.
可访问名称计算
我在那里偏离了一秒钟.我说的是 placeholder
属性是可访问名称计算的有效候选者这一事实.
这里是<的可访问名称计算/代码>
.相关点是第 4 点:
- 否则使用控件的占位符属性.
好的,所以 placeholder
对程序可确定"有效,但标签不是必须可见吗?
有趣的是,您不必为 1.1.1、1.3.1 和 4.1.2 设置可见标签,因此我们不能使用该参数来表示需要实际标签.
<块引用>这种技术(将标签与输入相关联)足以满足成功标准 1.1.1、1.3.1 和 4.1.2 无论标签元素是否可见.也就是说,它可能使用 CSS 隐藏.
说到下一句....
<块引用>但是,对于成功标准 3.3.2,标签元素必须是可见的,因为它为所有需要帮助理解该领域目的的用户提供帮助.
但在 3.3.2 中,虽然它必须是可见的,但不必正确标记(记住,我很久以前就说过!).
所以我可以在 WCAG 中找到所有相关信息,结果仍然模糊,但话虽如此,我确实有一个结论要给你!</p>
结论
WCAG 中没有任何地方明确声明不能将占位符用作标签.
WCAG 还指出 title
可以是用于标记 input
并且比占位符更糟糕.
我可能在某处遗漏了一个可以将它们联系在一起的关键句子,但从我读到的内容来看,我相信(非常令人惊讶!!)在 WCAG 2.1 下只有 上的占位符.
显然,正如我多次说过的那样不要仅在输入上使用占位符,因为很多用户都无法访问它(患有焦虑症或学习困难的人真的很讨厌带有占位符标签的输入)当他们输入时标签会消失,因此他们无法在不删除所有内容的情况下检查他们是否填写了正确的字段),某些屏幕阅读器和浏览器组合不适用于它们等.
I get that using placeholder text as a label is not very accessible, but does it technically go against WCAG 2? I could not find anything explicit but I wonder if a more lawyerly reading of that standard would find something in there.
Read first: this is not a suggestion that you should use a placeholder instead of a label, more of a thought experiment as to whether a placeholder is sufficient under WCAG guidance. If you do use a placeholder instead of a label then your site is not accessible as I have stated in hundreds of answers before. If you ever get sued for discrimination I doubt anything in this answer will help you (and neither should it).
To be clear - use a visible and correctly associated label! /endRant hehe.
Short Answer
A placeholder only and no label more than likely DOES comply with WCAG 2.1, but who knows with all the conflicting guidelines.
You would think in tens of thousands of words there would be a line that says "use a label" or "a placeholder only input would fail this criterion" but I couldn't find it.
The following may be hard to read as I read every page I could find to get a definitive answer so it could be a bit disjointed, apologies!
Onto the question
OK so lets assume we do not care about usability for a second and focus on the "letter of the law" with regards to WCAG 2.1, as it is an interesting question.
Now let's also assume we are talking purely about standard <inputs>
and <textarea>
s to make this easy (as checkboxes, selects etc. all definitely need labels), plus they are the only ones that accept a placeholder value anyway (I think?!).
What rules apply to inputs and labels that may be relevant?
The ones that are relevant here are 1.1.1 Non-text Context, 1.3.1 Info and Relationships, 2.4.6 Headings and Labels, 3.3.2 labels or instructions and 4.1.2 Name, Role, Value. I later found 2.5.3: Label in Name.
1.1.1
Controls, Input: If non-text content is a control or accepts user input, then it has a name that describes its purpose.
Now technically a standard <input>
isn't quite covered here, this criterion is more for things like images used as buttons, custom controls etc.
However if we assumed that we could stretch 1.1.1 to cover an <input>
is there anything in this criterion that means we can't use a placeholder (or have to have an associated label).
If you read the whole page there is not one specific mention of a <label>
element needing to be used on a form control.
outcome: so far no answer either way
1.3.1
Checking 1.3.1 - although it mentions labels, it does not specifically mention the <label>
element anywhere. It also states that it is a "judgement call" as to whether relationships should be programmatically determined or presented in text.
There may also be cases where it may be a judgment call as to whether the relationships should be programmatically determined or be presented in text. However, when technologies support programmatic relationships, it is strongly encouraged that information and relationships be programmatically determined rather than described in text.
outcome: no definitive answer either way
2.4.6
Now we can discount 2.4.6 Headings and labels as that only requires labels are descriptive, it does not specifically state labels must be provided.
outcome: not relevant
3.3.2
Now we are talking, "labels or instructions" must give us some concrete answers, right?
This Success Criterion does not require that labels or instructions be correctly marked up, identified, or associated with their respective controls.
Well that is annoying, no answer here!
In fact it it points us back to 1.3.1, which we already looked at and determined there was no definitive answer.
It also points us to 4.1.2 - maybe we will have some look with that criterion?
outcome: not relevant
4.1.2
Right, this must be it! It starts out on the right track at least!
For all user interface components (including but not limited to: form elements, links and components generated by scripts), the name and role can be programmatically determined;
Now the key part of this is "programmatically determined".
Ok so what is the definition of "programmatically determined"?
determined by software from author-supplied data provided in a way that different user agents, including assistive technologies, can extract and present this information to users in different modalities
Well now we might be onto something. Can different user agents and assistive technologies extract the placeholder information? Is it a valid labelling technique?
outcome: still no definitive answer
This is where we find the relevant WCAG recommendation finally (or do we?)
Success Criterion 2.5.3: Label in Name
2.5.3 label in name must be the confirmation we are looking for, surely.
Note that placeholder text within an input field is not considered an appropriate means of providing a label. The HTML5 specification states The placeholder attribute should not be used as an alternative to a . However, it is worth noting that "label" in that HTML5 statement is in code brackets and links to the label element. For the purposes of this Label in Name Success Criterion, "label" is not used in such a programmatic sense but is simply referring to a text string in close visual proximity to a component. As such, in the absence of any other nearby text string (as described in the preceding list), if an input contains placeholder text, such text may be a candidate for Label in Name. This is supported both through the accessible name calculation (discussed later) and from the practical sense that where a visible label is not otherwise provided, it is likely that a speech-input user may attempt to use the placeholder text value as a means of interacting with the input.
Within all of that rambling (as you can tell I am sick of reading the convoluted and unclear guidelines.) it says something interesting.
"if an input contains placeholder text, such text may be a candidate for Label in Name."
And with that we would have a "programmatically determined" name for the control. (see next heading)
side note on the paragraph above: The part "Note that placeholder text within an input field is not considered an appropriate means of providing a label." would lead you to believe that a placeholder is not valid as a label.
It also states that "The HTML5 specification states The placeholder attribute should not be used as an alternative to a <label>
."
Now to me that would be enough to persuade you to use a label.
However yet again, WCAG says that a webpage should be "valid HTML" (under SC 4.1.1 - Parsing).
So is an <input>
with a placeholder valid HTML? Well the answer (according to the W3C validator is yes! The following is valid HTML!
<html lang="en">
<head>
<title>test</title>
</head>
<body>
<form>
<input placeholder="test"/>
</form>
</body>
</html>
So if it is valid, I would say that is technically acceptable.
The accessible name computation
I got side-tracked there for a second. I was talking about the fact that a placeholder
attribute is a valid candidate for an accessible name computation.
Here it is, the accessible name computation for an <input>
. There relevant point is point number 4:
- Otherwise use the control's placeholder attribute.
OK so placeholder
is valid for "programatically determinable", but doesn't a label have to be visible?
Interestingly you do not have to have a visible label for 1.1.1, 1.3.1 and 4.1.2 so we can't use that argument as to needing an actual label.
Taken from H44: Using label elements to associate text labels with form controls
This technique (associating a label with an input) is sufficient for Success Criteria 1.1.1, 1.3.1 and 4.1.2 whether or not the label element is visible. That is, it may be hidden using CSS.
With that being said the next sentence....
However, for Success Criterion 3.3.2, the label element must be visible since it provides assistance to all users who need help understanding the purpose of the field.
But in 3.3.2 although it has to be visible, it doesn't have to be correctly marked up (remember, I did say it a long time ago!).
So there is all the relevant information I could find in WCAG and the outcome is still fuzzy, but with that being said I do have a conclusion for you!
Conclusion
Nowhere in WCAG does it explicitly state that a placeholder cannot be used as a label.
WCAG also states that a title
can be used to label an input
and that is worse than a placeholder.
It is possible I have missed a key sentence somewhere that would tie it all together but from what i have read I believe (very surprisingly!!) that it is valid under WCAG 2.1 to only have a placeholder on an <input>
.
Obviously as I stated several times DO NOT USE A PLACEHOLDER ONLY ON AN INPUT as it is not accessible to a lot of users (people with anxiety disorders or learning difficulties really hate inputs with placeholder labels as the label disappears when they type so they can't check they have filled the correct field in without deleting everything), some screen reader and browser combinations do not work with them etc.
这篇关于使用占位符作为标签是否符合 WCAG 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!