Semantics

    Labels are typically placed on top or to the left of the form fields:

    See the Pen Simple Form by Maria () on CodePen.

    Notice how you can include autocomplete='on' on the form element and it will apply to all inputs in your form. You can also set different for each input.

    Provide labels to describe the purpose of all form control; linking for and id:

    1. <label for="name">Name</label>
    2. <input type="text" name="name" id="name" v-model="name"/>

    See the Pen Form Label by Maria () on CodePen.

    If you inspect this element in your chrome developer tools and open the Accessibility tab inside the Elements tab, you will see how the input gets its name from the label:

    Chrome Developer Tools showing input accessible name from label

    Warning:

    Though you might have seen labels wrapping the input fields like this:

    1. <label>
    2. Name:
    3. <input type="text" name="name" id="name" v-model="name"/>
    4. </label>

    Explicitly setting the labels with an matching id is better supported by assistive technology.

    aria-label

    You can also give the input an accessible name with aria-label.

    1. <label for="name">Name</label>
    2. <input type="text" name="name" id="name" v-model="name" :aria-label="nameLabel"/>

    See the Pen by Maria (@mlama007) on .

    Chrome Developer Tools showing input accessible name from aria-label

    aria-labelledby

    Using is similar to aria-label expect it is used if the label text is visible on screen. It is paired to other elements by their id and you can link multiple ids:

    See the Pen Form ARIA labelledby by Maria () on CodePen.

    Chrome Developer Tools showing input accessible name from aria-labelledby

    aria-describedby

    aria-describedby is used the same way as aria-labelledby expect provides a description with additional information that the user might need. This can be used to describe the criteria for any input:

    1. <form class="demo" action="/dataCollectionLocation" method="post" autocomplete="on">
    2. <h1 id="billing">Billing</h1>
    3. <div class="form-item">
    4. <input type="text" name="name" id="name" v-model="name" aria-labelledby="billing name" aria-describedby="nameDescription"/>
    5. </div>
    6. <button type="submit">Submit</button>
    7. </form>

    See the Pen by Maria (@mlama007) on .

    You can see the description by instecting Chrome DevTools:

    Chrome Developer Tools showing input accessible name from aria-labelledby and description with aria-describedby

    Avoid using placeholders as they can confuse many users.

    One of the issues with placeholders is that they don’t meet the color contrast criteria by default; fixing the color contrast makes the placeholder look like pre-populated data in the input fields. Looking at the following example, you can see that the Last Name placeholder which meets the color contrast criteria looks like pre-populated data:

    See the Pen by Maria (@mlama007) on .

    It is best to provide all the information the user needs to fill out forms outside any inputs.

    1. <fieldset>
    2. <legend>Using aria-labelledby</legend>
    3. <label id="date-label" for="date">Current Date:</label>
    4. <input type="date" name="date" id="date" aria-labelledby="date-label date-instructions" />
    5. <p id="date-instructions">MM/DD/YYYY</p>
    6. </fieldset>

    Alternatively, you can attach the instructions to the input with aria-describedbySemantics - 图11:

    1. <fieldset>
    2. <legend>Using aria-describedby</legend>
    3. <label id="dob" for="dob">Date of Birth:</label>
    4. <input type="date" name="dob" id="dob" aria-describedby="dob-instructions" />
    5. <p id="dob-instructions">MM/DD/YYYY</p>
    6. </fieldset>

    See the Pen by Maria (@mlama007) on .

    Usually it is not recommended to visually hide labels, even if the input has an accessible name. However, if the functionality of the input can be understood with surrounding content, then we can hide the visual label.

    Let’s look at this search field:

    We can do this because the search button will help visual users identify the purpose of the input field.

    We can use CSS to visually hide elements but keep them available for assistive technology:

    1. .hidden-visually {
    2. position: absolute;
    3. overflow: hidden;
    4. white-space: nowrap;
    5. margin: 0;
    6. height: 1px;
    7. width: 1px;
    8. clip: rect(0 0 0 0);
    9. }

    See the Pen Form Search by Maria () on CodePen.

    aria-hidden=”true”

    Adding aria-hidden="true" will hide the element from assistive technology but leave it visually available for other users. Do not use it on focusable elements, purely on decorative, duplicated or offscreen content.

    1. <p>This is not hidden from screen readers.</p>
    2. <p aria-hidden="true">This is hidden from screen readers.</p>

    When using buttons inside a form, you must set the type to prevent submitting the form. You can also use an input to create buttons:

    1. <form action="/dataCollectionLocation" method="post" autocomplete='on'>
    2. <!-- Buttons -->
    3. <button type="button">Cancel</button>
    4. <button type="submit">Submit</button>
    5. <!-- Input buttons -->
    6. <input type="button" value="Cancel">
    7. <input type="submit" value="Submit">
    8. </form>

    See the Pen Form Buttons by Maria () on CodePen.

    Functional Images

    You can use this technique to create functional images.

    • Input fields

      • These images will act as a submit type button on forms
    1. <form role="search">
    2. <label for="searchIcon" class="hidden-visually">Search: </label>
    3. <input type="text" name="searchIcon" id="searchIcon" v-model="searchIcon">
    4. <button type="submit">
    5. <i class="fas fa-search" aria-hidden="true"></i>
    6. <span class="hidden-visually">Search</span>
    7. </form>

    See the Pen Functional Images by Maria () on CodePen.