A workaround for the '100vh' issue in mobile browsers
npm install react-div-100vhDiv100vh React component and use100vh React hook
This is a workaround for iOS Safari and other mobile browsers.
In mobile browsers, the _real_ height of the viewport is dynamic, as browser
"chrome" (panels) slide away on scrolling. The browser developers faced two
choices: either to reflow the page as the pixel value of a vh changes, or
ignore the fact that the browser panel covers part of the screen.
The browser panels are supposed to slide away smoothly, and because the layout
reflow during scrolling will not look smooth, the browser developers went for
the second option.
It may work for the most of use cases, but if you're looking for an app-like
full-screen experience, or want to make sure that the call to action button at
the bottom of your splash screen isn't covered, you may need to know the fair
value of a vh.
| More on this issue `` const MyFullHeightComponent = () => ( For more advanced use cases (for instance, if you need 50% of the real height), ` const MyHalfHeightExampleComponent = ({ children }) => { Under the hood This component is tested with | |
| ------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| !Page cropped by bottom Safari chrome | !Page cropped by bottom Safari chrome |
here.The solution
Div100vh React component is the default export:jsx
import Div100vh from 'react-div-100vh'
)
`
there is a named export use100vh. This React hook provides an accuratenumber
vertical height in pixels. The return type is a in a browser and nullnull
in Node environment. You may need to check if it's not if you're doingpx
SSR, otherwise, manipulate the value as you wish and concatenate the result with:jsx
import { use100vh } from 'react-div-100vh'
const height = use100vh()
const halfHeight = height ? height / 2 : '50vh'
return {children}
}
`use100vh uses measureHeight function which is exported asdocument.documentElement?.clientHeight || window.innerHeight
well, so feel free to use it, even without React. Currently it returns if executed in anull` if on a server.
browser or Testing
title="BrowserStack" alt="BrowserStack Logo" height="40"
src="https://raw.githubusercontent.com/mvasin/react-div-100vh/master/images/browser-stack.svg">.