0

我有一个简单的反应表,目前以以下格式显示:

[1][2][3][4]
[1][2][3][4]
[1][2][3][4]
[1][2][3][4]

但是我想添加一个媒体查询,以便在移动视图中转换为以下格式:

[1]
[2]
[3]
[4]

[1]
[2]
[3]
[4]

[1]
[2]
[3]
[4]

[1]
[2]
[3]
[4]

问题是我无法让它工作。我搜索和搜索,发现唯一可能的解决方案是向每个表格单元格添加一组顺序 CSS 值,例如:

<td className="cell-one"> One </td>

.cell-one {
  order: 1;
}

但这仍然没有奏效。逻辑就在那里,我可以看到一半,但我就是无法正确理解。任何有关如何实现这一目标的见解将不胜感激!

<table className="table-container">
  <tbody>
    <tr className="row">
      <th className="header-row">First Name</th>
      <th className="header-row">Surname</th>
      <th className="header-row">Age</th>
      <th className="header-row">Town</th>
    </tr>
    <tr className="row">
      <td className="table-content">James</td>
      <td className="table-content">Stout</td>
      <td className="table-content">35</td>
      <td className="table-content">Camden</td>
    </tr>
    <tr className="row">
      <td className="table-content">Karen</td>
      <td className="table-content">Smith</td>
      <td className="table-content">40</td>
      <td className="table-content">Stevenage</td>
    </tr>
  </tbody>
</table>
4

1 回答 1

1

添加这个 CSS

   @media screen and (min-width: 480px) {
     tbody tr th {
        display: none;
    }

    td, th {
        display: block;
    }

    td[data-th]:before  {
        content: attr(data-th);
        margin-right: 10px;
        font-weight: bold;
    }
}

@media screen and (min-width: 480px) {
     tbody tr th {
        display: none;
    }

    td, th {
        display: block;
    }

    td[data-th]:before  {
        content: attr(data-th);
        margin-right: 10px;
        font-weight: bold;
    }
}
<table className="table-container">
  <tbody>
<tr className="row">
  <th className="header-row">First Name</th>
  <th className="header-row">Surname</th>
  <th className="header-row">Age</th>
  <th className="header-row">Town</th>
</tr>
<tr className="row">
  <td data-th="First name" className="table-content">James</td>
  <td data-th="Surname" className="table-content">Stout</td>
  <td data-th="Age" className="table-content">35</td>
  <td data-th="Town" className="table-content">Camden</td>
</tr>
<tr className="row">
  <td data-th="First name" className="table-content">Karen</td>
  <td data-th="Surname" className="table-content">Smith</td>
  <td data-th="Age" className="table-content">40</td>
  <td data-th="Town" className="table-content">Stevenage</td>
</tr>
  </tbody>
</table>

于 2018-06-11T15:59:29.113 回答