css 倾斜表格文本与我们在MS Excel中的文本方向中获得的文本相同

nhjlsmyf  于 2022-12-24  发布在  其他
关注(0)|答案(1)|浏览(91)

我想为我的HTML表格创建倾斜的标题,如下面Excel的截图所示。有没有办法用CSS实现这个设计

我尝试使用Flex我的HTML代码:

.container {
  display: flex;
  justify-content: space-between;
}

.container > * {
  flex: 1;
  border: 1px solid black;
  padding: 0px 2px;
  transform: rotate(-45deg);
}
<div class="container">
  <div class="matrix-column">
    Column 1
  </div>
  <div class="matrix-column">
    Column 2
  </div>
  <div class="matrix-column">
    Column 3
  </div>
  <div class="matrix-column">
    Column 4
  </div>
  <div class="matrix-column">
    Column 5
  </div>
  <div class="matrix-column">
    Column 6
  </div>
</div>

Codepen链接:https://codepen.io/a-arora/pen/QWBWvxK
提前感谢您的帮助!:)

67up9zun

67up9zun1#

我找到了两个在线资源,指出了解决方案。

使用transform: rotate()translate()

rotate()用于调整页眉Angular ,translate()用于调整位置。原始文章可在here中找到。
以下片段:

.table-header-rotated {
  border-collapse: collapse;
}
.csstransforms .table-header-rotated td {
  width: 30px;
}
.no-csstransforms .table-header-rotated th {
  padding: 5px 10px;
}
.table-header-rotated td {
  text-align: center;
  padding: 10px 5px;
  border: 1px solid #ccc;
}
.csstransforms .table-header-rotated th.rotate {
  height: 140px;
  white-space: nowrap;
}
.csstransforms .table-header-rotated th.rotate > div {
  transform: translate(25px, 51px) rotate(315deg);
  width: 30px;
}
.csstransforms .table-header-rotated th.rotate > div > span {
  border-bottom: 1px solid #ccc;
  padding: 5px 10px;
}
.table-header-rotated th.row-header {
  padding: 0 10px;
  border-bottom: 1px solid #ccc;
}
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/modernizr-2.7.1.js"></script>
<table class="table table-header-rotated">
  <thead>
    <tr>
      <!-- First column header is not rotated -->
      <th></th>
      <!-- Following headers are rotated -->
      <th class="rotate"><div><span>Column header 1</span></div></th>
        <th class="rotate"><div><span>Column header 2</span></div></th>
        <th class="rotate"><div><span>Column header 3</span></div></th>
        <th class="rotate"><div><span>Column header 4</span></div></th>
        <th class="rotate"><div><span>Column header 5</span></div></th>
        <th class="rotate"><div><span>Column header 6</span></div></th>
    </tr> 
  </thead>
  <tbody>
    <tr>
      <th class="row-header">Row header 1</th>
      <td><input checked="checked" name="column1[]" type="radio" value="row1-column1"></td>
      <td><input checked="checked" name="column2[]" type="radio" value="row1-column2"></td>
      <td><input name="column3[]" type="radio" value="row1-column3"></td>
      <td><input name="column4[]" type="radio" value="row1-column4"></td>
      <td><input name="column5[]" type="radio" value="row1-column5"></td>
      <td><input name="column6[]" type="radio" value="row1-column6"></td>
    </tr>
    <tr>
      <th class="row-header">Row header 2</th>
      <td><input name="column1[]" type="radio" value="row2-column1"></td>
      <td><input name="column2[]" type="radio" value="row2-column2"></td>
      <td><input checked="checked" name="column3[]" type="radio" value="row2-column3"></td>
      <td><input checked="checked" name="column4[]" type="radio" value="row2-column4"></td>
      <td><input name="column5[]" type="radio" value="row2-column5"></td>
      <td><input name="column6[]" type="radio" value="row2-column6"></td>
    </tr>
    <tr>
      <th class="row-header">Row header 3</th>
      <td><input name="column1[]" type="radio" value="row3-column1"></td>
      <td><input name="column2[]" type="radio" value="row3-column2"></td>
      <td><input name="column3[]" type="radio" value="row3-column3"></td>
      <td><input name="column4[]" type="radio" value="row3-column4"></td>
      <td><input checked="checked" name="column5[]" type="radio" value="row3-column5"></td>
      <td><input checked="checked" name="column6[]" type="radio" value="row3-column6"></td>
    </tr>
  </tbody>
</table>

使用transform: rotate()skew()

与上面类似,但使用skew来调整位置。请参阅原始Codepen示例here
以下片段:
一个二个一个一个

相关问题