Table 固定单元格和 upward-orientation header
Table with fixed cell and upward-orientation header
我想做这个
是否可以仅使用 HTML 和 CSS 来同时解决问题(upward-orientation 和方形单元格)?
upward-orientation 单元格宽度应匹配 line-height
如果我必须使用javascript,是否可以不使用任何框架来完成?
谢谢
编辑 1:
这是我目前所做的
<style>
table {
border-collapse: collapse;
}
table td {
border: 1px solid #555;
width: 150px;
height: 150px;
}
.vertical {
transform: rotateZ(-90deg);
-o-transform: rotateZ(-90deg);
-ms-transform: rotateZ(-90deg);
-moz-transform: rotateZ(-90deg);
-webkit-transform: rotateZ(-90deg);
}
</style>
<table>
<tr>
<td rowspan="2" colspan="2"></td>
<td colspan="2">Horizontal Header</td>
</tr>
<tr>
<td>Horizontal Sub-Header</td>
<td>Horizontal Sub-Header</td>
</tr>
<tr>
<td rowspan="2" class="vertical">Vertical Header</td>
<td class="vertical">Vertical Sub-Header</td>
<td>[0][0]</td>
<td>[0][1]</td>
</tr>
<tr>
<td class="vertical">Vertical Sub-Header</td>
<td>[1][0]</td>
<td>[1][1]</td>
</tr>
</table>
变成这样:
然后我更改 table header 的结构并使 .vertical class 像这样:
<style>
. . .
.vertical {
display: inline-block;
. . .
}
</style>
<table>
. . .
<td rowspan="2"><span class="vertical">Vertical Header</span></td>
<td><span class="vertical">Vertical Sub-Header</span></td>
. . .
</table>
并设法实现了这样的目标:
垂直 header 单元格的 width
未换行至垂直行高。
所以这是使用 CSS 网格布局的一种方法:
使用grid-template-areas
设置布局
grid-template-rows
和 grid-template-columns
将设置您的布局尺寸
设置垂直文本使用:
writing-mode: tb-rl;
transform: rotate(-180deg);
参见下面的演示:
body {
margin: 0;
}
* {
box-sizing: border-box;
}
.wrapper {
display: inline-grid;
grid-template-areas: '. . heading heading'
'. . sub-heading1 sub-heading2'
'heading-v sub-heading1-v s1 s2'
'heading-v sub-heading2-v s3 s4';
grid-template-rows: auto auto 150px 150px;
grid-template-columns: auto auto 150px 150px;
grid-gap: 1px;
background: cadetblue;
border: 1px solid cadetblue;
}
.wrapper>div {
background: #fff;
}
.vertical {
writing-mode: tb-rl;
transform: rotate(-180deg);
min-height: 0;
height: 100%;
}
.heading {
grid-area: heading;
}
.sub-heading1 {
grid-area: sub-heading1;
}
.sub-heading2 {
grid-area: sub-heading2;
}
.heading-v {
grid-area: heading-v;
}
.sub-heading1-v {
grid-area: sub-heading1-v;
}
.sub-heading2-v {
grid-area: sub-heading2-v;
}
.s1 {
grid-area: s1;
}
.s2 {
grid-area: s2;
}
.s3 {
grid-area: s3;
}
.s4 {
grid-area: s4;
}
<div class='wrapper'>
<div class="heading">Horizontal header</div>
<div class="sub-heading1">Horizontal sub-header</div>
<div class="sub-heading2">Horizontal sub-header</div>
<div class="heading-v vertical">Vertical header</div>
<div class="sub-heading1-v vertical">Vertical sub-header</div>
<div class="sub-heading2-v vertical">Vertical sub-header</div>
<div class="s1"></div>
<div class="s2"></div>
<div class="s3"></div>
<div class="s4"></div>
</div>
@kukkuz 的回答可能更好,但如果你想用实际的 table 来做,我想你可以使用 writing-mode
和 transform
CSS 选项.
所以可以这样做:
th,
td {
border: 1px solid black;
}
td {
width: 150px;
height: 150px;
}
.vertical-header span,
.vertical-header {
white-space: nowrap;
writing-mode: vertical-lr;
-webkit-writing-mode: vertical-lr;
-ms-writing-mode: vertical-lr;
}
.vertical-header span {
transform: rotate(0.5turn);
}
<html>
<body>
<table>
<tr>
<th></th>
<th></th>
<th colspan="2">h-header</th>
</tr>
<tr>
<th></th>
<th></th>
<th>sub-h-header1</th>
<th>sub-h-header2</th>
</tr>
<tr>
<th rowspan="2" class="vertical-header"><span>v-header</span></th>
<th class="vertical-header"><span>sub-v-header1</span></th>
<td>0,0</td>
<td>0,1</td>
</tr>
<tr>
<th class="vertical-header"><span>sub-v-header2</span></th>
<td>1,0</td>
<td>1,1</td>
</tr>
</table>
</body>
</html>
参考:
我想做这个
是否可以仅使用 HTML 和 CSS 来同时解决问题(upward-orientation 和方形单元格)?
upward-orientation 单元格宽度应匹配 line-height
如果我必须使用javascript,是否可以不使用任何框架来完成?
谢谢
编辑 1:
这是我目前所做的
<style>
table {
border-collapse: collapse;
}
table td {
border: 1px solid #555;
width: 150px;
height: 150px;
}
.vertical {
transform: rotateZ(-90deg);
-o-transform: rotateZ(-90deg);
-ms-transform: rotateZ(-90deg);
-moz-transform: rotateZ(-90deg);
-webkit-transform: rotateZ(-90deg);
}
</style>
<table>
<tr>
<td rowspan="2" colspan="2"></td>
<td colspan="2">Horizontal Header</td>
</tr>
<tr>
<td>Horizontal Sub-Header</td>
<td>Horizontal Sub-Header</td>
</tr>
<tr>
<td rowspan="2" class="vertical">Vertical Header</td>
<td class="vertical">Vertical Sub-Header</td>
<td>[0][0]</td>
<td>[0][1]</td>
</tr>
<tr>
<td class="vertical">Vertical Sub-Header</td>
<td>[1][0]</td>
<td>[1][1]</td>
</tr>
</table>
变成这样:
然后我更改 table header 的结构并使 .vertical class 像这样:
<style>
. . .
.vertical {
display: inline-block;
. . .
}
</style>
<table>
. . .
<td rowspan="2"><span class="vertical">Vertical Header</span></td>
<td><span class="vertical">Vertical Sub-Header</span></td>
. . .
</table>
并设法实现了这样的目标:
垂直 header 单元格的 width
未换行至垂直行高。
所以这是使用 CSS 网格布局的一种方法:
使用
grid-template-areas
设置布局
grid-template-rows
和grid-template-columns
将设置您的布局尺寸设置垂直文本使用:
writing-mode: tb-rl; transform: rotate(-180deg);
参见下面的演示:
body {
margin: 0;
}
* {
box-sizing: border-box;
}
.wrapper {
display: inline-grid;
grid-template-areas: '. . heading heading'
'. . sub-heading1 sub-heading2'
'heading-v sub-heading1-v s1 s2'
'heading-v sub-heading2-v s3 s4';
grid-template-rows: auto auto 150px 150px;
grid-template-columns: auto auto 150px 150px;
grid-gap: 1px;
background: cadetblue;
border: 1px solid cadetblue;
}
.wrapper>div {
background: #fff;
}
.vertical {
writing-mode: tb-rl;
transform: rotate(-180deg);
min-height: 0;
height: 100%;
}
.heading {
grid-area: heading;
}
.sub-heading1 {
grid-area: sub-heading1;
}
.sub-heading2 {
grid-area: sub-heading2;
}
.heading-v {
grid-area: heading-v;
}
.sub-heading1-v {
grid-area: sub-heading1-v;
}
.sub-heading2-v {
grid-area: sub-heading2-v;
}
.s1 {
grid-area: s1;
}
.s2 {
grid-area: s2;
}
.s3 {
grid-area: s3;
}
.s4 {
grid-area: s4;
}
<div class='wrapper'>
<div class="heading">Horizontal header</div>
<div class="sub-heading1">Horizontal sub-header</div>
<div class="sub-heading2">Horizontal sub-header</div>
<div class="heading-v vertical">Vertical header</div>
<div class="sub-heading1-v vertical">Vertical sub-header</div>
<div class="sub-heading2-v vertical">Vertical sub-header</div>
<div class="s1"></div>
<div class="s2"></div>
<div class="s3"></div>
<div class="s4"></div>
</div>
@kukkuz 的回答可能更好,但如果你想用实际的 table 来做,我想你可以使用 writing-mode
和 transform
CSS 选项.
所以可以这样做:
th,
td {
border: 1px solid black;
}
td {
width: 150px;
height: 150px;
}
.vertical-header span,
.vertical-header {
white-space: nowrap;
writing-mode: vertical-lr;
-webkit-writing-mode: vertical-lr;
-ms-writing-mode: vertical-lr;
}
.vertical-header span {
transform: rotate(0.5turn);
}
<html>
<body>
<table>
<tr>
<th></th>
<th></th>
<th colspan="2">h-header</th>
</tr>
<tr>
<th></th>
<th></th>
<th>sub-h-header1</th>
<th>sub-h-header2</th>
</tr>
<tr>
<th rowspan="2" class="vertical-header"><span>v-header</span></th>
<th class="vertical-header"><span>sub-v-header1</span></th>
<td>0,0</td>
<td>0,1</td>
</tr>
<tr>
<th class="vertical-header"><span>sub-v-header2</span></th>
<td>1,0</td>
<td>1,1</td>
</tr>
</table>
</body>
</html>
参考: