使垂直表格易于访问

发布于 2025-01-10 12:00:25 字数 1485 浏览 0 评论 0原文

我有一些非常基本的数据,如下所示:

{
  "name": "John Doe",
  "city": "Minneapolis",
  "state": "Minnesota",
  "country": "United States",
  "age": "42"
}

我需要像这样显示它:

| name    | John Doe      |
| city    | Minneapolis   |
| state   | Minnesota     |
| country | United States |
| age     | 42            |

正如您所看到的,上面的表格标题左对齐,行值全部对齐。

根据W3,做垂直表格的方法就像下面的代码片段,但他们还指出:

注意:某些屏幕阅读器会在“地点”单元格中读取“日期 - 事件 - 地点”,因为 元素的方向不明确。

table th {
 text-align: left;
}
<table>
  <tr>
    <th>Name</th>
    <td>John Doe</td>
  </tr>
  <tr>
    <th>City</th>
    <td>Minneapolis</td>
  </tr>
  <tr>
    <th>State</th>
    <td>Minnesota</td>
  </tr>
  <tr>
    <th>Country</th>
    <td>United States</td>
  </tr>
  <tr>
    <th>Age</th>
    <td>42</td>
  </tr>
</table>

我的问题是这样的垂直表格的可访问性如何,我最好使用

    和一些 Flexbox 或网格魔术来获得我的样式要求并将数据设置为列表吗?还有我没有考虑的替代方案吗?

I have some very basic data, like this:

{
  "name": "John Doe",
  "city": "Minneapolis",
  "state": "Minnesota",
  "country": "United States",
  "age": "42"
}

I need to display it like this:

| name    | John Doe      |
| city    | Minneapolis   |
| state   | Minnesota     |
| country | United States |
| age     | 42            |

As you can see the table above has the headers left justified and the row values are all aligned.

According to W3, the way to do a vertical table is like the snippet below, but they also call out:

Note: Some screen readers will read “Date – Event – Venue” in the “Venue” cell because the direction of the <th> elements is ambiguous.

table th {
 text-align: left;
}
<table>
  <tr>
    <th>Name</th>
    <td>John Doe</td>
  </tr>
  <tr>
    <th>City</th>
    <td>Minneapolis</td>
  </tr>
  <tr>
    <th>State</th>
    <td>Minnesota</td>
  </tr>
  <tr>
    <th>Country</th>
    <td>United States</td>
  </tr>
  <tr>
    <th>Age</th>
    <td>42</td>
  </tr>
</table>

My question is how accessible is a vertical table like this, and would I be better off using a <ul> with some flexbox or grid magic to get my styling requirements and setting the data as a list? Is there an alternative I'm not considering?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

濫情▎り 2025-01-17 12:00:25

如果编码正确,屏幕阅读器绝对可以访问表格。您的示例(以及 W3 示例)中唯一缺少的是 的范围。您应该始终指定scope属性,以便它明确说明表标题是针对行还是列。当您省略 scope 时,不要依赖浏览器或屏幕阅读器来“猜测”含义。

当指定 scope 时,标头的方向没有任何歧义。

<table>
  <tr>
    <th scope="row">Name</th>
    <td>John Doe</td>
  </tr>
  <tr>
    <th scope="row">City</th>
    <td>Minneapolis</td>
  </tr>
  <tr>
    <th scope="row">State</th>
    <td>Minnesota</td>
  </tr>
  <tr>
    <th scope="row">Country</th>
    <td>United States</td>
  </tr>
  <tr>
    <th scope="row">Age</th>
    <td>42</td>
  </tr>
</table>

现在,当屏幕阅读器用户导航到数据单元格(例如“明尼阿波利斯”)时,他们然后使用表格导航键(例如 ctrl+alt+向下箭头),他们会先听到行标签,然后听到数据单元格值,例如“明尼苏达州”。

Tables are absolutely accessible to screen readers when coded properly. The only thing missing in your example (and the W3 example) is the scope of the <th>. You should always specify the scope attribute so that it specifically says if the table header is for a row or a column. Don't rely on the browser or the screen reader to "guess" at the meaning when you leave scope out.

There is nothing ambiguous about the direction of the header when scope is specified.

<table>
  <tr>
    <th scope="row">Name</th>
    <td>John Doe</td>
  </tr>
  <tr>
    <th scope="row">City</th>
    <td>Minneapolis</td>
  </tr>
  <tr>
    <th scope="row">State</th>
    <td>Minnesota</td>
  </tr>
  <tr>
    <th scope="row">Country</th>
    <td>United States</td>
  </tr>
  <tr>
    <th scope="row">Age</th>
    <td>42</td>
  </tr>
</table>

Now when a screen reader user navigates to a data cell, for example "Minneapolis", and then they navigate to the next cell down using a table navigation key (such as ctrl+alt+downarrow), they will hear the row label announced first and then the data cell value, such as "State, Minnesota".

话少情深 2025-01-17 12:00:25

由于没有多个人并且您列出了键/值对,因此 描述列表元素比表格元素更合适。

HTML 元素表示描述列表。该元素包含术语组列表(使用

元素指定)和描述(由
元素提供)。此元素的常见用途是...显示元数据(键值对列表)。

dl {
  display: grid;
  grid-template: auto / auto auto;
  justify-content: start;
  column-gap: 1ch;
}
dd {
  margin: 0;
}
<dl>
  <dt>name</dt>
  <dd>John Doe</dd>
  <dt>city</dt>
  <dd>Minneapolis</dd>
  <dt>state</dt>
  <dd>Minnesota</dd>
  <dt>country</dt>
  <dd>United States</dd>
  <dt>age</dt>
  <dd>42</dd>
</dl>

Since there's not multiple people and you're listing key/value pairs, a description list element would be more appropriate than a table element.

The <dl> HTML element represents a description list. The element encloses a list of groups of terms (specified using the <dt> element) and descriptions (provided by <dd> elements). Common uses for this element are...to display metadata (a list of key-value pairs).

dl {
  display: grid;
  grid-template: auto / auto auto;
  justify-content: start;
  column-gap: 1ch;
}
dd {
  margin: 0;
}
<dl>
  <dt>name</dt>
  <dd>John Doe</dd>
  <dt>city</dt>
  <dd>Minneapolis</dd>
  <dt>state</dt>
  <dd>Minnesota</dd>
  <dt>country</dt>
  <dd>United States</dd>
  <dt>age</dt>
  <dd>42</dd>
</dl>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文