What unit or approach is best (or at least good!) practice for using measurements relative to screen size? I'm constraining this question to mobile devices, which I suspect may affect the answers. I'd thus wrap the styles inside @media (hover:none), (hover:on-demand) { }
.
1 Answer
[...] measurements relative to screen size
For elements at the top level within <body>
you can use %
.
But in many cases I'd be inclined to use viewport units:
vw
- 1% of the viewport widthvh
- 1% of the viewport heightvmax
- equal to 1% of whichever is larger, viewport width or viewport heightvmin
- equal to 1% of whichever is smaller, viewport width or viewport height
Further Reading:
-
1One thing that's slightly bad about
vw
is that if there's a scrollbar then the box will ignore it, leaving horizontal and vertical scrolling. Butvh
always works fine. Commented Aug 25, 2020 at 15:18 -
2Agreed, @Sven -
100vw
and100%
of<body>
give different results depending on what type of scrollbar you're dealing with - a touchscreen style scrollbar or a desktop window viewport style scrollbar. This is consistently a pain, but it won't be resolved until we have OS-agnostic scrollbars.– RouninCommented Aug 25, 2020 at 18:01