Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table with frozen header with vertical text

I wish to create a table where the thread is frozen and the labels in the thead are orientated in a vertical manner. My attempt is below, however there are a number of issues with this implementation since I am very new to css.

  1. One thing I don't like about my solution is that it is not obvious that there are more rows to be scrolled into view as the scroll bar is hidden until you start scrolling the table. Is there a way of making the scroll bar permanently visible?

  2. There is also a large scape at the top of the thead above where the party names are displayed. I can make this gap bigger by playing around with the values in th.vertical but I cant eliminate it. How can I force the thead to be only as high as the text it contains?

  3. Finally, I would like the vertical text in the thead to be centred with respect to the column values but I haven't figured out how to do this. How might I do this?

  4. I would also like to be able to sort on any column.

table thead tr {
  display: block;
}

table th,
table td {
  width: 75px; //fixed width
}

div.vertical {
  margin-left: -100px;
  position: absolute;
  width: 210px;
  transform: rotate(-90deg);
  -webkit-transform: rotate(-90deg);
  /* Safari/Chrome */
  -moz-transform: rotate(-90deg);
  /* Firefox */
  -o-transform: rotate(-90deg);
  /* Opera */
  -ms-transform: rotate(-90deg);
  /* IE 9 */
}

th.vertical {
  height: 60px;
  line-height: 14px;
  padding-bottom: 100px;
  text-align: left;
}

table tbody {
  display: block;
  height: 500px;
  overflow: auto;
}
<table class='vrt-header'>
  <thead>
    <tr>
      <th></th>
      <th class="vertical">
        <div class="vertical">Republican</div>
      </th>
      <th class="vertical">
        <div class="vertical">Democrat</div>
      </th>
      <th class="vertical">
        <div class="vertical">Libertarian</div>
      </th>
      <th class="vertical">
        <div class="vertical">Green</div>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        Alabama
      </td>
      <td>
        65
      </td>
      <td>
        25
      </td>
      <td>
        10
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        Alaska
      </td>
      <td>
        75
      </td>
      <td>
        15
      </td>
      <td>
        10
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        Arizona
      </td>
      <td>
        55
      </td>
      <td>
        40
      </td>
      <td>
        5
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        Arkansas
      </td>
      <td>
        65
      </td>
      <td>
        25
      </td>
      <td>
        10
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        California
      </td>
      <td>
        25
      </td>
      <td>
        65
      </td>
      <td>
        3
      </td>
      <td>
        7
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>
  </tbody>
</table>
like image 344
Baz Avatar asked Jun 08 '18 15:06

Baz


People also ask

How do I freeze headers in a table?

To freeze the row/column we can use a simple HTML table and CSS. HTML: In HTML we can define the header row by <th> tag or we can use <td> tag also. Below example is using the <th> tag. We also put the table in DIV element to see the horizontal and vertical scrollbar by setting the overflow property of the DIV element.

How do you vertically rotate text in HTML table?

The first trick is to use writing-mode: vertical-lr to get the text to run vertically. By itself, the text runs top to bottom, but we want it to run bottom to top, so we spin it around it with the transform: rotate(180deg) . The default transform origin is the center of the element, so this works out great.

How do I fix a scrolling header in a table?

By setting postion: sticky and top: 0, we can create a fixed header on a scroll in HTML tables.


1 Answers

I've played with your code…

  1. I don't know about another way to always show the scrollbar than overflow-y: scroll;.
  2. I modified all the styling of your headers, but I couldn't find an easy way to automatically adjust the height after your transform. I used a fixed value.
  3. The new styling is correcting this point.
  4. I suggest you to try and read about the Stupid-Table-Plugin for jQuery to easily sort a table.

… Plus, I added:

  • Some styling on :hover to highlight the action on the ths.
  • An alternation of color for the rows, using the :nth-of-type() selector.

… and I ended-up with this snippet:

// Doc here: https://joequery.github.io/Stupid-Table-Plugin/
$("#simpleTable").stupidtable();
table thead,
table tbody {
  display: block;
}

table tbody {
  height: 500px;
  overflow-y: scroll; /* To make the scroll bar always visible */
}

thead th {
  position: relative;
  cursor: pointer; /* change cursor to show we can do an action */
  height: 90px;
  background: #ccc;
  border-bottom: 2px solid black;
}

thead th:hover {
  background: #ccf; /* Styling on hover */
}

thead th p {
  position: absolute;
  bottom: 20px;
  transform: rotate(-90deg);
  text-align: left;
}

table th,
table th p,
table td {
  width: 75px;
}

table td {
  text-align: center;
}

table tr td:first-of-type {
  text-align: left;
}

table tr:nth-of-type(odd) td {
  background: #eee;
}

table tr:nth-of-type(even) td {
  background: #ddd;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stupidtable/1.1.3/stupidtable.min.js"></script>

<table id="simpleTable" class='vrt-header'>

  <thead>
    <tr>
      <th data-sort="string"></th>
      <th data-sort="int">
        <p>Republican</p>
      </th>
      <th data-sort="int">
        <p>Democrat</p>
      </th>
      <th data-sort="int">
        <p>Libertarian</p>
      </th>
      <th data-sort="int">
        <p>Green</p>
      </th>
    </tr>
  </thead>

  <tbody>

    <tr>
      <td>
        Alabama
      </td>
      <td>
        65
      </td>
      <td>
        25
      </td>
      <td>
        10
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        Alaska
      </td>
      <td>
        75
      </td>
      <td>
        15
      </td>
      <td>
        10
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        Arizona
      </td>
      <td>
        55
      </td>
      <td>
        40
      </td>
      <td>
        5
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        Arkansas
      </td>
      <td>
        65
      </td>
      <td>
        25
      </td>
      <td>
        10
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        California
      </td>
      <td>
        25
      </td>
      <td>
        65
      </td>
      <td>
        3
      </td>
      <td>
        7
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

    <tr>
      <td>
        ?
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
      <td>
        0
      </td>
    </tr>

  </tbody>


</table>

Hope it helps.

like image 129
Takit Isy Avatar answered Sep 21 '22 14:09

Takit Isy