如何将这个 4 列表转换为单列表,但仍然保持表格的可访问性

How to convert this 4 column table into single column table, but still maintaining the accessibility the table

提问人:Jitendra Vyas 提问时间:6/17/2011 更新时间:6/17/2011 访问量:4785

问:

我想使这个表格从 4 列 http://jsfiddle.net/B7SVK/ 为单列,因为我需要将这个表格从桌面网站转换为移动网站,而移动设备的宽度有限。我不想在网页中水平滚动。

那么有没有办法将这个 4 列表转换为单列表,同时保持相关数据单元格和标题之间的可访问性和关联

  <table>
        <caption>
        Contact Information
        </caption>
<thead>
        <tr>
                <th scope="col">Name</th>
                <th scope="col">Phone#</th>
                <th scope="col">Fax#</th>
                <th scope="col">City</th>
        </tr>
</thead>
<tbody>
        <tr>
                <th scope="row">Joel Garner</th>
                <td>412-212-5421</td>
                <td>412-212-5400</td>
                <td>Pittsburgh</td>
        </tr>
        <tr>
                <th scope="row">Clive Lloyd</th>
                <td>410-306-1420</td>
                <td>410-306-5400</td>
                <td>Baltimore</td>
        </tr>
        <tr>
                <th scope="row">Gordon Greenidge</th>
                <td>281-564-6720</td>
                <td>281-511-6600</td>
                <td>Houston</td>
        </tr>
</tbody>
</table>
HTML CSS、 XHTML 辅助功能 、语义标记

评论

0赞 clairesuzy 6/17/2011
您希望单列的外观如何(关于表格标题)
0赞 Jitendra Vyas 6/17/2011
@clairesuzy - 需要您的建议。数据应该在同一关联中是可读的。
0赞 clairesuzy 6/17/2011
在我的回答中添加了一个建议。我的想法意味着该表具有与HTML相同的关联,没有更改(没有代码重写;)) - 您只需要为您想要单列版本的设备提供单独的样式表。

答:

10赞 clairesuzy 6/17/2011 #1

您可以通过更改表格中的所有元素来完全重新设置表格样式,例如display: block;

table, thead, tbody, tr, th, td {display: block; text-align: left;}

这只是一个广泛的扫描,因为你也可以拥有其中一些元素,或者就好像它们是非表格元素一样。这取决于您希望单个列的外观。display: inline;float

也许生成的内容会有助于表格标题(视觉关联)..即通过隐藏元素并在“单元格”内容之前生成“标题”。将尝试制作样品小提琴<th>

示例 JSFiddle

小提琴中的代码:

html, body {margin: 0; padding: 0;}

table, thead, tbody, tr, th, td {display: block; text-align: left;}

thead {display: none;}

td:before {
float: left;
width: 200px;
background: #eee;
}

td {overflow: hidden;}

td:before {content: "Item";}
td+td:before {content: "Price (Euro)";}
td+td+td:before {content: "Postage and Packaging (Euro)";}
td+td+td+td:before {content: "Estimated Delivery Time";}

HTML:注意我把标题行放到一个 这样它就可以隐藏了<thead>

<table class="single-borders">
  <thead>
    <tr><th>Item</th><th>Price (Euro)</th><th>Postage and Packaging (Euro)</th><th>Estimated Delivery Time</th></tr>
  </thead>
  <tbody>
   <tr><td>Rocking Horse</td><td>€&nbsp;40.00</td><td>€&nbsp;20</td><td>3 working days</td></tr>
   <tr><td style="color:#666">Princess Costume *</td><td>€&nbsp;20.00</td><td>€&nbsp;5</td><td>Next day if ordered before 12.00</td></tr>
   <tr><td>Soldier Uniform</td><td>€&nbsp;20.00</td><td>€&nbsp;5</td><td>Next day if ordered before 12.00</td></tr>
   <tr><td>Pink Roller Skates</td><td>€&nbsp;35.00</td><td>€&nbsp;10</td><td>Next day if ordered before 12.00</td></tr>
   <tr><td style="color:#666">Black Roller Skates *</td><td>€&nbsp;35.00</td><td>€&nbsp;10</td><td>Next day if ordered before 12.00</td></tr>
  </tbody>
</table>

更新

根据注释更改为 176px 的单列;

试试这个CSS:

html, body {margin: 0; padding: 0;}

table, thead, tbody, tr, th, td {display: block; text-align: left;}

table {width: 176px;}

thead {display: none;}

tr {
 margin: 10px 0; 
 border: 1px solid #000;
}

td:before {
 display: block;
 background: #eee;
 font-weight: bold;
}

td:before {content: "Item";}
td+td:before {content: "Price (Euro)";}
td+td+td:before {content: "Postage and Packaging (Euro)";}
td+td+td+td:before {content: "Estimated Delivery Time";}

将显示更改为:

更新的小提琴

一旦你意识到你可以以这种方式设置表格的样式,即与任何其他元素相同 - 你几乎可以让它看起来像你想要的样子;)

评论

0赞 Jitendra Vyas 6/17/2011
感谢您的努力,但我想在单列中直观地显示所有内容,因为我需要以 176 px 的宽度显示此表。
0赞 clairesuzy 6/17/2011
@Jitendra..更新了一些 CSS,使其以 176px 的宽度显示,并且仍然有标题