惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
C
Check Point Blog
博客园 - Franky
月光博客
月光博客
T
Tenable Blog
博客园 - 聂微东
Cyberwarzone
Cyberwarzone
The GitHub Blog
The GitHub Blog
C
CERT Recently Published Vulnerability Notes
Scott Helme
Scott Helme
IT之家
IT之家
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CXSECURITY Database RSS Feed - CXSecurity.com
F
Full Disclosure
博客园 - 司徒正美
Project Zero
Project Zero
Y
Y Combinator Blog
A
Arctic Wolf
美团技术团队
博客园 - 叶小钗
S
Securelist
F
Fortinet All Blogs
T
Threatpost
T
Threat Research - Cisco Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
酷 壳 – CoolShell
酷 壳 – CoolShell
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Recorded Future
Recorded Future
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Schneier on Security
Apple Machine Learning Research
Apple Machine Learning Research
L
LangChain Blog
P
Privacy International News Feed
博客园_首页
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
P
Proofpoint News Feed
Cisco Talos Blog
Cisco Talos Blog
AWS News Blog
AWS News Blog
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
雷峰网
雷峰网
P
Proofpoint News Feed
Latest news
Latest news
G
GRAHAM CLULEY

TetraLogical Blog

Designing for people with reading disabilities - TetraLogical Designing for people who are D/deaf - TetraLogical Designing accessible documents - TetraLogical Introduction to creating accessible documents - TetraLogical Inclusive user research: vulnerable people - TetraLogical Designing for people who are blind - TetraLogical Designing for people with low vision - TetraLogical Meet the team: Niamh Madden - TetraLogical Designing for people with anxiety - TetraLogical Designing for people with disabilities - TetraLogical Accessible building blocks for the web videos - TetraLogical Common accessibility misconceptions - TetraLogical Common misconceptions about testing accessibility - TetraLogical Common misconceptions about implementing accessibility - TetraLogical Common misconceptions about WCAG - TetraLogical Common misconceptions about disability - TetraLogical Meet the team: Grace Snow - TetraLogical Sustainable accessibility in complex organisations: strategic foundations - TetraLogical Sustainable accessibility in complex organisations: organisational realities - TetraLogical Sustainable accessibility in complex organisations: external factors - TetraLogical Common misconceptions about screen readers - TetraLogical Guide to the Inclusive Design Principles - TetraLogical Meet the team: Ian Lloyd - TetraLogical Annotating designs using common language - TetraLogical Meet the team: Catriona Morrison - TetraLogical Championing inclusive language - TetraLogical Press release: TetraLogical launches accessible self-led training courses to help digital teams build confidence in accessibility - TetraLogical Why inclusive products are green products - TetraLogical Accessible Recruitment - TetraLogical Accessibility and the agentic web - TetraLogical Meet the team: Craig Abbott - TetraLogical Foundations: types of assistive technology and adaptive strategies - TetraLogical European Accessibility Act (EAA) FAQ - TetraLogical Screen reader HTML support tables - TetraLogical Interview with Lola Odelola - TetraLogical Understanding EN 17161 Design for All - TetraLogical Inclusive user research: building rapport - TetraLogical Foundations: Keyboard accessibility - TetraLogical Can generative AI write contextual text descriptions? - TetraLogical Understanding the European Accessibility Act (EAA) - TetraLogical Meet Josh: a sportsman who has spinal muscular atrophy - TetraLogical Meet Jonathan: a photographer who has ADHD - TetraLogical XR Accessibility: for people with moving disabilities - TetraLogical Meet Andre: a music producer and blind screen reader user - TetraLogical Foundations: types of disability - TetraLogical Meet Lauren: a film editor who has ADHD - TetraLogical Meet Steve: a photographer who is deaf and low vision - TetraLogical Foundations: form validation and error messages - TetraLogical Meet Hasmukh: a blind cricketer and screen reader user - TetraLogical XR Accessibility: for people with hearing disabilities - TetraLogical XR Accessibility: for people with thinking disabilities - TetraLogical XR Accessibility: for people with seeing disabilities - TetraLogical Introduction to XR Accessibility - TetraLogical Foundations: labelling text fields with input and label - TetraLogical Design patterns and WCAG - TetraLogical Does WCAG 2.2 apply to native apps - TetraLogical Why are my live regions not working? - TetraLogical Building a culture of accessibility - TetraLogical When to use tabindex= Accessibility foundations - TetraLogical Meet the team: Demelza Feltham - TetraLogical Can generative AI help write accessible code? - TetraLogical Meet the team: Steve Faulkner - TetraLogical Meet the team: Gez Lemon - TetraLogical Keyboard accessibility myths and WCAG - TetraLogical Amendment to the Public Sector Accessibility Regulations - TetraLogical What Considerations for TV user interface accessibility - TetraLogical Meet the team: Alistair Duggin - TetraLogical Sticky content: focus in view - TetraLogical The only accessibility specialist in the room - TetraLogical Meet the team: Ian Pouncey - TetraLogical Meet the team: Dean Holden - TetraLogical Meeting WCAG Level AAA - TetraLogical Foundations: accessible names and descriptions - TetraLogical Inclusive XR: accessible augmented reality experiences - TetraLogical Foundations: pointer gestures - TetraLogical Meet the team: Graeme Coleman - TetraLogical Adding sign language to videos - TetraLogical Foundations: introduction to WAI-ARIA - TetraLogical Meet the team: Joe Lamyman - TetraLogical Inclusive XR: accessible 3D experiences - TetraLogical Foundations: visible focus styles - TetraLogical Foundations: target sizes - TetraLogical Meet the team: Henny Swan - TetraLogical Meet the team: Ela Gorla - TetraLogical Foundations: native versus custom components - TetraLogical Foundations: HTML semantics - TetraLogical Accessibility and supporting Internet Explorer - TetraLogical Meet the team: Felicity Miners-Jones - TetraLogical Accessibility and QR codes - TetraLogical Inclusive user research: recruiting participants - TetraLogical Research insight: accessibility of images - TetraLogical Meet the team: Léonie Watson - TetraLogical Android accessibility: roles and TalkBack - TetraLogical Accessible design systems - TetraLogical Meet the team: Patrick H. Lauke - TetraLogical Inclusive user research: analysing findings - TetraLogical How to write user stories for accessibility - TetraLogical Triaging WCAG 2.1 Level AAA - TetraLogical
Foundations: grouping forms with `<fieldset>` and `<legend>` - TetraLogical
2025-01-31 · via TetraLogical Blog

Posted on by Demelza Feltham in Design and development

Tags: Assistive Technology, Code, Foundations, WCAG

In this post, we explore how to use the <fieldset> and <legend> elements to group and label form elements effectively, creating a more accessible and well-structured experience.

In addition to labelling text fields with <input> and <label> elements, implementing form validation and error messages effectively, accessible forms can be enhanced by using the <fieldset> and <legend> elements to create clear structure and meaningful grouping.

Much of a form's structure can be conveyed visually, whether through intentional spacing between sections or the use of bold text to highlight overarching questions paired with related controls. However, the visual grouping created by spacing should also be reflected in the markup. This can be done by using the <fieldset> and <legend> elements.

Accessible grouping benefits everyone, but in particular:

The <fieldset> Element

The <fieldset> element serves a dual purpose, visually grouping related controls by default, with browsers typically adding an outline around the content it wraps. This visual style can be customised as needed. Additionally, it semantically conveys this grouping to assistive technologies, ensuring users can easily understand the relationship between the grouped controls.

Code sample:

<fieldset>
  <p>I am wrapped in fieldset</p>
</fieldset>
A thin outline of a fieldset surrounding the text that says I am wrapped in a fieldset
An example of a fieldset element showcasing the default visual outline generated by the markup.

It is important to note that a <fieldset> element (or any element with a role="group") will only be recognised as a group by assistive technologies if it has an accessible name. Without an accessible name, the group role will not be conveyed to people using a screen reader. For instance, in the above example of a <fieldset> without a <legend> or accessible name, it would not be exposed as a group by a screen reader to someone who is blind.

Typically, a <fieldset> element is used in conjunction with a <legend> element to provide a label and therefore, an accessible name, for the grouped section.

The <legend> Element

The <legend> labels the <fieldset>. This could take the form of a question. If a question is visually linked with inputs such as radio buttons or checkboxes, but not coded correctly, people relying on assistive technology like screen readers may not perceive the connection. A <legend> may also be a section heading that will provide context to the remainder of the <fieldset>, without having to clutter each individual form label:

Two forms appearing next to one another, one with the heading Delivery address, the other Billing address. Each form contains the following fields, Name, Street, City, Post code and a blue Submit button
An example of two identical forms, distinguished solely by the legend element at the top of each form.

When a user navigating a form with a screen reader enters a group defined by a <fieldset> containing a <legend>, the screen reader announces the <legend> to provide context for the controls within the group.

Common accessibility issues arise when <fieldset> and <legend> elements are omitted or incorrectly implemented. These errors are frequently observed during assessments and are typically recorded as 1.3.1 Info and Relationships failures, as they prevent assistive technologies from accurately conveying the grouping and relationships within the form, despite the information being presented to sighted users.

A common issue we find during accessibility assessments is when there is a visual label for a group of controls, but the association is not reflected in the markup. For example:

<p>Can you see the question</p>
<div>
  <input type="radio" id="yes" name="agreement" value="yes">
  <label for="yes">Yes</label>
</div>

<div>
  <input type="radio" id="no" name="agreement" value="no">
  <label for="no">No</label>
</div>

Someone that is blind and relying on a screen reader could initially perceive the on-screen content as follows:

Two radio buttons stacked vertically. One is labelled Yes and the other is labelled No
An example illustrating how two radio buttons without an associated legend in the markup might be perceived by someone navigating with a screen reader

By restructuring the <p> element to be a <legend>, and wrapping the entire contents within a <fieldset>, assistive technology users will be made aware of the context.

<form>
  <fieldset>
    <legend>Can you see the question?</legend>
    
    <div>
      <input type="radio" id="yes" name="agreement" value="yes">
      <label for="yes">Yes</label>
    </div>
    
    <div>
      <input type="radio" id="no" name="agreement" value="no">
      <label for="no">No</label>
    </div>
    
  </fieldset>
</form>
A fieldset outline that contains two radio buttons that are vertically stacked. One is labelled Yes and one is labelled No. There is an overarching question that reads Can you see the question?
An example demonstrating how a properly marked-up fieldset and legend provide clear context to form controls.

Another common use case is when a form is divided into sections, such as when ordering a gift for someone and the delivery address differs from the billing address. While the same inputs (e.g., street, city, post code) may be used for both sections, grouping them with <fieldset> elements and providing a <legend> like "Delivery Address" and "Billing Address" ensures clear context and group labelling. This allows users to distinguish between the sections, even if the individual input labels are identical.

Changing the experience for screen readers users from this:

Two form fields appearing next to one another. Each form contains the following fields, Name, Street, City, Post code and a blue Submit button
An example illustrating how two identical forms may be perceived without a legend to differentiate their fieldset groups.

To this:

Two form fields appearing next to one another, one with the heading Delivery address, the other Billing address. Each form contains the following fields, Name, Street, City, Post code and a blue Submit button
An example of two identical forms, distinguished solely by the legend element at the top of each form.

Technical considering for <fieldset> and <legend>

<legend> as first child

For a <legend> to function as intended serving as a label for the group defined by the <fieldset> it must be the first child of the <fieldset>. This means no elements can appear between the opening <fieldset> element and the <legend> element. Placing any element between the two disrupts their semantic relationship, causing the <legend> to no longer be recognised as the label for the group. As a result, assistive technologies cannot convey the relationship between the grouped elements and their label, reducing the accessibility of the form.

If hints or instructions need to precede the controls, they should be placed after the <legend> within the <fieldset>. Alternatively, ARIA attributes can be used to provide the necessary grouping and labelling, which is discussed in more detail later.

Headings as <legend> elements

Previously, the HTML specification did not allow headings to also function as <legend> elements, but this has since changed and headings can now be wrapped within a <legend>. For example,

<legend>
  <h2>Your order details</h2>
</legend>

Using ARIA Instead of <fieldset> and <legend>

In some cases, grouping and labelling may not be achievable with HTML semantics, requiring the use of ARIA instead. This might occur when a <legend> cannot be the first child of a <fieldset> or when existing content on the page is being referenced as a label for the group.

Regardless of the approach, a defined group and its corresponding label are essential to ensure accessible grouping and labelling. While you can now wrap a heading within a <legend>, it’s also possible to use a heading (or any other element) outside of a <fieldset> to label the group. To do this, assign an id to the heading or other element, and reference it with the <fieldset> using the aria-labelledby attribute. This approach allows the heading to serve as the label for the <fieldset> group.

For example:

<h2 id="contactDetails">Your contact details</h2>
<fieldset aria-labelledby="contactDetails"></fieldset>

If the <legend> cannot be placed as the first child of a <fieldset>, the semantic relationship between the two elements is disrupted. In such cases, adding role="group" (or in the case of a group of radio buttons, role="radiogroup") to the grouping element can help convey the intended grouping to assistive technologies. However, note that the visual outline typically provided by the <fieldset> will not be displayed. To add a label to the group, assign an id to the element that will act as a label and reference it with aria-labelledby on the grouping element. This ensures assistive technologies recognise the group and its label.

For example:

<div role="group" aria-labelledby="group-label">
  <p id="group-label">Please select your preferences:</p>
  <div>
    <input type="checkbox" id="checkbox1" name="option1">
    <label for="checkbox1">Option 1</label>
  </div>
  <div>
    <input type="checkbox" id="checkbox2" name="option2">
    <label for="checkbox2">Option 2</label>
  </div>
</div>

Using the <fieldset> and <legend> elements is an effective way to improve form accessibility and usability. These elements ensure that visual structure is conveyed to assistive technologies, benefiting a broad range of users. By implementing them thoughtfully, you create forms that are both visually organised and semantically structured.

Summary

  • Group related controls using <fieldset> and label them with <legend> to provide clear context for users, especially those relying on assistive technologies
  • Ensure the <legend> is the first child of the <fieldset> to preserve the semantic relationship
  • Leverage ARIA when needed. If <fieldset> and <legend> cannot be used, apply role="group" (or in the case of a group of radio buttons, role="radiogroup") and aria-labelledby for semantic grouping
  • Use a <legend> for overarching context instead of repeating similar labels for every control
  • If the default <fieldset> appearance doesn't fit your design, use CSS to customise its visual style while retaining accessibility

More information

Next steps

Read more accessibility foundations posts or sign up for Accessibility Unlocked, our free six-day newsletter series designed to help you kick-start accessibility.

Updated 13 June, 2025