11

Can anybody tell me the html/css to have Japanese text print from top to bottom, right to left (like in books) without changing the actual ilgnment of the characters? I am using UTF-16, If it helps.

Andrea
  • 19,134
  • 4
  • 43
  • 65
William Edwards
  • 115
  • 1
  • 6

1 Answers1

13

As @Michael_B pointed out in their comment above, you can use the writing-mode CSS property with the vertical-rl mode for this. For example:

.vertical {
  -webkit-writing-mode: vertical-rl;
  -moz-writing-mode: vertical-rl;
  -ms-writing-mode: vertical-rl;
  writing-mode: vertical-rl;
}
<p class=vertical lang=ja>これはテストテキスト。日本語は楽しいです。</p>

You might want to apply this to your whole page by targeting html rather than just individual elements, if you want things aligned to the right of the page:

html {
  -webkit-writing-mode: vertical-rl;
  -moz-writing-mode: vertical-rl;
  -ms-writing-mode: vertical-rl;
  writing-mode: vertical-rl;
}
<p lang=ja>これはテストテキスト。日本語は楽しいです。</p>
Andrea
  • 19,134
  • 4
  • 43
  • 65
  • 1
    Thank you! Worked great! – William Edwards Mar 23 '16 at 14:57
  • 1
    By the way (to any future readers), vertical text support in browsers, while widespread now, is not very good and you may need to use hacks to get it to work correctly in some situations (e.g. furigana). – Andrea Aug 07 '17 at 18:31
  • With a long text, this simply runs off the left side of the page and you have to scroll to the left to read it. How do you get it to stop at the edge of the page and then continue in a new row below? This needs to be responsive. That is, if the page is in a computer it will be wide and automatically "wrap" down to the next row, if it is in a smartphone it will be narrow and automatically "wrap" down to the next row. – Bathrobe Aug 04 '18 at 19:50
  • 1
    @Bathrobe You mean you want it to be right-to-left yet have top-to-bottom pagination? That would be strange, I don't know if there's a simple way to do that in CSS. – Andrea Aug 05 '18 at 21:34
  • I want Japanese text in an English-language page, which no one ever thinks of. So "XXXX" in English, then Japanese "YYYY", then English "XXXX". If the Japanese is short, no problem. But if it is long it will run off the page to the left. Of course you can cut the Japanese up, so: YYYY (new section) YYYY (new section) YYYY, never running off the page. But that would not be responsive. Text would have to be cut into short sections to match the width of smart phones, which would look silly on a computer. Perhaps flexbox would work (each box a short section, wrapping when it meets the edge)? – Bathrobe Aug 06 '18 at 22:41
  • For an example (but using only with very short sections of Japanese), see http://www.cjvlang.com/parsesnips/ishigamiletterseng.html I would like to embed longer texts in Japanese and have them wrap automatically to the row below when they reach the edge of the browser window. Nobody has ever asked this question because no one appears to have ever tried to do this in a live web page! Any solutions? – Bathrobe Aug 06 '18 at 22:50
  • I've done this with Mongolian traditional text, but it's not such a problem because Mongolian runs off the RIGHT edge of the browser window. Scrolling to the right to read the whole Mongolian text seems reasonably natural. But with text that runs off the LEFT edge of the browser window, it feels very awkward scrolling to the left. But even with Mongolian, Google search results note that the page isn't optimised for mobile devices! Google have never considered the consequences of mixing different text orientations on the same page. (Sorry for long comments) – Bathrobe Aug 06 '18 at 23:04
  • Mm. I am wondering if CSS columns can work here, but I don't know if they can be used vertically. – Andrea Aug 07 '18 at 10:51
  • I've found an alternative treatment. Rather than let the text go off the edge of the window, the text can be enclosed in some kind of box (fixed-width div) and read by scrolling (to the right or left, depending on the script). One problem with this is that the reader may be unaware that only part of the text is visible, particularly on Mac OSX, which likes to hide the scroll bar in the interest of 'looking nice'. Another problem might be printing the page out. My understanding is that overflow text will be ignored when printing (unfortunately I don't have a functioning printer). – Bathrobe Aug 18 '18 at 00:12
  • 1
    @Bathrobe you can use `@media print {}` to change the overflow behaviour for print and test with Print Preview – Andrea Aug 18 '18 at 21:46