表头垂直th文本

时间:2014-11-20 10:28:58

标签: jquery html css3 html-table

我有一个大表的问题,我想显示不同,所以我决定垂直显示表头45deg。当我有更多的注册时,一切都很好,但是当我在桌子上进行搜索时只返回一两个注册表,这个表看起来不太好用,就像在这张附图中一样:

enter image description here

所以我的问题是如何使这些列采用动态宽度而不是大的左头和小右列。

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;
}

2 个答案:

答案 0 :(得分:2)

th行以外的每一行thead更改为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>

然后,您可以使用以下规则对齐第一个单元格内容:

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

Demo fiddle

答案 1 :(得分:1)

我会建议像:

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