我正在尝试将表格顶行中的文本对齐,以便使用旋转变换功能垂直显示。虽然我成功地旋转了表格标题中的单词,但是我无法将表格列的宽度缩短为与旋转标题相同的宽度。 (如果它是水平放置的话,它与标题的宽度保持相同)。另外,我使用百分比来表示列宽。
.vertical-th {
transform: rotate(-90deg);
}

<table>
<tr>
<th colspan="3" class="text-center risk-th" style="width: 20%">Controls</th>
<th class="risk-th" style="width: 4%">
<!--Manuality-->
</th>
<th class="risk-th" style="width: 4%">
<!--Probability-->
</th>
<th class="risk-th" style="width: 4%">
<!--Gravity-->
</th>
<th class="risk-th" style="width: 4%">
<!--Mitigation-->
</th>
<th colspan="3"></th>
<th class="vertical-th">Manuality</th>
<th class="vertical-th">Probability</th>
<th class="vertical-th">Gravity</th>
<th class="vertical-th">Mitigation</th>
</tr>
</table>
&#13;
答案 0 :(得分:3)
使用以下概念创建旋转表头的简单方法。
$(function() {
var header_height = 0;
$('.rotate-table-grid th span').each(function() {
if ($(this).outerWidth() > header_height) header_height = $(this).outerWidth();
});
$('.rotate-table-grid th').height(header_height);
});
table.rotate-table-grid{box-sizing: border-box;
border-collapse: collapse;}
.rotate-table-grid tr, .rotate-table-grid td, .rotate-table-grid th {
border: 1px solid #ddd;
position: relative;
padding: 10px;
}
.rotate-table-grid th span {
transform-origin: 0 50%;
transform: rotate(-90deg);
white-space: nowrap;
display: block;
position: absolute;
bottom: 0;
left: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="rotate-table-grid">
<thead>
<tr>
<th><span>Shorter Title</span></th>
<th><span>Much Much Longer Title</span></th>
<th><span>Shorter Title</span></th>
<th><span>Much Much Longer Title</span></th>
<th><span>Shorter Title</span></th>
<th><span>Much Much Longer Title</span></th>
<th><span>Shorter Title</span></th>
<th><span>Much Much Longer Title</span></th>
</tr>
</thead>
<tbody>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
</tbody>
</table>
答案 1 :(得分:1)
我们可以通过在DIV's
内扭曲标题文字并在其中th
和DIV's
上应用一些规则来做一些CSS技巧,然后我们可以获得更多样式化能力然后我们可以缩短即使文本很长,标题的宽度也是如此。
有些事情: 我希望它对您有所帮助,谢谢
th, td, table{
border:solid 1px;
}
div.vertical{
margin-left: -85px;
position: absolute;
width: 215px;
transform: rotate(-90deg);
-webkit-transform: rotate(-90deg); /* Safari/Chrome */
-moz-transform: rotate(-90deg); /* Firefox */
-o-transform: rotate(-90deg); /* Opera */
-ms-transform: rotate(-90deg); /* IE 9 */
}
th.vertical{
max-width: 50px;
height: 85px;
line-height: 14px;
padding-bottom: 20px;
text-align: inherit;
}
&#13;
<table>
<tr>
<th colspan="3" class="text-center risk-th" style="width: 20%">Controls</th>
<th class="risk-th" style="width: 4%">
<!--Manuality-->
</th>
<th class="risk-th" style="width: 4%">
<!--Probability-->
</th>
<th class="risk-th" style="width: 4%">
<!--Gravity-->
</th>
<th class="risk-th" style="width: 4%">
<!--Mitigation-->
</th>
<th class="vertical"><div class="vertical">Manuality</div></th>
<th class="vertical"><div class="vertical">Probability</div></th>
<th class="vertical"><div class="vertical">Gravity</div></th>
<th class="vertical"><div class="vertical">Mitigation</div></th>
</tr>
</table>
&#13;
答案 2 :(得分:1)
<table border="1">
<tr>
<th style="transform: rotate(-90deg);width: 1px;padding: 10px;" className="rotateth">head 1</th>
<th>head 2</th>
<th>head 3</th>
<th>head 4</th>
</tr>
<tr>
<td>data 1</td>
<td>data 2</td>
<td>data 3</td>
<td>data 4</td>
</tr>
</table>