Day 38: vh, svh, lvh, and dvh

Using the viewport unit vh in desktop browsers is usually straight-forward, 100vh matches the height of the viewport. On mobile that’s different because the viewport height changes depending on whether or not certain user interface elements ar…


This content originally appeared on Manuel Matuzović - Web development blog and was authored by Manuel Matuzović

Using the viewport unit vh in desktop browsers is usually straight-forward, 100vh matches the height of the viewport. On mobile that's different because the viewport height changes depending on whether or not certain user interface elements are visible, 100vh doesn't always match the height of the viewport.

On mobile we have the small viewport and the large viewport. According to the spec, the small viewport is the viewport sized assuming any UA [User Agent/Browser] interfaces [for example the address bar] that are dynamically expanded and retracted to be expanded.

a red border marking the visible area of the viewport. Below address bar and other controls.

The large viewport is the viewport sized assuming any UA interfaces that are dynamically expanded and retracted to be retracted.

a red border marking the visible area of the viewport, which fills almost the whole screen, no UA interface elements visible.

The problem with 100vh on mobile is that it doesn’t respect whether user interface elements are expanded or not. It usually always matches the large viewport. CSS introduces new viewport units to address that issue.

You can use svh for the small viewport and lvh for the large viewport.

div {
height: 100svh;
/* See result in the first screenshot */
}
div {
height: 100lvh;
/* See result in the second screenshot */
}

That's great, but depending on if and how the user interacts with the page, they might sometimes see the large viewport and sometimes the small viewport. Setting the height to either unit probably isn't what you want because the height changes dynamically. Instead, you want to use the third new unit dvh, which dynamically either matches svh or lvh.

div {
height: 100dvh;
}
comparisson of vh, lvh, svh, and dvh on a small and large viewport


This content originally appeared on Manuel Matuzović - Web development blog and was authored by Manuel Matuzović


Print Share Comment Cite Upload Translate Updates
APA

Manuel Matuzović | Sciencx (2022-11-16T09:38:54+00:00) Day 38: vh, svh, lvh, and dvh. Retrieved from https://www.scien.cx/2022/11/16/day-38-vh-svh-lvh-and-dvh/

MLA
" » Day 38: vh, svh, lvh, and dvh." Manuel Matuzović | Sciencx - Wednesday November 16, 2022, https://www.scien.cx/2022/11/16/day-38-vh-svh-lvh-and-dvh/
HARVARD
Manuel Matuzović | Sciencx Wednesday November 16, 2022 » Day 38: vh, svh, lvh, and dvh., viewed ,<https://www.scien.cx/2022/11/16/day-38-vh-svh-lvh-and-dvh/>
VANCOUVER
Manuel Matuzović | Sciencx - » Day 38: vh, svh, lvh, and dvh. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/11/16/day-38-vh-svh-lvh-and-dvh/
CHICAGO
" » Day 38: vh, svh, lvh, and dvh." Manuel Matuzović | Sciencx - Accessed . https://www.scien.cx/2022/11/16/day-38-vh-svh-lvh-and-dvh/
IEEE
" » Day 38: vh, svh, lvh, and dvh." Manuel Matuzović | Sciencx [Online]. Available: https://www.scien.cx/2022/11/16/day-38-vh-svh-lvh-and-dvh/. [Accessed: ]
rf:citation
» Day 38: vh, svh, lvh, and dvh | Manuel Matuzović | Sciencx | https://www.scien.cx/2022/11/16/day-38-vh-svh-lvh-and-dvh/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.