0

I have some writing-mode: vertical-rl text in a table cell. By default, it gets pushed to the left side of the cell, but I want to center it or push it right. If I were handling horizontal-tb text, I would use text-align: center or vertical-align but there doesn't seem to be a corresponding horizontal-align property.

In this example, I want the vertical text centered in the cell:

Vertical text is pushed left, but I want it centered

I am testing this in Firefox 91.

Here is a jsfiddle for the above image: https://jsfiddle.net/a965ej2x/

And the corresponding code:

<table>
<tbody>
  <tr>
    <th>This is my heading</th>
  </tr>
  <td>
    しょうがない
  </td>
</tbody>
</table>

CSS:

td {
  writing-mode: vertical-rl;
  text-align: center;
  vertical-align: middle;
}
td, th {
  border: 1px solid black;
}
erjiang
  • 44,417
  • 10
  • 64
  • 100

2 Answers2

1

It seems to be a bug on Firefox that you can fix by adding an extra element where you apply the writing-mode

td {
  text-align: center;
}

td span {
  writing-mode: vertical-rl;
  vertical-align:top; /* remove bottom whitespace */
}

td,
th {
  border: 1px solid black;
}
<table>
  <tbody>
    <tr>
      <th>This is my heading</th>
    </tr>
    <td>
      <span>しょうがない</span>
    </td>
  </tbody>
</table>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • I'm accepting this solution of adding an additional knowing that it's kind of a hacky workaround. It also works around a different but related Chrome bug, so bonus points for that too. – erjiang Sep 13 '21 at 00:57
0

This should work.

td {
  display: flex;
  box-sizing: border-box;
  writing-mode: vertical-rl;
  width: 200px;
  border: 1px solid black;
  line-height: normal;
  align-items: center;
  cursor: text;
}
<table border="1">
        <tbody>
          <tr>
            <th>This is my heading</th>
          </tr>
          <td>
            しょうがない
          </td>
        </tbody>
        </table>
Kelpi
  • 11
  • 1