简体   繁体   中英

Table header vertical th text

I have some problems with a large table that I want to display different so I have decided to show table header vertically 45deg. All good and fine when I have more registration but when I do a search in table that returned me just one r two registration the table look not nice and easy to use like in this attached picture:

在此处输入图片说明

So my question is how to make that columns to take a dynamic width instead of large left header and small right columns.

Here is a fiddle with my example :

HTML:

<table class="table table-striped table-header-rotated">
    <thead>
      <tr>
        <!-- First column header is not rotated -->
        <th></th>
        <!-- Following headers are rotated -->
        <th class="rotate-45"><div><span>Column header 1</span></div></th>
        <th class="rotate-45"><div><span>Column header 2</span></div></th>
        <th class="rotate-45"><div><span>Column header 3</span></div></th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th class="row-header">Row header 1</th>
        <td>33</td>
        <td>978</td>
        <td>57</td>
      </tr>
      <tr>
        <th class="row-header">Row header 2</th>
        <td>99</td>
        <td>678</td>
        <td>67</td>
      </tr>
      <tr>
        <th class="row-header">Row header 3</th>
        <td>332</td>
        <td>701</td>
        <td>877</td>
      </tr>
    </tbody>
  </table>

CSS:

.table{width:93%;}
.table-header-rotated th.row-header{
  width: auto ;
}
.table-header-rotated td{
  width: 40px;
  border-top: 1px solid #dddddd;
  border-left: 1px solid #dddddd;
  border-right: 1px solid #dddddd;
  vertical-align: middle;
  text-align: center;
}

.table-header-rotated th.rotate-45{
  height: 80px;
  position: relative;
  vertical-align: bottom;
  padding: 0;
  font-size: 12px;
  line-height: 0.8;
}

.table-header-rotated th.rotate-45 > div{
  position: relative;
  top: 0px;
  left: 40px; /* 80 * tan(45) / 2 = 40 where 80 is the height on the cell and 45 is the transform angle*/
  height: 100%;
  transform:skew(-45deg,0deg);
  overflow: hidden;
  border-left: 1px solid #dddddd;
  border-right: 1px solid #dddddd;
  border-top: 1px solid #dddddd;
}

.table-header-rotated th.rotate-45 span {
  transform:skew(45deg,0deg) rotate(315deg);
  position: absolute;
  bottom: 30px; /* 40 cos(45) = 28 with an additional 2px margin*/
  left: -25px; /*Because it looked good, but there is probably a mathematical link here as well*/
  display: inline-block;
  width: 85px; /* 80 / cos(45) - 40 cos (45) = 85 where 80 is the height of the cell, 40 the width of the cell and 45 the transform angle*/
  text-align: left;
}

Change the th in each line other than the thead rows to td :

Demo Fiddle

<div class="scrollable-table">
  <table class="table table-striped table-header-rotated">
    <thead>
      <tr>
        <!-- First column header is not rotated -->
        <th></th>
        <!-- Following headers are rotated -->
        <th class="rotate-45"><div><span>Column header 1</span></div></th>
        <th class="rotate-45"><div><span>Column header 2</span></div></th>
        <th class="rotate-45"><div><span>Column header 3</span></div></th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="row-header">Row header 1</td> <!-- needs to be TD not TH-->
        <td>33</td>
        <td>978</td>
        <td>57</td>
      </tr>
      <tr>
        <td class="row-header">Row header 2</td> <!-- needs to be TD not TH -->
        <td>99</td>
        <td>678</td>
        <td>67</td>
      </tr>
      <tr>
        <td class="row-header">Row header 3</td> <!-- needs to be TD not TH -->
        <td>332</td>
        <td>701</td>
        <td>877</td>
      </tr>
    </tbody>
  </table>
</div>

You can then align the first cells content using the rule:

.table-header-rotated th:first-of-type,.table-header-rotated td:first-of-type{
     text-align:left;   
}

Demo fiddle

I will suggest something like:

.row-header {
    max-width: 30%;
    min-width: 10%;
    width: auto;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM