GithubHelp home page GithubHelp logo

joppuyo / large-small-dynamic-viewport-units-polyfill Goto Github PK

View Code? Open in Web Editor NEW
40.0 3.0 1.0 72 KB

Polyfill for svh, dvh and lvh CSS viewport units

Home Page: https://www.npmjs.com/package/large-small-dynamic-viewport-units-polyfill

HTML 76.86% JavaScript 23.14%
android css mobile safari units vh viewport 100vh

large-small-dynamic-viewport-units-polyfill's Introduction

Large, small, and dynamic viewport units polyfill

npm

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.

⚠️ 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.

How it works

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.

How to use

1. Import script

Include script tag or import the script using a bundler.

1.A. Script tag

<script src="https://unpkg.com/[email protected]/dist/large-small-dynamic-viewport-units-polyfill.min.js"></script>

1.B. Bundler

Install the package.

npm install large-small-dynamic-viewport-units-polyfill

Import the package.

import 'large-small-dynamic-viewport-units-polyfill';

2. Write CSS

Add CSS like this in your project:

.my-small-hero-element {
    width: 100%;
    height: 100vh; /* For browsers that don't support CSS variables */
    height: calc(var(--1svh, 1vh) * 100); /* This is the "polyfill" */
    height: 100svh; /* This is for future browsers that support svh, dvh and lvh viewport units */
}

.my-dynamic-hero-element {
    width: 100%;
    height: 100vh; /* For browsers that don't support CSS variables */
    height: calc(var(--1dvh, 1vh) * 100); /* This is the "polyfill" */
    height: 100dvh; /* This is for future browsers that support svh, dvh and lvh viewport units */
}

.my-large-hero-element {
    width: 100%;
    height: 100vh; /* For browsers that don't support CSS variables */
    height: calc(var(--1lvh, 1vh) * 100); /* This is the "polyfill" */
    height: 100lvh; /* This is for future browsers that support svh, dvh and lvh viewport units */
}

⚠️ 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.

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).

ℹ️ 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.

Compatibility

I've tested this library with the following operating systems and devices:

  • Mobile Safari, iOS 15, iPhone 12 Pro
  • Mobile Safari, iOS 15, iPad Pro 11" 2018
  • Mobile Safari, iOS 14, iPhone SE (1st generation)
  • Mobile Safari, iOS 15.2, iPhone 13 Pro (Simulator)
  • Mobile Safari, iOS 15.2, iPad Pro (11-inch, 3rd Generation, Simulator)
  • Mobile Safari, iOS 16.1, iPhone 14 Pro (Simulator)
  • Mobile Safari, iOS 16.1, iPad Pro (11-inch, 4th Generation, Simulator)
  • Google Chrome 97, iOS 15, iPhone 12 Pro
  • Google Chrome 97, iPadOS 15, iPad Pro 11" 2018
  • Google Chrome 97, Android 8.1, Nexus 5X
  • Google Chrome 108, Android Emulator

Todo

  • Add some automated tests
  • Figure out how to run automated tests on mobile devices (preferably on the cheap)

Further reading

large-small-dynamic-viewport-units-polyfill's People

Contributors

joppuyo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jesse-deboer

large-small-dynamic-viewport-units-polyfill's Issues

GitHub actions

User GitHub action to build the library and release it to NPM

Finalize API

  • Decide if we want automatic, self-instantiating API or an explicit function that needs to be run by the user (check how some other popular polyfills work)
  • Provide an API so the user can refresh the css properties on demand
  • Figure out CommonJS, AMD, RequireJS, ES6 Modules and UMD stuff
  • Provide a way to use the library with script tag (attach a global to the window object)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.