简体   繁体   English

CSS旋转表格单元格中的内容和高度调整

[英]CSS rotate content in table cell and height adjustment

I have a html table, and the column widths are fixed. 我有一个html表,并且列宽是固定的。 I have content in one column that needs to be rotated 270 degrees. 我在一列中的内容需要旋转270度。 The length of the content is dynamic and can vary. 内容的长度是动态的,可以变化。 I have the code below, but the problem is when it rotates, firstly the width of the table cell expands and the height doesnt and as a result the content just goes upwards and disappears. 我有下面的代码,但是问题是当它旋转时,首先表格单元的宽度会扩大,而高度却不会,因此内容只会向上而消失。 And also it doesnt align to the bottom of the cell. 而且它不与单元格的底部对齐。

I would like the width of the cell to remain the fixed with that I have defined but the height needs to auto adjust. 我希望单元格的宽度保持与定义的固定不变,但高度需要自动调整。 Here is a JSFiddle: https://jsfiddle.net/d7c4q924/1/ 这是一个JSFiddle: https ://jsfiddle.net/d7c4q924/1/

And here is the code I have currently: 这是我目前拥有的代码:

<style type="text/css">
.container{
width:100%; 
display: inline-block; 
white-space: nowrap; 
transform: rotate(270deg); 
transform-origin: left top 0;
}
</style>



<table border="1" width="600px">
<tr>
<td width="100px">column1</td>
<td width="100px"><span class="container">this column has a dynamic length string</span></td>
<td width="100px">column3</td>
<td width="300px">column4</td>
</tr>
</table>

I did it perfectly with a bit of javascript: 我用一些JavaScript做到了完美:

HTML HTML

<table border="1" >
  <tr>
    <td>column1</td>
    <td>
     <div class="container">
       this column has a dynamic length string
     </div>
    </td>
    <td>
     <div class="container">
       this column has a dynamic length string longer than the other
     </div>
    </td>
    <td>column4</td>
  </tr>
</table>

CSS CSS

.container{
  position: relative;
  white-space: nowrap;
  transform-origin: left top 0;
  padding: 10px;
}

Javascript 使用Javascript

You have to set padding variable at the same value as the padding in the css. 您必须将padding变量设置为与CSS中的padding相同的值。

var containers = document.getElementsByClassName('container');
var maxWidth = 0;
var padding = 10;
for (i = 0; i < containers.length; i++) {
  var container = containers[i];
  if (container.clientWidth > maxWidth) maxWidth = container.clientWidth;
}
var i=0;
for (i = 0; i < containers.length; i++) {
  var container = containers[i];
  var width = container.clientWidth;
  var height = container.clientHeight;
  container.style.height = maxWidth + 'px';
  container.style.width = height + 'px';
  container.style.transform = 'rotate(270deg) translateX(-' + (maxWidth + padding) + 'px) translateY(' + padding + 'px)';
}

You should put the javascript into an window.onload event. 您应该将javascript放入window.onload事件中。 Here is my fiddle: https://jsfiddle.net/d7c4q924/5/ 这是我的小提琴: https : //jsfiddle.net/d7c4q924/5/

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

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