简体   繁体   English

如何旋转表格标题单元格并自动调整为内容大小?

[英]How could I rotate table header cells and automatically adjust to content size?

The table I am trying to design is going to end up to be somewhat dynamic. 我要设计的表最终将变得有些动态。 I would like the table header cells to appear rotated (to save horizontal space). 我希望表格标题单元格显示为旋转(以节省水平空间)。 I am aware there might be better methods to save some table space, but as I was playing around with some HTML, I got interested in something that seemed quite impossible. 我知道可能有更好的方法来节省一些表空间,但是当我玩一些HTML时,我对似乎不太可能的事情产生了兴趣。

Within following piece of code I tried to rotate the cells: 在以下代码中,我尝试旋转单元格:

 th { background-color: #ccc; } th, td { border: 1px solid #000; } .rotate { transform: rotate(-90deg); text-align: center; margin-bottom: 100px;/* <-- Anything I could do here? */ } 
 <table cellspacing="0" cellpadding="0"> <thead> <tr> <th class="rotate"> Foo </th> <th class="rotate"> Bar </th> </tr> </thead> <tbody> <tr> <td> Foo collection </td> <td> Bar collection </td> </tr> </tbody> </table> 

I found some code in a couple other questions in order to address my issue(s), however most of these questions do not address the issue(s) I am having: 我在其他几个问题中找到了一些代码来解决我的问题,但是其中大多数问题都无法解决我遇到的问题:

Whenever given table cell's content would increase (eg more characters and/or words), content would flow either outside of the given cell or break the appearance of the cell. 只要给定表格单元格的内容增加(例如,更多的字符和/或单词),内容就会在给定单元格之外流动或破坏单元格的外观。

First off I would like my table cells to stay where they are supposed to (so no hovering over other cells like what is happening in my fiddle above): 首先,我希望表格单元格停留在它们应该放置的位置(因此,不要像上面我的小提琴中那样,将鼠标悬停在其他单元格上):

示例表1

and secondly I would like to be able to adjust the text inside these cells, meaning that the cells must be able to resize without breaking the table's layout: 其次,我希望能够调整这些单元格中的文本,这意味着这些单元格必须能够调整大小而不会破坏表格的布局:

示例表2

In other words: I would like to create rotated table header cells without losing any of HTML's table default functionalities. 换句话说:我想创建旋转的表头单元,而不会丢失任何HTML的表默认功能。 Is it possible to rotate a table header cell ( <th> element) without breaking the table? 是否可以在不破坏表的情况下旋转表标题单元格( <th>元素)?

When you want to change the write direction of the text, you can use writing-mode . 当您想更改文本的书写方向时,可以使用writing-mode In this case I use writing-mode: vertical-lr; 在这种情况下,我使用writing-mode: vertical-lr; , which makes the text vertical, and the container height will change to fit the text. ,它使文本垂直,并且容器高度将更改以适合文本。 We also need to rotate the text in place, but in the future I would use sideways-lr , which lacks support now. 我们还需要将文本旋转到适当的位置,但是将来我会使用sideways-lr ,该语言现在没有支持。

 th { background-color: #ccc; } th, td { border: 1px solid #000; } .rotate span { writing-mode: vertical-rl; transform: rotate(180deg); } 
 <table cellspacing="0" cellpadding="0"> <thead> <tr> <th class="rotate"> <span>Foo</span> </th> <th class="rotate"> <span>Foo Bar Bazz</span> </th> <th class="rotate"> <span>FooBar</span> </th> </tr> </thead> <tbody> <tr> <td> Foo collection </td> <td> Foo Bar Bazz collection </td> <td> Foo Bar collection </td> </tr> </tbody> </table> 

If you mean to change them dynamically then create a CSS class and add: 如果要动态更改它们,请创建一个CSS类并添加:

.rotate-text {
    //for old browsers
    -moz-transform: rotate(90deg);
    -webkit-transform: rotate(90deg) ;
    -o-transform: rotate(90deg) ;
    -ms-transform: rotate(90deg) ;
    //for all modern browsers
    transform: rotate(90deg);
}

Now use jQuery to check if number of words increased the target, if true toggle the class created above inside your TD or TH 现在使用jQuery检查单词数是否增加了目标,如果为true,则在TD或TH中切换上面创建的类

jQuery("th").toggleClass("rotate-text");

I hope this helps you. 我希望这可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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