Saltar al contenido ForoHackers

100vh on mobile browsers is lying to you (y cómo arreglarlo)

AndroidCSSFrontendiOS
#1210 · spanglish_stacktrace · · Denunciar

Classic frontend pain. You build a full-screen hero with height: 100vh, it looks perfect on the desktop, and then on the phone the bottom of the page sits under the browser's address bar. I have seen this bug in almost every project I have worked on, y siempre me sorprende.

Por qué: on mobile browsers the address bar collapses when you scroll, so the visible area changes while you use the page. 100vh was defined as the largest possible viewport (bar hidden), so while the bar is showing, 100vh is taller than what you can see.

La solución moderna son las unidades nuevas de viewport:

.hero {
  min-height: 100vh;   /* fallback para navegadores viejos */
  min-height: 100svh;  /* small viewport: always fits, bar visible */
}
/* or 100dvh to follow the bar as it collapses */

Y para iPhones con notch o la barra de gestos:

.footer {
  padding-bottom: env(safe-area-inset-bottom);
}

Three things I now do: test on a real device, not only in devtools' responsive mode; avoid height-locking containers if the content can grow; and never trust the emulator's address bar, which behaves like a polite fiction.

#2144 · dual_boot_dani · · Denunciar

svh vs dvh was the thing I never got. So svh is "the layout that always fits even with the bar showing" and dvh is "the one that jumps around as the bar moves"? Muy bien explicado.

#2145 · spanglish_stacktrace · · Denunciar

Exacto, Dani. dvh resizes while scrolling, which can make things jump or re-layout, así que para un hero estático yo uso svh. Para algo que debe llenar siempre la pantalla visible, dvh.

#2146 · selenium_sleepwalker · · Denunciar

Test automation angle: headless desktop browsers resized to a phone width do not reproduce the collapsing bar, so your CI is green and your phone is broken. Run a handful of visual checks on real devices or on a device farm, and keep them few and stable. Flaky tests made of screenshots are my personal nightmare.

#2147 · tabs_over_spaces_42 · · Denunciar

Or build the page with a layout that does not depend on viewport height at all. I will leave that as an exercise for people who like to be happy. (Indented with tabs, naturally.)

#2148 · git_blame_myself · · Denunciar

Shipped a login screen once where the submit button was below the fold on every phone. Tested only in the desktop browser's responsive mode. The commit message said "mobile support". The commit message was a lie.

Inicia sesión o únete para responder.