如何使表格单元垂直和拉伸/缩小以适合内容?

时间:2018-08-01 01:27:22

标签: html css html-table

为了节省屏幕空间,我正在尝试将表格标题单元格(th)转换为单个垂直文本行。

我已经在标头单元格中添加了.vertical-text {transform: rotate(-90deg); white-space: nowrap;}来实现此目的,但是它具有一些奇怪的效果。

问题:

1)文本不直接位于其列的顶部。它位于侧面,很难区分哪个标题属于哪一列。

2)标题单元格的高度不适合转换后的文本的垂直高度,因此被切除。我需要标题行进行拉伸以适应文本的新高度。

3)在使文本垂直之后,列的宽度不会缩小,它们保持与文本不是垂直时相同的宽度。使标题单元格垂直的主要目的是节省空间,但这样做并不会调整列宽的大小。

.container {
  width: 80%;
  margin: 0 auto;
  display: flex;
  align-items: center;
  flex-direction: column;
}

.vertical-text {
  transform: rotate(-90deg);
  white-space: nowrap;
}

table {
  border-collapse: collapse;
  background: white;
  border-radius: 10px;
  overflow: hidden;
  min-width: 100%;
  position: relative;
  margin: 0 -20px;
}

table * {
  position: relative;
}

table td,
table th {
  padding-left: 8px;
}

table thead tr {
  background: #36304a;
}

table tbody tr {
  height: 50px;
}

table tbody tr:last-child {
  border: 0;
}

table td,
table th {
  text-align: left;
}

table td.l,
table th.l {
  text-align: right;
}

table td.c,
table th.c {
  text-align: center;
}

table td.r,
table th.r {
  text-align: center;
}

.header th {
  font-size: 18px;
  color: #fff;
  line-height: 1.2;
  font-weight: unset;
}

tbody tr:nth-child(even) {
  background-color: #f5f5f5;
}

tbody tr {
  font-size: 15px;
  color: #808080;
  line-height: 1.2;
  font-weight: unset;
}

tbody tr:hover {
  color: #555555;
  background-color: #f5f5f5;
  cursor: pointer;
}

.column {
  width: 160px;
}

.column:first-child {
  padding-left: 40px;
}

.column:last-child {
  padding-right: 40px;
}
<div class="container">
  <table>
    <thead>
      <tr class="header">
        <th class="column vertical-text"></th>
        <th class="column vertical-text">Remove Apps</th>
        <th class="column vertical-text">Restrict App Access</th>
        <th class="column vertical-text">Block Tracking</th>
        <th class="column vertical-text">Disable Ads</th>
        <th class="column vertical-text">Disable Scheduler</th>
        <th class="column vertical-text">Backup</th>
        <th class="column vertical-text">Restore</th>
        <th class="column vertical-text">Misc</th>
        <th class="column vertical-text">Last Updated</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="column">Some Name Here</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
      </tr>
      <tr>
        <td class="column">Some Name Here</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
      </tr>
      <tr>
        <td class="column">Some Name Here</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
      </tr>


    </tbody>
  </table>
</div>

1 个答案:

答案 0 :(得分:0)

Just Add
th.vertical-text p{
    writing-mode: vertical-rl;
    text-orientation: upright;
}

.container {
  width: 80%;
  margin: 0 auto;
  display: flex;
  align-items: center;
  flex-direction: column;
}

.vertical-text {
     
}
th.vertical-text p{
writing-mode: vertical-rl;
text-orientation: upright;
}

table {
  border-collapse: collapse;
  background: white;
  border-radius: 10px;
  overflow: hidden;
  min-width: 100%;
  position: relative;
  margin: 0 -20px;
}

table * {
  position: relative;
}

table td,
table th {
  padding-left: 8px;
}

table thead tr {
  background: #36304a;
}

table tbody tr {
  height: 50px;
}

table tbody tr:last-child {
  border: 0;
}

table td,
table th {
  text-align: left;
}

table td.l,
table th.l {
  text-align: right;
}

table td.c,
table th.c {
  text-align: center;
}

table td.r,
table th.r {
  text-align: center;
}

.header th {
  font-size: 18px;
  color: #fff;
  line-height: 1.2;
  font-weight: unset;
}

tbody tr:nth-child(even) {
  background-color: #f5f5f5;
}

tbody tr {
  font-size: 15px;
  color: #808080;
  line-height: 1.2;
  font-weight: unset;
}

tbody tr:hover {
  color: #555555;
  background-color: #f5f5f5;
  cursor: pointer;
}

.column {
  width: 160px;
}

.column:first-child {
  padding-left: 40px;
}

.column:last-child {
  padding-right: 40px;
}
<div class="container">
  <table>
    <thead>
      <tr class="header">
        <th class="column vertical-text"><p></p></th>
        <th class="column vertical-text"><p>Remove Apps</p></th>
        <th class="column vertical-text"><p>Restrict App Access</p></th>
        <th class="column vertical-text"><p>Block Tracking</p></th>
        <th class="column vertical-text"><p>Disable Ads</p></th>
        <th class="column vertical-text"><p>Disable Scheduler</p></th>
        <th class="column vertical-text"><p>Backup</p></th>
        <th class="column vertical-text"><p>Restore</p></th>
        <th class="column vertical-text"><p>Misc</p></th>
        <th class="column vertical-text"><p>Last Updated</p></th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="column">Some Name Here</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
      </tr>
      <tr>
        <td class="column">Some Name Here</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
      </tr>
      <tr>
        <td class="column">Some Name Here</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">Yes</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">No</td>
        <td class="column" data-title="Points">Yes</td>
      </tr>


    </tbody>
  </table>
</div>