Quick HTML/CSS hack for prettier Japanese titles
Tags: html, css, typography, japanese
Published: 2021-11-23 18:43:37

As you read this article in English, look at the end of each line of text. You can thank the `<space>` character for cleanly separating words into unbreakable chunks that aren't interrupted by newlines. **English: words trump newlines**.

Japanese does not use spaces, in general. Humans that read Japanese just have a 'spidey sense' of where words start and stop based on grammar and context.

For large blocks of text like the body of articles, it is common for multi-character "words" to be split at newlines. This is OK:  characters are mono-spaced and paragraphs keep an aesthetically pleasing blockiness. **Japanese: newlines trump words**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y196s8ad8mhvbej0vjva.png)
*From Asahi Shimbun (text in bottom section is read top-to-bottom, right-to-left).*

Note above how each newline breaks in the same spot, regardless of position in the word. No hyphens necessary in Japanese! 

While “newlines trump words” looks nice in large blocks of text, it looks awkward on lonely one-line text like titles.

For example, this text "Learn body parts":

```html
<h2>体の部分を学ぶ</h2>
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zfnqz2ebmi113lk1xha9.gif)
*Default "PEZ dispenser" behavior*

**Without a space character or hyphen telling the browser where to delimit words, the default behavior is to just pop characters off one-by-one like PEZ candies in a PEZ dispenser.**

This is the English equivalent of what's happening on resize:

```
Learn body part
s
Learn body par
ts
Learn body pa
rts
...
```

Can we agree this is ugly and unreadable?

To prevent PEZ dispensing and force compound characters to stick together in this Japanese title, you'll want to wrap the desired chunks in display: `inline-block` style.

**HTML**
```html
<h2>
  <span class="w">体の</span>
  <span class="w">部分を</span>
  <span class="w">学ぶ</span>
</h2>

<!-- 

CSS style:

.w {
  display: inline-block;
}

-->
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8f4vdtcc6o4zynj69bhf.gif)
*Not PEZ dispensing*

Much better!

## Automate it!
This solution is fine quick fix for the occasional eye-catching title, but requires knowledge of Japanese grammar to know where to put the `<span>` tags.

Fortunately there is a cool tool called [Budou](https://medium.com/r/?url=https%3A%2F%2Fgithub.com%2Fgoogle%2Fbudou) that automates this formatting. You don't even have to know Japanese - it uses AI to semantically parse text into words and insert the spans for you (it works for Chinese and Korean too!).

This has been the first of what will probably be many articles on "things I overlooked when translating my website into Japanese".

-----
See the pretty Japanese line-wrapping in action on my children's book website: [The Power of Vegetables](https://www.power-of-vegetables.com/ja)

*Deer illustrations in GIFs by [Varvara Fomina](https://www.instagram.com/Fbarbara.fomina)*

*This article was originally posted on [Medium](https://medium.com/@clairefroelich/quick-html-css-hack-for-prettier-japanese-titles-fb4ce3d41309)*