No daily nuggets remaining

You're on the free tier. Upgrade now to unlock unlimited nuggets and more.

You Shouldn’t Rely on CSS 100vh and Here’s Why! | Medium

The Nugget

  • On mobile devices, CSS 100vh may not actually be 100vh due to browser toolbar height not being taken into account. Avoid relying on 100vh and consider alternatives like 'fill-available' or JavaScript solutions when designing for mobile responsiveness.

Make it stick

  • 📏 CSS 100vh on mobile may lack browser toolbar height consideration.
  • 📱 Consider 'fill-available' or JavaScript to fix CSS 100vh issues on mobile.

Key insights

CSS 100vh Limitation on Mobile Devices

  • 100vh in CSS may not truly be 100% of the viewport height on mobile devices due to browser UI elements like toolbars.
  • Testing CSS 100vh on real devices is crucial to understand the actual rendering on different platforms.

Alternatives to 100vh in CSS

  • Consider using 'fill-available' property in CSS to address the 100vh issue on mobile devices more accurately.
  • Implementing JavaScript solutions, like setting element heights dynamically, can also provide reliable mobile responsiveness.

Challenges and Solutions

  • HTML doctype declarations can interfere with 'fill-available' in CSS, requiring adjustments like removing the declaration for Chrome compatibility.
  • Issues like vertical padding on Safari can be resolved by wrapping content in additional div elements.
  • Using 'calc()' with 'fill-available' is limited; JavaScript can be utilized for more complex height calculations.

Key quotes

  • "To fix 100vh Issue on Mobile Devices? My first advice to you is to use vh as little as possible."
  • "Fixing the 100vh issue with fill-available is indeed straightforward but there are things that I personally experienced while working on this solution."
  • "Some developers like to define a CSS variable based on the window inner height and use that variable to style their desired elements."
  • "I mean it’s a CSS-only solution, guys! Use JavaScript only if you have to!"
  • "Adding vertical padding to the element that has its min-height (or height) to fill-available causes a problem on the Safari browser and the height won’t be correct."
This summary contains AI-generated information and may have important inaccuracies or omissions.