Have you ever wondered how to create an intuitive foundational code base that effortlessly supports the addition of vertical or right-to-left language with just a few keystrokes? Understanding CSS Writing Modes, direction properties, and their impacted attributes is key to achieving this. By mastering these tools, you can develop user interfaces that are not only culturally inclusive but also deeply appreciated by users from diverse linguistic backgrounds.
CSS Writing Mode is a CSS property that defines the direction of text flow within a block container. It is used to specify whether lines of text are laid out horizontally or vertically, and the direction in which blocks progress.
CSS writing Mode has a simple set of three possible values: horizontal-tb
, vertical-rl
, and vertical-lr
. The default value is horizontal-tb
, which means that text is laid out horizontally from left to right, and top to bottom.
These different values then impact the direction of the following CSS properties:
padding-block-start
and padding-block-end
padding-inline-start
and padding-inline-end
margin-block-start
and margin-block-end
margin-inline-start
and margin-inline-end
border-block-start
and border-block-end
border-inline-start
and border-inline-end
inset-block-start
and inset-block-end
inset-inline-start
and inset-inline-end
text-align
vertical-align
overflow-block
and overflow-inline
scroll-snap-align
block
refers to the top-to-bottom direction, and inline
refers to the left-to-right direction in htb
languages.
Here is a table of the vertical languages and their writing modes:
Language | Writing Mode in CSS | Description |
---|---|---|
Japanese | vertical-rl | Traditionally used for literature, manga, and artistic texts. Lines progress from right to left. |
Chinese | vertical-rl | Used in traditional texts and calligraphy. Lines progress from right to left. |
Mongolian | vertical-lr | Traditional script is vertical, with lines progressing from left to right. |
Korean | vertical-rl | Historically used in classical literature, though less common now. Lines progress from right to left. |
All other languages | horizontal-tb | Depending on whether the language is right-to-left or left-to-right, the css direction property will impact the layout the most. |
This interactive demo showcases the use of CSS Writing Modes to build a multilingual UI that adeptly adjusts its direction to accommodate different languages.
Try changing the language, to see how the layout changes depending on the properties of the language.
If you want to see the UI translated, un-check the "Show in English" checkbox.
The demo's logic relies on a JavaScript object, which includes language properties such as direction, translation, and flex-box content placement.
The justify-content
property's value is set from a css variable. This allows for some additional layout changes that aren't covered by CSS Writing Modes.
CSS Scroll Snap is a modern CSS technique that enhances the user's scrolling experience by "snapping" the viewport to certain elements as they scroll. This is particularly useful in creating carousels, image galleries, or any content that you want to encourage users to pause and view.
When dealing with multilingual layouts, especially those with vertical or RTL (Right-to-Left) text, Scroll Snap can significantly improve the user experience. It ensures that the essential content is properly and clearly presented, regardless of the text orientation.
scroll-snap-type
: This property applies to the container and dictates the strictness of the snapping. For example, scroll-snap-type: x mandatory;
ensures that the scroll will always snap to a child element along the x-axis.
scroll-snap-align
: Applied to child elements, this property determines the element's snap point. For instance, scroll-snap-align: start;
will align the start of the element with the start of the scroll container.
Below, you'll find an interactive demo that showcases the versatility of CSS variables in controlling scroll direction and scroll snap behavior. As you explore, observe how the layout and scroll behavior dynamically adapt when you switch between languages. This functionality is powered by a JavaScript object containing scroll properties specific to each language. Upon changing the language, the CSS variables are updated, allowing you to see the corresponding changes in the user interface.
To wrap up, our deep dive into CSS Writing Modes and Scroll Snap underscores their pivotal role in crafting web interfaces that are not only functionally robust but also culturally sensitive. By effectively utilizing these CSS properties, developers can seamlessly accommodate diverse text orientations, a crucial aspect in today's increasingly globalized digital landscape.
This exploration is really just the beginning of my journey to understand how to build multilingual layouts. While there may be unique cases that require specialized solutions, understanding the basics of CSS Writing Modes and Scroll Snap lays a solid foundation for developing a multilingual UI that makes your audience feel included.
At the time of this writing, I was intrigued to note the scarcity of vertical websites, even in languages like Japanese or Chinese where such layouts would be highly applicable. By far, most of the ones I found were horizontal. While it seems like they haven't gained widespread popularity yet, it's plausible to anticipate a surge in their use as more developers adopt these techniques. This potential shift could significantly influence future trends in web design, making our journey into these realms even more relevant.
Happy coding!
入乡随俗
"When entering a village, follow its customs." - Chinese Proverb