All Projects → Faisal-Manzer → Postcss Viewport Height Correction

Faisal-Manzer / Postcss Viewport Height Correction

Licence: mit
PostCSS plugin to solve the popular problem when 100vh doesn’t fit the mobile browser screen.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Postcss Viewport Height Correction

Rtlcss
Framework for transforming Cascading Style Sheets (CSS) from Left-To-Right (LTR) to Right-To-Left (RTL)
Stars: ✭ 1,363 (+894.89%)
Mutual labels:  css3, postcss, postcss-plugin
Postcss Register Custom Props
PostCSS plugin that transforms custom property registration in CSS to JS
Stars: ✭ 20 (-85.4%)
Mutual labels:  postcss, postcss-plugin
Postcss Banner
PostCSS plugin to add text banner and footer to resulting file
Stars: ✭ 13 (-90.51%)
Mutual labels:  postcss, postcss-plugin
Postcss Import
PostCSS plugin to inline @import rules content
Stars: ✭ 1,048 (+664.96%)
Mutual labels:  postcss, postcss-plugin
Purgecss
Remove unused CSS
Stars: ✭ 6,566 (+4692.7%)
Mutual labels:  postcss-plugin, postcss
Postcss Position
PostCSS plugin that adds shorthand declarations for position attributes
Stars: ✭ 26 (-81.02%)
Mutual labels:  postcss, postcss-plugin
Postcss Alias
PostCSS plugin that allows you to create aliases for CSS properties
Stars: ✭ 47 (-65.69%)
Mutual labels:  postcss, postcss-plugin
Postcss Sprites
Generate sprites from stylesheets.
Stars: ✭ 402 (+193.43%)
Mutual labels:  postcss, postcss-plugin
Postcss Nested Props
PostCSS plugin to unwrap nested properties.
Stars: ✭ 58 (-57.66%)
Mutual labels:  postcss, postcss-plugin
Postcss Prefix Selector
Prefix all CSS rules with a selector
Stars: ✭ 75 (-45.26%)
Mutual labels:  postcss, postcss-plugin
Postcss Less
PostCSS Syntax for parsing LESS
Stars: ✭ 93 (-32.12%)
Mutual labels:  postcss, postcss-plugin
Container Query
A PostCSS plugin and Javascript runtime combination, which allows you to write container queries in your CSS the same way you would write media queries.
Stars: ✭ 119 (-13.14%)
Mutual labels:  postcss, postcss-plugin
Postcss Easing Gradients
PostCSS plugin to create smooth linear-gradients that approximate easing functions.
Stars: ✭ 689 (+402.92%)
Mutual labels:  postcss, postcss-plugin
Postcss Interpolate
PostCSS plugin for values interpolation between breakpoints.
Stars: ✭ 9 (-93.43%)
Mutual labels:  postcss, postcss-plugin
Html5
HTML5学习、总结、实践
Stars: ✭ 493 (+259.85%)
Mutual labels:  css3, postcss
Postcss Icon
PostCSS plugin that adds `css icons` from icon sets
Stars: ✭ 20 (-85.4%)
Mutual labels:  postcss, postcss-plugin
postcss-momentum-scrolling
PostCSS plugin add 'momentum' style scrolling behavior (-webkit-overflow-scrolling: touch) for elements with overflow (scroll, auto) on iOS
Stars: ✭ 69 (-49.64%)
Mutual labels:  postcss, postcss-plugin
Postcss Responsive Type
Automagical responsive typography, built on PostCSS
Stars: ✭ 363 (+164.96%)
Mutual labels:  postcss, postcss-plugin
Postcss Triangle
PostCSS plugin to create a triangle.
Stars: ✭ 57 (-58.39%)
Mutual labels:  postcss, postcss-plugin
Postcss Plugins
The "officially unofficial" consolidated list of PostCSS plugins in a ready-to-use package
Stars: ✭ 107 (-21.9%)
Mutual labels:  postcss, postcss-plugin

PostCSS Viewport Height Correction

PostCSS plugin to solve the popular problem when 100vh doesn’t fit the mobile browser screen.

Installation

yarn add postcss-viewport-height-correction
# --- OR ----
npm install --save postcss-viewport-height-correction

And then add this javascript to public/index.html (for React), or add to template.html (for Preact).

var customViewportCorrectionVariable = 'vh';


function setViewportProperty(doc) {
  var prevClientHeight;
  var customVar = '--' + ( customViewportCorrectionVariable || 'vh' );
  function handleResize() {
    var clientHeight = doc.clientHeight;
    if (clientHeight === prevClientHeight) return;
    requestAnimationFrame(function updateViewportHeight(){
      doc.style.setProperty(customVar, (clientHeight * 0.01) + 'px');
      prevClientHeight = clientHeight;
    });
  }
  handleResize();
  return handleResize;
}
window.addEventListener('resize', setViewportProperty(document.documentElement));

Check you project for existed PostCSS config: postcss.config.js in the project root, "postcss" section in package.json or postcss in bundle config.

If you already use PostCSS, add the plugin to plugins list:

module.exports = {
  plugins: [
+   require('postcss-viewport-height-correction'),
    require('autoprefixer')
  ]
}

If you do not use PostCSS, add it according to official docs and set this plugin in settings.

Configuration

You really will rarely need this. Use this when you have some conflicting css variable. We use --vh as variable to fix the viewport height. You can use --pvh or any other variable of your choice.

Configure postcss to use your variable.

module.exports = {
  plugins: [
+   require('postcss-viewport-height-correction')({ variable: 'pvh' }),
    require('autoprefixer')
  ]
}

Also change variable name in javascript you added. Change customViewportCorrectionVariable value to your variable.

+ var customViewportCorrectionVariable = 'pvh'
- var customViewportCorrectionVariable = 'vh'

NOTE: Only use plain alphabetical characters as custom variable name. We are using regex to patch viewport value, any variable with special characters can lead to unknown issues.

Inspiration

The viewport height which we use as "vh" unit in css does not give the actual viewport height but gives the height of the browser window. This plugin is an implememtation of CSS Tricks article on this issue.

Example Output

.foo {
    /* Input example */
    height: 100vh;
}

.bar {
    min-height: 50vh;
    max-height: 75vh;
    margin: -1vh;
}
.foo {
  /* Output example */
  height: 100vh;
  height: calc(var(--vh, 1vh) * 100); /* corrected viewport height using css custom variables */
}

.bar {
      min-height: calc(var(--vh, 1vh) * 50);
      max-height: calc(var(--vh, 1vh) * 75);
      margin: calc(var(--vh, 1vh) * -1);
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].