Viewport units can consider the writing mode
- Published at
- Updated at
- Reading time
- 2min
Slowly but surely, developers adopt logical CSS properties to be good web citizens and write CSS that follows a defined writing mode, directionality, and text orientation.
width
becomes inline-size
, margin-inline
is a shorthand for margin-left
and margin-right
, you get the idea.
MDN lists all the logical CSS properties if you haven't heard about them yet.
That's all good and dandy, but there's at least one situation where logical CSS properties aren't enough. Suppose you want to style a container and make it 50% of the viewport on the horizontal axis.
You can define inline-size: 50vw
; but isn't that mixing things up? inline-size
will define either the horizontal or vertical size depending on the writing mode, but 50vw
will always be the viewport width.
I don't know if that's a real issue because I can't read languages that go from top to bottom, let alone build a website. But I can imagine that writing mode dependent viewport units can be handy!
And today I learned these viewport units exist! vi
and vb
consider writing direction!
vi
is the percentage of the initial containing blocks inline axis and vb
the percentage of the block axis. ๐คฏ
In left-to-right
vi -> vw
vb -> vh
---
In top-to-bottom
vi -> vh
vb -> vw
That's confusing stuff, so I build a component to learn how this works!
.๐ {
writing-mode: horizontal-tb;
width: 50vw;
height: 20vh;
}
Text is lined out horizontally and flows from top to bottom.
The container's horizontal size is 50% viewport width and its vertical size is 20% viewport height.
What's the browser support? Chrome started supporting these units not very long ago.
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
108 | 108 | 108 | 101 | 101 | 15.4 | 15.4 | 21.0 | 108 |
I'm no expert in languages other than German and English, but are people building Asian websites ecstatic about these units and that they're almost cross-browser supported at the time I'm publishing this article?
If you know more about it, I'd love to hear more!
Yes? Cool! You might want to check out Web Weekly for more quick learnings. The last edition went out 18 days ago.