根据设备方向转置HTML表

时间:2018-12-14 19:27:31

标签: html css html-table

说我有一个这样的表:

x A B C D E F
1 a b c d e f
2 g h i j k l

现在,当设备处于纵向方向(或者只是窗口的宽度太小)时,我想像这样显示此表:

x 1 2 
A a g
B b h
C c i 
D d j
E e k
F f l

是否可以在不使用JavaScript的情况下实现这一目标?

4 个答案:

答案 0 :(得分:2)

您可以使用网格系统创建一些表并按断点显示它们。例如使用Boostrap 4:

.row > .col, .row > [class^="col-"] {
    padding-top: .75rem;
    padding-bottom: .75rem;
    background-color: rgba(86,61,124,.15);
    border: 1px solid rgba(86,61,124,.2);
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<div class="d-none d-sm-block">
  <div class="row">
    <div class="col">x</div>
    <div class="col">A</div>
    <div class="col">B</div>
    <div class="col">C</div>
    <div class="col">D</div>
    <div class="col">E</div>
    <div class="col">F</div>
  </div>
  <div class="row">
    <div class="col">1</div>
    <div class="col">a</div>
    <div class="col">b</div>
    <div class="col">c</div>
    <div class="col">d</div>
    <div class="col">e</div>
    <div class="col">f</div>
  </div>
  <div class="row">
    <div class="col">2</div>
    <div class="col">g</div>
    <div class="col">h</div>
    <div class="col">i</div>
    <div class="col">j</div>
    <div class="col">q</div>
    <div class="col">l</div>
  </div>
</div>

<div class="d-sm-none">
  <div class="row">
    <div class="col">x</div>
    <div class="col">1</div>
    <div class="col">2</div>
  </div>
  <div class="row">
    <div class="col">A</div>
    <div class="col">a</div>
    <div class="col">g</div>
  </div>
  <div class="row">
    <div class="col">B</div>
    <div class="col">b</div>
    <div class="col">h</div>
  </div>
  <div class="row">
    <div class="col">C</div>
    <div class="col">c</div>
    <div class="col">i</div>
  </div>
  <div class="row">
    <div class="col">D</div>
    <div class="col">d</div>
    <div class="col">j</div>
  </div>
  <div class="row">
    <div class="col">E</div>
    <div class="col">e</div>
    <div class="col">k</div>
  </div>
  <div class="row">
    <div class="col">F</div>
    <div class="col">f</div>
    <div class="col">l</div>
  </div>
</div>

答案 1 :(得分:1)

您可以将所有内容绝对放置,并使用媒体查询来使用另一组CSS坐标,但这很疯狂,不再是“ HTML表格”。

答案 2 :(得分:1)

学习grid。这很容易。

div {
  display:grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
  width:300px;
}

@media screen and (max-width:600px){
  div {
    grid-template-columns: 1fr 1fr 1fr;
    width:100px;
}
}
<div>
  <span>X</span>
  <span>A</span>
  <span>B</span>
  <span>C</span>
  <span>D</span>
  <span>E</span>
  <span>F</span>
  <span>1</span>
  <span>a</span>
  <span>b</span>
  <span>c</span>
  <span>d</span>
  <span>e</span>
  <span>f</span>
  <span>2</span>
  <span>g</span>
  <span>i</span>
  <span>j</span>
  <span>k</span>
  <span>l</span>
  <span>h</span>
</div>

答案 3 :(得分:1)

CSS允许这样做,但Edge,FF或Chrome都无法正确执行。 Chrome会忽略单元格的书写模式,FF不会为单元格提供足够的高度,而Edge则使旋转后的桌子超高。

http://jsfiddle.net/dgrogan/z0fhj7gq/3/

let state = 0;
change.onclick = function() {
  outer.style.writingMode = (state ? "horizontal-tb" : "vertical-lr");
  state = !state;
}
td,
th {
  writing-mode: horizontal-tb;
}
<button id=change>rotate
</button>

<div id=outer style="writing-mode:horizontal-tb; border:1px solid red;">
  <table>
    <tr>
      <th>1</th>
      <th>2</th>
      <th>3</th>
    </tr>
    <tr>
      <td>4</td>
      <td>5</td>
      <td>6</td>
    </tr>
  </table>

</div>