├── .github └── FUNDING.yml ├── .gitignore ├── README.md ├── dist └── .gitkeep ├── index.html ├── package-lock.json ├── package.json ├── src └── index.js └── test └── index.html /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: joppuyo 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /dist 2 | !/dist/.gitkeep 3 | /node_modules 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Large, small, and dynamic viewport units polyfill 2 | 3 | [](https://www.npmjs.com/package/large-small-dynamic-viewport-units-polyfill) 4 | 5 | This is a "polyfill" which adds support for svh, dvh and lvh viewport units. It's mainly useful when dealing with iOS and Android mobile devices. 6 | 7 | ⚠️ **This library is pre-release quality. It should work but it requires more comprehensive testing on different devices. Please test it out and report any bugs you might encounter.** 8 | 9 | ## How it works 10 | 11 | It adds CSS custom properties (variables) `--1svh`, `--1dvh` and `--1lvh` using JavaScript. These correspond to 1 svh, 1 dvh and 1 lvh unit respectively. These custom properties are automatically updated when the viewport size changes. You can use CSS `calc()` function to multiply them to your desired size. 12 | 13 | ## How to use 14 | 15 | ### 1. Import script 16 | 17 | Include script tag or import the script using a bundler. 18 | 19 | #### 1.A. Script tag 20 | 21 | ```html 22 | 23 | ``` 24 | 25 | #### 1.B. Bundler 26 | 27 | Install the package. 28 | 29 | ```shell 30 | npm install large-small-dynamic-viewport-units-polyfill 31 | ``` 32 | 33 | Import the package. 34 | 35 | ```js 36 | import 'large-small-dynamic-viewport-units-polyfill'; 37 | ``` 38 | 39 | ### 2. Write CSS 40 | 41 | Add CSS like this in your project: 42 | 43 | ```css 44 | .my-small-hero-element { 45 | width: 100%; 46 | height: 100vh; /* For browsers that don't support CSS variables */ 47 | height: calc(var(--1svh, 1vh) * 100); /* This is the "polyfill" */ 48 | height: 100svh; /* This is for future browsers that support svh, dvh and lvh viewport units */ 49 | } 50 | 51 | .my-dynamic-hero-element { 52 | width: 100%; 53 | height: 100vh; /* For browsers that don't support CSS variables */ 54 | height: calc(var(--1dvh, 1vh) * 100); /* This is the "polyfill" */ 55 | height: 100dvh; /* This is for future browsers that support svh, dvh and lvh viewport units */ 56 | } 57 | 58 | .my-large-hero-element { 59 | width: 100%; 60 | height: 100vh; /* For browsers that don't support CSS variables */ 61 | height: calc(var(--1lvh, 1vh) * 100); /* This is the "polyfill" */ 62 | height: 100lvh; /* This is for future browsers that support svh, dvh and lvh viewport units */ 63 | } 64 | ``` 65 | 66 | ⚠️ **You should include all three lines so your styles will work properly. Because browsers will simply ignore CSS properties with values they do not understand, new browsers that implement the units will use the last line.** 67 | 68 | You may omit the first line if you don't need to support very old browsers like IE 11 (As of 2023, CSS custom properties are supported by 97% of browsers). 69 | 70 | ℹ️ **lvh unit is only included for the sake of completeness. It currently behaves exactly like vh unit. This may break in a future version of iOS or Android.** 71 | 72 | ## Compatibility 73 | 74 | I've tested this library with the following operating systems and devices: 75 | 76 | * Mobile Safari, iOS 15, iPhone 12 Pro 77 | * Mobile Safari, iOS 15, iPad Pro 11" 2018 78 | * Mobile Safari, iOS 14, iPhone SE (1st generation) 79 | * Mobile Safari, iOS 15.2, iPhone 13 Pro (Simulator) 80 | * Mobile Safari, iOS 15.2, iPad Pro (11-inch, 3rd Generation, Simulator) 81 | * Mobile Safari, iOS 16.1, iPhone 14 Pro (Simulator) 82 | * Mobile Safari, iOS 16.1, iPad Pro (11-inch, 4th Generation, Simulator) 83 | * Google Chrome 97, iOS 15, iPhone 12 Pro 84 | * Google Chrome 97, iPadOS 15, iPad Pro 11" 2018 85 | * Google Chrome 97, Android 8.1, Nexus 5X 86 | * Google Chrome 108, Android Emulator 87 | 88 | ## Todo 89 | 90 | * Add some automated tests 91 | * Figure out how to run automated tests on mobile devices (preferably on the cheap) 92 | 93 | ## Further reading 94 | 95 | * [The Large, Small, and Dynamic Viewports – Bram.us](https://www.bram.us/2021/07/08/the-large-small-and-dynamic-viewports/) 96 | * [The trick to viewport units on mobile – CSS-Tricks](https://css-tricks.com/the-trick-to-viewport-units-on-mobile/) 97 | * [New Viewport Units Introduced to Values and Units L4 – CSS WG Blog](https://www.w3.org/blog/CSS/2021/07/15/css-values-4-viewport-units/) 98 | -------------------------------------------------------------------------------- /dist/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joppuyo/large-small-dynamic-viewport-units-polyfill/408e2deb3c0e7805d152aed0c463e4e3a3d9bab1/dist/.gitkeep -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 72 | 73 | 142 | 143 | 144 | 145 | 146 | 147 | 148 |