创建具有垂直方向文本的HTML表作为表头

时间:2012-02-24 16:59:38

标签: html css vertical-text

我想创建一个HTML表格,其中垂直书写的文本作为标题(即标题文本旋转90度)。我正在使用flollowing风格

<th style="-webkit-transform:rotate(90deg); writing-mode:tb-rl; -moz-transform:rotate(90deg); -o-transform: rotate(90deg); white-space:nowrap; display:blocking; padding-left:1px;padding-right:1px;padding-top:10px;padding-bottom:10px; " align="left"  id="#COLUMN_HEADER_NAME#">#COLUMN_HEADER#</th>

在MS IE 9.x中显示OK。在Firefox和Chrome中,标题似乎浮动在表格的顶部,它与下面的表格行重叠。有人可以帮忙吗?我很简单,不知道为什么会这样。我从本教程开始:http://scottgale.com/blog/css-vertical-text/2010/03/01/

TIA,Tamas

4 个答案:

答案 0 :(得分:15)

我不相信只用CSS旋转它的内容就可以改变<th>的高度。以下是我使用jQuery的方法。

http://jsfiddle.net/tsYRQ/1004/

答案 1 :(得分:5)

来自http://blog.petermares.com/2010/10/27/vertical-text-in-html-table-headers-for-webkitmozilla-browsers-without-using-images/

使用CSS在<div>元素内旋转元素(例如<td>)会导致列宽缩小以适应旋转的文本 - 但行高不会根据需要增长。 / p>

适用于Mac上的Chrome和Safari(至少对我而言)。

<html>
    <head>
        <style>
            th
            {
                background-color: grey;
                color: white;
                text-align: center;
                vertical-align: bottom;
                height: 150px;
                padding-bottom: 3px;
                padding-left: 5px;
                padding-right: 5px;
            }

            .verticalText
            {
                text-align: center;
                vertical-align: middle;
                width: 20px;
                margin: 0px;
                padding: 0px;
                padding-left: 3px;
                padding-right: 3px;
                padding-top: 10px;
                white-space: nowrap;
                -webkit-transform: rotate(-90deg); 
                -moz-transform: rotate(-90deg);                 
            };
        </style>
    </head>
    <body>

        <table>
            <tr>
                <th>Column 1</th>
                <th><div class="verticalText">Column 2</div></th>
                <th>Column 3</th>
                <th><div class="verticalText">Column 4</div></th>
                <th>Column 5</th>
                <th><div class="verticalText">Column 6</div></th>
                <th>Column 7</th>
                <th><div class="verticalText">Column 8</div></th>
                <th>Column 9</th>
            </tr>
            <tr>
                <td>Data 1</td>
                <td>Data 1</td>
                <td>Data 1</td>
                <td>Data 1</td>
                <td>Data 1</td>
                <td>Data 1</td>
                <td>Data 1</td>
                <td>Data 1</td>
                <td>Data 1</td>
            </tr>
            <tr>
                <td>Data 2</td>
                <td>Data 2</td>
                <td>Data 2</td>
                <td>Data 2</td>
                <td>Data 2</td>
                <td>Data 2</td>
                <td>Data 2</td>
                <td>Data 2</td>
                <td>Data 2</td>
            </tr>
        </table>

    </body>
</html>

答案 2 :(得分:1)

我认为有更简单的方法可以在表格单元格中获取垂直文本:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style>
        td {
        text-align: center;
        }
        /*Creation of vertical text in cell*/
        .vertical_Text {

                display: block;
                color: #f00;

                -webkit-transform: rotate(-90deg); 
                -moz-transform: rotate(-90deg);                 
            };
    </style>
</head>
<body>
<table border="1" cellspacing="10" cellpadding="20">
    <thead>
        <tr>
            <!-- vertical cell -->
            <td rowspan="2"><span class="vertical_Text" title="vertical text">HEAD</span></td>
            <td>header1-2</td>
            <td colspan="3">header1-2</td>
        </tr>

        <tr>
            <td>header2-1</td>
            <td>header2-2</td>
            <td>header2-3</td>
            <td>header2-4</td>
        </tr>

    </thead>
    <tbody>

        <!-- tr*5>td{data-$/$$}*5 -->
        <tr>
            <!-- vertical cell -->
            <td rowspan="5"><span  class="vertical_Text" title="vertical text">DATA</span></td>
            <td>data</td>
            <td>data</td>
            <td>data</td>
            <td>data</td>
        </tr>
        <tr>
            <td>data</td>
            <td>data</td>
            <td>data</td>
            <td>data</td>
        </tr>
        <tr>
            <td>data</td>
            <td>data</td>
            <td>data</td>
            <td>data</td>
        </tr>
        <tr>
            <td>data</td>
            <td>data</td>
            <td>data</td>
            <td>data</td>
        </tr>
        <tr>
            <td>data</td>
            <td>data</td>
            <td>data</td>
            <td>data</td>
        </tr>
    </tbody>
</table>
</body>
</html>

答案 3 :(得分:0)

你可以简单地用 CSS 来实现

table th {
    writing-mode: vertical-rl;
    text-orientation: mixed;
    text-align: right;
}

<!DOCTYPE html>
<html>
<head>
<style>
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}
table th {
    font-size: .85em;
    letter-spacing: .1em;
    text-transform: uppercase;
    writing-mode: vertical-rl;
    text-orientation: mixed;
    text-align: right;
}
td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
</style>
</head>
<body>

<h2>HTML Table</h2>

<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
</table>

</body>
</html>