Forms – Label Placement and Structure
Forms are used to submit user data on websites and web applications. Forms can be simple such as a feedback form and complex such as placing an order form. It can be a simple one-page form or a multipage form.
Accessible forms make the task of inputting the information an equitable experience for people with different types of disabilities.
Form labels CAN be defined using HTML or ARIA semantics.
NOTE:
- New to accessibility or uncertain of requirements, it will be helpful to review all sections below.
- Already familiar with requirements, skip to the “Working Example” section for sample HTML, CSS and JavaScript (when needed), along with a working demo.
A form with well labelled controls helps not only people with disabilities but any user to input the data correctly at the first go!
Form labels are used to identify the purpose of form controls such as text fields, checkboxes, and radio buttons.
Visual text used to label user interface controls should be a part of the accessible name for the respective control. In fact, the best practice is to have the visual text at the beginning of the accessible name to enable assistive technologies, such as speech recognition tools and text-to-speech software behave in a predictable manner and avoid confusion.
Required fields SHOULD indicated via the label. Additionally, the following recommendations SHOULD be observed.
-
The
required
attribute MAY be used.
Note: Caution when using this attribute as it will cause some browsers to show generic error messages. -
In addition to the
required
attribute, usearia-required="true"
attribute inside the<input>
field. - Include the word “required” in the form field label rather than an "*".
- If you use an "*" to denote required fields, include instructions at the top of the page indicating that an asterisk means that a form field is required.
- When validation fails, ensure that focus is taken to the form fields that fail validation.
Below points MUST be considered when labelling a form control.
-
Place the label in close visual proximity to the related form control.
- The placement of the label SHOULD be Top Left Aligned for input fields and dropdowns. In DOM, it SHOULD be placed before the respective form control.
- The placement of the label SHOULD be Right Aligned for radio buttons and checkboxes. In DOM, it SHOULD be placed after the respective form control.
- The labels for the form controls MUST be unique.
- The label MUST describe the purpose of the form control.
-
Form control label text MUST have sufficient color contrast with its background.
- Minimum requirement of 4.5:1 MUST be met for the standard text size labels.
- Minimum requirement of 3:1 MUST be met for labels having larger text size i.e., 14pts with bold weight or more than 18 pts.
-
<label>
element:- This element is used to define a label for form control.
-
The
<label>
element can be defined implicitly and explicitly.-
For explicit association, the label is associated with the form control using for
and id attributes.
For example:<label for="firstname">First name (required)</label> <input type="text" name="firstname" id="firstname"><br>
<input type="checkbox" name="remember" id="remember"> <label for="remember">Remember Me</label>
-
For implicit association, the form control is defined within the
<label>
element. Here, no for and id attributes are used.
For example:<label> First name (required) <input type="text" name="firstname"> </label>
- Form controls MUST have unique id attribute value.
-
For explicit association, the label is associated with the form control using for
and id attributes.
-
A visually hidden text CAN also be specified within the label element to
explicitly associate it with the form control in case of design constraints.
-
Hiding the label text CAN be achieved with CSS clip property and
hidden: overflow
property.
Note: A visible label is ALWAYS advisable since it is beneficial for all users.
-
Hiding the label text CAN be achieved with CSS clip property and
-
title
attribute:-
This attribute CAN be used to specify an accessible name for the form
control ONLY when the visual text is not available.
For example:<input title="Search" type="text" name="search">
- On mouse hover, it will display the specified text in a form of tooltip.
- This should NEVER be used ideally even though it is part of the accessible name calculation. It often leads developers to believe a valuable tooltip has been created which is not the case.
- This attribute often leads to duplicated announcements by screen readers if a well-defined programmatic label is already present.
-
This attribute CAN be used to specify an accessible name for the form
control ONLY when the visual text is not available.
-
placeholder
attribute:- It provides instruction or an example of required data format within the field that has not yet been edited by user.
-
It CAN be used as last resort along with an aria-label attribute to convey
the purpose through a visual text for the field wherein there are design constraints or no
visual label at all.
For example:<input placeholder="Search your text" type="text" name="search" aria-label="Search">
- Though it can provide an additional instruction or information, it MUST be avoided in scenarios wherein a well-defined programmatic label and instructions are provided for the form control.
- Generally, has a very poor contrast than the text that has been input by user.
- It disappears as soon as the user types anything within the field.
- It is NOT to be considered as a replacement for labels.
-
aria-labelledby
attribute:- This attribute is used to associate the label with the form control by referencing the id attribute of visual text container or visually hidden text container.
-
Concatenation of multiple pieces of text to form a label is possible through this attribute
by referencing the respective
id
attribute values.- With concatenation, the order of
id
attribute value is important.
- With concatenation, the order of
-
id
attribute values SHOULD be present in the same HTML document. -
It CAN be specified in case of design constraint as well wherein an
additional visual text cannot be added for the form control.
For example:<input type="text" name="search" aria-labelledby="searchbutton"> <button id="searchbutton" type="submit">Search</button>
-
If both <label> and
aria-labelledby
oraria-label
andaria-labelledby
are present, thearia-labelledby
will take precedence to convey the label of the form control.
Note: A visible label is always advisable since it is beneficial for all users.
-
aria-label
attribute:-
This attribute is used to define a label for a form control that does not have supporting
visual label.
For example:<input type="text" name="search" aria-label="Search">
- It’s a challenge for internationalization because its value will not get translated and will require manual updating.
- It’s also a challenge for labelling wherein the labels must be updated as these are hardcoded values.
- It’s not visible on-screen so ONLY helps assistive technology users such as screen reader users.
-
This attribute is used to define a label for a form control that does not have supporting
visual label.
-
aria-describedby
attribute:-
It is used to associate the instruction or error messages with the respective form control
by referencing the id attribute of visual instruction text container or visually hidden
instruction text container.
For example:<label for="usern">Username*</label> <input type="text" name="user" id="usern" aria-describedby="userDesc"> <span id="userDesc">This must be a valid email address</span>
- This attribute must NOT be used for labelling a form control.
-
It is used to associate the instruction or error messages with the respective form control
by referencing the id attribute of visual instruction text container or visually hidden
instruction text container.
A form with well-defined labels for form controls benefits majorly the below users.
- People with cognitive disabilities
- People using speech input
- People with limited dexterity by making the clicking area bigger.
- People using screen readers
<form class="auto">
<h2>Top Left aligned explicitly associated label for input field</h2>
<label for="fullname">Full Name</label>
<input type="text" name="fullname" id="fullname" autocomplete="name">
<h2>Top Aligned Implicit label for input field</h2>
<label>Email <input type="email" id="email" autocomplete="email" name="email"></label>
<h2>Right aligned label for checkbox</h2>
<fieldset>
<legend><strong>Newsletter</strong></legend>
<div>
<input type="checkbox" name="newsletter" id="check_2" value="Monthly">
<label for="check_2">Monthly</label>
</div>
<div>
<input type="checkbox" name="newsletter" id="check_3" value="Yearly">
<label for="check_3">Yearly</label>
</div>
</fieldset>
<h2>Right aligned label for radio button</h2>
<fieldset>
<legend><strong>File Type</strong></legend>
<div>
<input type="radio" name="format" id="word" value="word">
<label for="word">Word</label>
</div>
<div>
<input type="radio" name="format" id="pdf" value="pdf">
<label for="pdf">PDF</label>
</div>
</fieldset>
<h2>Off-screen label for input field during design constraint</h2>
<label for="txtSearch" class="screenReaderOnly">Search: </label>
<input id="txtSearch" type="text" title="Type search term here"/>
<input type="submit" name="submit" value="Search">
<h2>Title attribute as a label</h2>
<select id="city" name="select" title="select your city" >
<option value="">-- Select city -- </option>
<option value="1">Amsterdam</option>
<option value="2">Buenos Aires</option>
<option value="3">Delhi</option>
<option value="4">Hong Kong</option>
<option value="5">London</option>
<option value="6">Los Angeles</option>
<option value="7">Moscow</option>
<option value="8">Mumbai</option>
<option value="9">New York</option>
<option value="10">Sydney</option>
<option value="11">Tokyo</option>
</select>
</form>
body {
font-style: Helvetica,Verdana,sans-serif;
font-size: 1rem;
}
input {
display: block;
margin-bottom: 0.5rem;
padding: 0.8rem;
border: 1px solid #8e8e8e;
line-height: 1.15;
width: 40%;
border-radius: 4px;
}
label { font-size:1.2rem; }
fieldset {
width: 40%;
margin-bottom: 1rem;
}
fieldset input {
display: inline;
width: auto;
}
input[type=submit] {
background-color: #eff3f6;
border: 1px solid #000;
border-radius: 10px;
color: #000;
padding: 0.35rem 1rem;
width: auto;
margin: 0.5rem 0;
font-size: 1rem;
cursor: pointer;
width: auto;
display: inline;
}
.screenReaderOnly {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
#txtSearch {
width:auto;
display: inline-block;
}
select {
background-color: #f5f5f5;
border: 2px solid rgba(0,0,0,.5);
border-radius: 4px;
display: block;
font-size: 1em;
min-height: calc(1.4em + 26px);
padding: 12px 16px 14px;
width: 30%;
}
span {
display: block;
font-size: 1.2rem;
}
@media screen and (max-width: 600px) {
.auto { width: 100%; }
input,
fieldset,
select {
width: 90%;
}
}
The form has been reset.