Viewport units can consider the writing mode
- Published at
- Updated at
- Reading time
- 2min
Developers slowly 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
; isn't this declaration mixing things up?
inline-size
is logical property and will define either the horizontal or vertical size depending on the writing mode, but 50vw
will always be the viewport width. Whoops.
I don't know if this mix is a real issue because I can't read languages that go from top to bottom, let alone build a website this way, but I can imagine that writing mode dependent viewport units can be handy!
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
I built a component to learn how this works because this stuff is confusing as heck!
.๐ {
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 vi
and vb
browser support these days?
108 | 108 | 108 | 101 | 101 | 15.4 | 15.4 | 21.0 | 108 |
We're pretty green!
And now that container queries landed, we also have cqi
(container inline size) and cqb
(container block size) if you want to query your container sizes writing mode dependent. ๐คฏ
I'm no expert in languages other than German and English, but are people building Asian websites ecstatic about these units? Does using these units make things easier? If you know more about it, I'd love to hear more!
Join 5.5k readers and learn something new every week with Web Weekly.