Table vertical header?

后端 未结 6 678
自闭症患者
自闭症患者 2020-12-25 12:36

How can I make the table header appear on the left side of the table as a column instead on the top as a row? I have this markup:



        
                      
相关标签:
6条回答
  • 2020-12-25 13:13

    You can see the result here. You mean like this?

    <table border="1">
        <thead>
            <tr>
                <th></th>
                <th colspan="2">Letters</th>
            </tr>
            <tr>
                <th></th>
                <th>a</th>
                <th>b</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td rowspan="3">Numbers</td>
                <td>1</td>
                <td>4</td>
            </tr>
            <tr>
                <td>2</td>
                <td>5</td>
            </tr>
            <tr>
                <td>3</td>
                <td>6</td>
            </tr>
        </tbody>
    </table>
    

    You usually use rowspan and colspan for cells spanning multiple columns/rows.

    0 讨论(0)
  • 2020-12-25 13:14

    I needed something a little different, but the answers by @alex and @marion got me started in the right direction. The problem was that when you needed many items in the table, the "columns" started stacking funny on smaller screens.

    Thanks to Serge for his answer here that led me in this solution. This solution allows for scrolling horizontally and doesn't stack funny regardless of the size of the screen/window. I tested it in Chrome, Firefox, Opera, Edge, and IE11. Here's the fiddle with the correct alignment for the new "rows" and "columns": https://jsfiddle.net/berrym/6r3zvaef/21/

    And just in case it disappears from JSFiddle:

    <style>
    table{
      display:block;
      white-space:nowrap;
      width:100%;
    }
    td, th {
      border-bottom: 1px solid red;
      border-collapse: collapse;
    }
    thead {
      float: left;
      background: yellow;
      width: 10%;
    }
    thead tr {
      width:100%;
      float:left;
    }
    thead th {
      display: block;
    }
    tbody {
      float: left;
      width: 90%;
    }
    tbody tr {
      display: inline-block;
    }
    tbody td {
      float:left;
      width:100%;
    }
    </style>
    <table>
      <thead>
        <tr>
          <th>A</th>
          <th>B</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>a1</td>
          <td>b1</td>
        </tr>
        <tr>
          <td>a2</td>
          <td>b2</td>
        </tr>
        <tr>
          <td>a3</td>
          <td>b3</td>
        </tr>
        <tr>
          <td>a1</td>
          <td>b1</td>
        </tr>
        <tr>
          <td>a2</td>
          <td>b2</td>
        </tr>
        <tr>
          <td>a3</td>
          <td>b3</td>
        </tr>
        <tr>
          <td>a1</td>
          <td>b1</td>
        </tr>
        <tr>
          <td>a2</td>
          <td>b2</td>
        </tr>
        <tr>
          <td>a3</td>
          <td>b3</td>
        </tr>
        <tr>
          <td>a1</td>
          <td>b1</td>
        </tr>
        <tr>
          <td>a2</td>
          <td>b2</td>
        </tr>
        <tr>
          <td>a3</td>
          <td>b3</td>
        </tr>
        <tr>
          <td>a1</td>
          <td>b1</td>
        </tr>
        <tr>
          <td>a2</td>
          <td>b2</td>
        </tr>
        <tr>
          <td>a3</td>
          <td>b3</td>
        </tr>
        <tr>
          <td>a1</td>
          <td>b1</td>
        </tr>
        <tr>
          <td>a2</td>
          <td>b2</td>
        </tr>
        <tr>
          <td>a3</td>
          <td>b3</td>
        </tr>
        <tr>
          <td>a1</td>
          <td>b1</td>
        </tr>
        <tr>
          <td>a2</td>
          <td>b2</td>
        </tr>
        <tr>
          <td>a3</td>
          <td>b3</td>
        </tr>
        <tr>
          <td>a1</td>
          <td>b1</td>
        </tr>
        <tr>
          <td>a2</td>
          <td>b2</td>
        </tr>
        <tr>
          <td>a3</td>
          <td>b3</td>
        </tr>
        <tr>
          <td>a1</td>
          <td>b1</td>
        </tr>
        <tr>
          <td>a2</td>
          <td>b2</td>
        </tr>
        <tr>
          <td>a3</td>
          <td>b3</td>
        </tr>
        <tr>
          <td>a1</td>
          <td>b1</td>
        </tr>
        <tr>
          <td>a2</td>
          <td>b2</td>
        </tr>
        <tr>
          <td>a3</td>
          <td>b3</td>
        </tr>
        <tr>
          <td>a1</td>
          <td>b1</td>
        </tr>
        <tr>
          <td>a2</td>
          <td>b2</td>
        </tr>
        <tr>
          <td>a3</td>
          <td>b3</td>
        </tr>
        <tr>
          <td>a1</td>
          <td>b1</td>
        </tr>
        <tr>
          <td>a2</td>
          <td>b2</td>
        </tr>
        <tr>
          <td>a3</td>
          <td>b3</td>
        </tr>
        <tr>
          <td>a1</td>
          <td>b1</td>
        </tr>
        <tr>
          <td>a2</td>
          <td>b2</td>
        </tr>
        <tr>
          <td>a3</td>
          <td>b3</td>
        </tr>
      </tbody>
    </table>
    
    0 讨论(0)
  • 2020-12-25 13:22

    If you use bootstrap, you can achieve this easily with the table-reflow style: http://v4-alpha.getbootstrap.com/content/tables/#reflow

    0 讨论(0)
  • 2020-12-25 13:28

    How's this?

    Example

    Example

    CSS

    thead {
      float: left;   
    }
    
    thead th {
      display: block;   
    }
    
    tbody {
      float: right;   
    }
    

    jsFiddle.

    Update

    Well, the 1, 2 should also be as column, obviously.

    jsFiddle.

    It also looks like IE baulks at this. You may have to trade semantic-ness for cross browser compatibility.

    0 讨论(0)
  • 2020-12-25 13:33

    This worked perfectly for me : (inspired from the first answer)

    Example here

    html :

     <table>
      <thead>
        <tr>
          <th>A</th>
          <th>B</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>a1</td>
          <td>b1</td>
        </tr>
        <tr>
          <td>a2</td>
          <td>b2</td>
        </tr>
        <tr>
          <td>a3</td>
          <td>b3</td>
        </tr>
      </tbody>
    </table>
    

    css :

    table, td, th {
      border: 1px solid red;
    }
    
    thead {
      float: left;   
    }
    thead th {
      display: block;   
      background: yellow;
    }
    
    tbody {
      float: left;   
    }
    tbody tr {
      display: block;
      float: left;
    }
    tbody td {
      display: block;
    }
    
    0 讨论(0)
  • 2020-12-25 13:34

    Just use <th> as the first element in the row. Then add the scope attribute, which has no visual impact, but you could use it e.g. in CSS.

    <table>
      <tbody>
        <tr>
          <th scope="row">A</th>
          <td>b</td>
        </tr>
        <tr>
          <th scope="row">C</th>
          <td>d</td>
        </tr>
      </tbody>
    </table>
    

    See also http://www.w3.org/TR/WCAG20-TECHS/H63

    0 讨论(0)
提交回复
热议问题