Testing aria-level

aria-level defines an element's level within a hierarchy. It is commonly used with role="heading" to give an element a heading level without using a native <h1> to <h6> element.

This can be useful for components that need different heading levels depending on where they appear on a page.

Example 1: role="heading" with aria-level="2"

This example uses role="heading" to identify an element as a heading, and aria-level="2" to give it a level 2 heading.

This behaves like a heading, level 2

Expected result: should be announced as a level 2 heading, and should appear in a screen reader's headings list alongside real headings.

HTML markup
<div role="heading" aria-level="2">
  This behaves like a heading, level 2
</div>

Example 2: Same card, two different levels

This example shows how the same card component can use different heading levels depending on where it appears on a page. The aria-level value can be changed to match the heading structure.

Context A: card sits inside a sidebar, one level under this section's own h3

Understanding aria-level

A short related-content teaser, rendered from a CMS field with no heading markup of its own.

Context B: the same card component, now placed directly under this page's h1

Understanding aria-level

Identical markup and identical component, only the level passed in has changed.

Expected result: The first card's title should be announced as a level 4 heading, and the second as a level 2 heading. Both should appear at the correct levels in a screen reader's headings list.

HTML markup
<!-- the component itself never changes -->
<div class="fake-card">
  <div role="heading" aria-level="4">Understanding aria-level</div>
  <p>A short related-content teaser...</p>
</div>

<!-- only the level passed in by the parent context changes -->
<div class="fake-card">
  <div role="heading" aria-level="2">Understanding aria-level</div>
  <p>Identical markup and identical component...</p>
</div>

Example 3: aria-level in a treegrid

The aria-level attribute can also identify the level of rows in a treegrid. This example shows a file browser where aria-level identifies the hierarchy of folders and files.

Name Type Modified
📁 Reports Folder 14 Aug 2026
📄 Q3 report.docx Word doc 2 Aug 2026
📁 Archive Folder 11 Jun 2026
📄 Q1 report.docx Word doc 4 Apr 2026
📄 Q2 report.docx Word doc 7 Jul 2026
📄 budget.xlsx Excel 16 Aug 2026

Why not a native table?

A native <table> does not support hierarchical rows or expanding and collapsing groups of rows. A treegrid supports these features, along with arrow-key navigation between rows and cells.

Use a native table for ordinary tabular data. A treegrid is better suited to interactive, hierarchical data, such as a file browser.

Headings are not appropriate here because the folder names are row labels, not headings for sections of content.

Can aria-level go beyond 6?

Yes. Unlike native headings, which range from <h1> to <h6>, aria-level has no defined maximum. However, screen reader and browser support for levels above 6 can vary, particularly for headings.

Support for deeper levels in treegrid rows should be tested rather than assumed.

Setsize and position numbering

The aria-posinset values may appear out of order: 1, 1, 2, 1, 2, 2. This is because aria-setsize and aria-posinset describe an item's position within its group, not the entire treegrid. Each group starts counting from 1.

The R labels show each row's position in the overall grid, which is separate from its position within a group.

Expected result: Screen readers should announce each row's level and position within its group. Collapsing "Archive" should hide its child rows, and expanding "Reports" should not affect the state of "Archive".

R1: aria-rowcount: 7
R2:     Reports (Level 1, 1 of 2)
R3:         Q3 report.docx (Level 2, 1 of 2)
R4:         Archive (Level 2, 2 of 2)
R5:             Q1 report.docx (Level 3, 1 of 2)
R6:             Q2 report.docx (Level 3, 2 of 2)
R7:     budget.xlsx (Level 1, 2 of 2)

aria-posinset and aria-setsize describe each item's position within its group. aria-rowindex identifies its position in the overall grid.

aria-rowcount="7" includes the header row (R1) and six data rows (R2–R7).

HTML markup
<div role="treegrid" aria-label="Project files">

  <div role="row" aria-level="1" aria-expanded="true" aria-setsize="2" aria-posinset="1" data-id="reports">
    <span role="gridcell">📁 Reports</span>
    <span role="gridcell">Folder</span>
  </div>

  <div role="row" aria-level="2" data-parent="reports" aria-setsize="2" aria-posinset="1">
    <span role="gridcell">📄 Q3 report.docx</span>
    <span role="gridcell">Word doc</span>
  </div>

  <!-- a nested folder repeats the pattern one level deeper -->
  <div role="row" aria-level="2" aria-expanded="true" data-parent="reports" data-id="archive">
    <span role="gridcell">📁 Archive</span>
  </div>
  <div role="row" aria-level="3" data-parent="archive">
    <span role="gridcell">📄 Q1 report.docx</span>
  </div>

</div>

<!-- aria-level is on the row, never the gridcell -->

Note: aria-level only has an effect within a small set of roles that support it, chiefly heading, but also row, listitem, and treeitem in some structural contexts. Adding it to an element with no supporting role does nothing.