Mastering CSS `Writing-Mode`: A Comprehensive Guide for Developers

Written by

in

In the world of web development, we often think of content flowing from left to right, top to bottom. But what if you need to create a website that caters to languages like Japanese or Chinese, where text can be written vertically? Or perhaps you’re designing a creative layout that breaks the mold? This is where CSS `writing-mode` comes into play, offering a powerful tool to control the direction in which your text and layout elements are displayed.

Why `writing-mode` Matters

The `writing-mode` property allows you to define how text is laid out horizontally or vertically. It’s crucial for:

  • Internationalization (i18n): Supporting languages with different writing systems.
  • Creative Layouts: Designing unique and visually appealing interfaces.
  • Accessibility: Ensuring content is readable and understandable for all users.

Without understanding `writing-mode`, you might struggle to create websites that correctly display text in languages like Japanese, Chinese, and Korean, which often use vertical writing. Furthermore, you might find it difficult to achieve certain design aesthetics that require text to be oriented in non-traditional ways.

Understanding the Basics

The `writing-mode` property accepts several values, but we’ll focus on the most common and important ones:

  • horizontal-tb: (default) Text flows horizontally, top to bottom.
  • vertical-rl: Text flows vertically, right to left.
  • vertical-lr: Text flows vertically, left to right.

Let’s dive into each of these with examples and explanations.

horizontal-tb

This is the default value. It’s what you’re most familiar with. Text flows from left to right, and blocks stack from top to bottom. Think of it as the standard English writing style.

.element {
  writing-mode: horizontal-tb;
}

In this example, the element will display text horizontally, just like a standard paragraph.

vertical-rl

This value is used for vertical writing, where text flows from top to bottom, and lines stack from right to left. This is common in languages like Japanese and Chinese.

.element {
  writing-mode: vertical-rl;
}

With `vertical-rl`, the text within the element will be oriented vertically. The first character appears at the top right, and the subsequent characters stack downwards. If you have multiple lines, they’ll stack from right to left.

vertical-lr

Similar to `vertical-rl`, this also renders text vertically, but the lines stack from left to right. This is less common but still useful.

.element {
  writing-mode: vertical-lr;
}

In this case, the first character will be at the top left, with subsequent characters stacking downwards, and lines stacking to the right.

Practical Examples

Let’s look at some practical examples to see how `writing-mode` can be used.

Example 1: Vertical Navigation Menu

Imagine you want to create a vertical navigation menu. You can use `writing-mode` to achieve this easily.

HTML:

<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Services</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>

CSS:

nav {
  width: 100px; /* Adjust as needed */
  height: 300px; /* Adjust as needed */
  border: 1px solid #ccc;
  writing-mode: vertical-rl;
  text-orientation: upright; /* Important for vertical text */
}

nav ul {
  list-style: none;
  padding: 0;
  margin: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}

nav a {
  display: block;
  padding: 10px;
  text-decoration: none;
  color: #333;
  text-align: center;
}

In this example, we set the `writing-mode` to `vertical-rl` for the navigation element. The `text-orientation: upright;` property ensures that the text within the links is readable when written vertically. We also use `flexbox` to arrange the links vertically within the navigation container.

Example 2: Vertical Text in a Specific Element

You can apply `writing-mode` to a specific element within your page to create a unique visual effect.

HTML:

<div class="vertical-text">
  This is vertical text.
</div>

CSS:

.vertical-text {
  width: 100px;
  height: 200px;
  border: 1px solid #ccc;
  writing-mode: vertical-rl;
  text-orientation: upright;
  padding: 10px;
}

Here, the `div` with the class `vertical-text` will display its content vertically. The `text-orientation: upright;` ensures the text is readable.

Advanced Techniques and Considerations

text-orientation

The `text-orientation` property is often used in conjunction with `writing-mode`. It controls the orientation of the text within a vertical layout. The most common value is `upright`, which ensures that the text remains readable, even when written vertically.

.element {
  writing-mode: vertical-rl;
  text-orientation: upright;
}

direction

The `direction` property is used to set the text direction. It’s particularly relevant when dealing with bidirectional text (e.g., Arabic or Hebrew). Values include `ltr` (left-to-right) and `rtl` (right-to-left).

.element {
  direction: rtl; /* For right-to-left languages */
}

While `writing-mode` controls the general layout direction, `direction` specifies the text direction within that layout.

Browser Compatibility

`writing-mode` has good browser support, but it’s always a good idea to test your designs across different browsers and devices. Older versions of Internet Explorer (IE) might have limited support, so consider providing fallbacks if you need to support those browsers.

Responsive Design

When using `writing-mode`, remember to consider responsive design. Your vertical layouts might need adjustments on smaller screens. Use media queries to adapt your styles based on screen size.

@media (max-width: 768px) {
  .vertical-text {
    writing-mode: horizontal-tb;
    text-orientation: initial; /* Reset to default */
  }
}

This example shows how to revert the `writing-mode` to horizontal on smaller screens.

Common Mistakes and How to Fix Them

Mistake: Forgetting text-orientation

One of the most common mistakes is forgetting to set `text-orientation: upright;` when using `writing-mode: vertical-rl` or `writing-mode: vertical-lr`. This can result in text that’s difficult to read.

Fix: Always include `text-orientation: upright;` when using vertical `writing-mode` to ensure text readability.

Mistake: Not Considering Layout Changes

Changing the `writing-mode` can significantly impact your layout. Elements might not behave as expected. You might need to adjust widths, heights, and other properties.

Fix: Thoroughly test your layout after changing the `writing-mode`. Use the browser’s developer tools to inspect elements and identify any adjustments needed.

Mistake: Ignoring Browser Compatibility

While `writing-mode` has good support, older browsers might have issues. Failing to test across different browsers can lead to display inconsistencies.

Fix: Test your designs in various browsers and devices. Consider providing fallbacks for older browsers if necessary, using conditional comments or feature detection.

Key Takeaways

  • `writing-mode` is essential for internationalization and creative layouts.
  • Understand the core values: `horizontal-tb`, `vertical-rl`, and `vertical-lr`.
  • Use `text-orientation: upright;` for readable vertical text.
  • Test your designs thoroughly and consider responsive design.

FAQ

1. What is the difference between `writing-mode` and `text-orientation`?

writing-mode defines the overall direction of the text and layout (horizontal or vertical). text-orientation specifies the orientation of the text within a vertical layout (e.g., upright). They often work together.

2. Does `writing-mode` affect all elements on a page?

No, `writing-mode` applies to the specific element it’s applied to and its descendants. It doesn’t affect the entire page unless applied to the `html` or `body` element.

3. How do I make sure my vertical text is readable?

Use `text-orientation: upright;` in conjunction with `writing-mode: vertical-rl` or `writing-mode: vertical-lr`. This ensures that the text characters are oriented correctly.

4. What are some common use cases for `writing-mode`?

Common use cases include creating vertical navigation menus, displaying text in languages that use vertical writing (Japanese, Chinese, Korean), and designing creative layouts where text is oriented in non-traditional ways.

5. How can I handle `writing-mode` in a responsive design?

Use media queries to adjust the `writing-mode` property based on screen size. You might switch back to `horizontal-tb` on smaller screens to optimize readability and layout.

Mastering `writing-mode` opens up a new dimension of possibilities in web design. By understanding its core principles and applying it thoughtfully, you can create more inclusive, visually engaging, and internationally-friendly websites. Experiment with different values, combine them with other CSS properties, and explore the creative potential that `writing-mode` unlocks. As you delve deeper, you’ll find that it’s not just about supporting different languages; it’s about expanding the boundaries of what’s possible on the web and crafting experiences that truly resonate with your audience. The ability to control text flow is a powerful tool, and with practice, you’ll be able to wield it with confidence, creating websites that are both functional and aesthetically compelling.