我正在尝试在html表中旋转一些文本,但无论我做什么,我最终都会遇到一些对齐和文本流问题。
我尝试过使用写入模式的各种组合和css中的旋转,但无法解决问题。这是测试表:https://universaltheosophy.com/hpb/test-table.htm
这是代码:
table {
margin-left: auto;
margin-right: auto;
}
.c10 {
font-variant: small-caps;
}
.text-body-2-western {
text-align: center;
text-indent: 0em;
}
.c90bt {
-webkit-transform: rotate(270deg);
-moz-transform: rotate(270deg);
-ms-transform: rotate(270deg);
-o-transform: rotate(270deg);
transform: rotate(270deg);
}
<table cellpadding="2" cellspacing="0">
<col>
<col>
<col>
<tr>
<td rowspan="3">
<p class="text-body-2-western c90bt"><span class="c10">test title.</span><br>this is a test string of text rotated vertically that should fit within the table td limits.</p>
</td>
<td>
<p class="text-body-2-western">MAIN TITLE OF THE TABLE SHOULD CENTER HORIZONTALLY</p>
</td>
<td rowspan="3">
<p class="text-body-2-western c90bt"><span class="c10">test title.</span><br>this is a test string of text rotated vertically that should fit within the table td limits but for some reason reaches outside of it.</p>
</td>
</tr>
<tr>
<td>
<p class="text-body-2-western"><img src="https://universaltheosophy.com/resources/sd-2-300-blank.png"></p>
</td>
</tr>
<tr>
<td>
<p class="text-body-2-western text-body-no-spacing"><span class="c10">bottom title in small caps.</span></p>
</td>
</tr>
</table>
问题主要出现在两个地方:
编辑
为了解决这里的第一个评论(re:the rowspans),这里是没有rowspans的同一个表,结果相同:
table {
margin-left: auto;
margin-right: auto;
}
.c10 {
font-variant: small-caps;
}
.text-body-2-western {
text-align: center;
text-indent: 0em;
}
.c90bt {
-webkit-transform: rotate(270deg);
-moz-transform: rotate(270deg);
-ms-transform: rotate(270deg);
-o-transform: rotate(270deg);
transform: rotate(270deg);
}
<table cellpadding="5" cellspacing="0">
<col>
<col>
<col>
<tr>
<td colspan="3">
<p class="text-body-2-western">MAIN TITLE OF THE TABLE SHOULD CENTER HORIZONTALLY.</p>
</td>
</tr>
<tr>
<td>
<p class="text-body-2-western c90bt"><span class="c10">test title.</span><br>this is a test string of text rotated vertically that should fit within the table td limits.</p>
</td>
<td>
<p class="text-body-2-western"><img src="https://universaltheosophy.com/resources/sd-2-300-blank.png" class="img1"></p>
</td>
<td>
<p class="text-body-2-western c90bt"><span class="c10">test title.</span><br>this is a test string of text rotated vertically that should fit within the table td limits but for some reason reaches outside of it.</p>
</td>
</tr>
<tr>
<td colspan="3">
<p class="text-body-2-western text-body-no-spacing"><span class="c10">bottom title in small caps.</span></p>
</td>
</tr>
</table>
我使用CSS grid layout
解决了你的问题:
display: grid
创建网格布局 - 请注意,标记中的 
用于空网格项。min-content
创建grid-template-columns: repeat(3, min-content)
宽度的三列布局。grid-template-rows: 1fr min-content
将第二行调整到中间元素的高度writing-mode
:
.wmode {
writing-mode: tb-rl;
transform: rotate(-180deg);
}
见下面的演示:
.container {
display: grid;
grid-template-columns: repeat(3, min-content);
grid-template-rows: 1fr min-content;
justify-content: center;
}
.c10 {
font-variant: small-caps;
}
.text-body-2-western {
text-align: center;
text-indent: 0em;
}
.wmode {
writing-mode: tb-rl;
transform: rotate(-180deg);
}
<div class="container">
<p class="text-body-2-western">MAIN TITLE OF THE TABLE SHOULD CENTER HORIZONTALLY</p>
<p class="text-body-2-western wmode"><span class="c10">test title.</span><br>this is a test string of text rotated vertically that should fit within the table td limits.</p>
<img src="https://via.placeholder.com/400x338?text=image">
<p class="text-body-2-western wmode"><span class="c10">test title.</span><br>this is a test string of text rotated vertically that should fit within the table td limits but for some reason reaches outside of it.</p>
<p class="text-body-2-western text-body-no-spacing"><span class="c10">bottom title in small caps.</span></p>
</div>