Giter Club home page Giter Club logo

postcss-responsive-type's Introduction

PostCSS Responsive Type

NPM version NPM downloads Build Status

Generate automagical fluid typography, with new responsive properties for font-size, line-height, and letter-spacing. Built on PostCSS.

Responsive Type Demo

Inspired by this post.

Part of Rucksack - CSS Superpowers

Contents

Usage

Quick start

html {
  font-size: responsive;
}

Pro tip: set a reaponsive font-size on html and use rem units throughout your project to make your whole UI fluid

Specify parameters

Units can be in px, rem, or em. When using em units, be sure that the font-range is specified in em as well.

html {
  font-size: responsive 12px 21px; /* min-size, max-size */
  font-range: 420px 1280px; /* viewport widths between which font-size is fluid */
}

Expanded syntax

html {
  font-size: responsive;
  min-font-size: 12px;
  max-font-size: 21px;
  lower-font-range: 420px;
  upper-font-range: 1280px;
}

Responsive line-height and letter-spacing

PostCSS Responsive Type also allows you to set fluid sizes for the line-height and letter-spacing properties. They have the same syntax and work the same way as responsive font sizes.

html {
  line-height: responsive 1.2em 1.8em;
  line-height-range: 420px 1280px;

  /* or extended syntax: */
  line-height: responsive;
  min-line-height: 1.2em;
  max-line-height: 1.8em;
  lower-line-height-range: 420px;
  upper-line-height-range: 1280px;
}
html {
  letter-spacing: responsive 0px 4px;
  letter-spacing-range: 420px 1280px;

  /* or extended syntax: */
  letter-spacing: responsive;
  min-letter-spacing: 0px;
  max-letter-spacing: 4px;
  lower-letter-spacing-range: 420px;
  upper-letter-spacing-range: 1280px;
}

Note: Unitless line heights are not supported.

Defaults

To get started you only need to specify the responsive property, all other values have sane defaults.

font-size
  • min-font-size: 14px

  • max-font-size: 21px

  • lower-font-range: 420px

  • upper-font-range: 1280px

line-height
  • min-line-height: 1.2em

  • max-line-height: 1.8em

  • lower-line-height-range: 420px

  • upper-line-height-range: 1280px

letter-spacing
  • min-letter-spacing: 0px

  • max-letter-spacing: 4px

  • lower-letter-spacing-range: 420px

  • upper-letter-spacing-range: 1280px

Browser Support

postcss-responsive-type just uses calc, vw units, and media queries behind the scenes, so it works on all modern browsers (IE9+). Although Opera Mini is not supported.

Legacy browsers will ignore the output responsive font-size. You can easily provide a simple static fallback:

.foo {
  font-size: 16px;
  font-size: responsive;
}

MIT © Sean King

postcss-responsive-type's People

Contributors

adam-beck avatar aeyoll avatar artisin avatar azat-io avatar lmoval avatar madeleineostoja avatar perrin4869 avatar quintenvk avatar toreholmberg 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

postcss-responsive-type's Issues

pixrem / cssnext incompatibility ?

Tested with :

postcss([
  require("postcss-import")(),
  require("postcss-responsive-type")(),
  require("postcss-cssnext")(),
  require("postcss-extend")(),
  require("cssnano")()
])

And CSS :

html {
  font-size: 16px;
  font-size: responsive;
}

→ Error: pixrem: Root font-size is invalid

Calc sizing not working in Chrome

Hey!

Running Chrome Version 47.0.2526.73 (64-bit)

I seem to get an "invalid property" for my calc function.

the declaration is as follows:

$h1: responsive 4.8rem 5.2rem;
$h2: responsive 4.4rem 4.8rem;
$h3: responsive 3rem 3.4rem;
$h4: responsive 2rem 2.3rem;
$h5: responsive 1.6rem 2rem;
$h6: responsive 1rem 1.4rem;

Which results in this in Chrome

http://i.imgur.com/KjRunZE.png
screen shot 2015-12-04 at 10 16 56 am

I can confirm that on the latest Firefox and Safari the behaviour is as expected, however on Chrome it does not work.

Safari 9+ buggy with px/em/rem units

Not sure if newer version of Safari is supported but when I try the following it doesn't work as intended:

h1 {
 font-size: calc(28px + 20 * ((100vw - 320px) / 880));
}

http://codepen.io/umbriel/pen/WwLBxQ

Works fine in Firefox, Chrome and probably other modern browsers. But safari Version 9.0.3 fails entirely as evidenced in the Codepen I linked.

Does anyone have any ideas why this is?

edit: I may have found the culprit, it seems vw in conjunction with calc is the issue from what I tried. Is there a way of making it work?

Doesn't work with css variables

As i have updated my app to the latest Postcss version, the responsive type didn't work with standard CSS variables any more.

The font-range produces undefined and NaN, which leads to that the stylesheet can't be composed and font-size also doesn't take the given variable, but doesn't produce an error.

problem when prefix `::placeholder`

related issue https://github.com/postcss/autoprefixer/issues/1263
it happens when autoprefix input::placeholder

Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
ModuleBuildError: Module build failed (from ./node_modules/postcss-loader/src/index.js):
TypeError: Cannot read property 'indexOf' of undefined
    at /Users/t826148/dev/lightbox/repo/desktop/src/app/layouts/shared-navigation/search-nav-button.css:56:3
    at /Users/t826148/dev/lightbox/repo/desktop/node_modules/postcss-responsive-type/index.js:219:25
    at /Users/t826148/dev/lightbox/repo/desktop/node_modules/postcss/lib/container.js:239:18
    at /Users/t826148/dev/lightbox/repo/desktop/node_modules/postcss/lib/container.js:135:18
    at Root.each (/Users/t826148/dev/lightbox/repo/desktop/node_modules/postcss/lib/container.js:101:16)
    at Root.walk (/Users/t826148/dev/lightbox/repo/desktop/node_modules/postcss/lib/container.js:131:17)
    at Root.walkRules (/Users/t826148/dev/lightbox/repo/desktop/node_modules/postcss/lib/container.js:237:19)
    at /Users/t826148/dev/lightbox/repo/desktop/node_modules/postcss-responsive-type/index.js:213:9
    at LazyResult.run (/Users/t826148/dev/lightbox/repo/desktop/node_modules/postcss/lib/lazy-result.js:295:14)
    at LazyResult.asyncTick (/Users/t826148/dev/lightbox/repo/desktop/node_modules/postcss/lib/lazy-result.js:208:26)
    at LazyResult.asyncTick (/Users/t826148/dev/lightbox/repo/desktop/node_modules/postcss/lib/lazy-result.js:221:14)
    at LazyResult.asyncTick (/Users/t826148/dev/lightbox/repo/desktop/node_modules/postcss/lib/lazy-result.js:221:14)
    at /Users/t826148/dev/lightbox/repo/desktop/node_modules/postcss/lib/lazy-result.js:213:17
    at runLoaders (/Users/t826148/dev/lightbox/repo/desktop/node_modules/webpack/lib/NormalModule.js:316:20)
    at /Users/t826148/dev/lightbox/repo/desktop/node_modules/loader-runner/lib/LoaderRunner.js:367:11
    at /Users/t826148/dev/lightbox/repo/desktop/node_modules/loader-runner/lib/LoaderRunner.js:233:18
    at context.callback (/Users/t826148/dev/lightbox/repo/desktop/node_modules/loader-runner/lib/LoaderRunner.js:111:13)
    at Promise.resolve.then.then.catch (/Users/t826148/dev/lightbox/repo/desktop/node_modules/postcss-loader/src/index.js:208:9)

More flexibility?

Thanks for the plugin! I took your calculations and made a SASS mixin out of it.

I was running in to trouble because my use case didn't line up 100% with your intent-- I was using it as a way to make the font size of a module larger on a large handheld viewport, like from 400px to 600px. The issue was that I often wanted to reset to a smaller font-size, rather than transitioning to the "max-font-size" value.

With my mixin, you can use the fluid font size for more than one range, and the media query is scoped (using a Zurb Foundation function) to the supplied breakpoints.

Not sure if this plugin needs that, but just wanted to offer my experience in return for the knowledge I got from your plugin. That calculation is intense!

Regression in IE with new sizing logic

New logic in 0.2.0 (e3a9242) has introduced a regression in all versions of IE - where the font size shrinks past min-font-size and snaps back when it reaches the lower bound. Works in all other browsers - no idea what IE does differently with calc() and why this wouldn't work there.

If anyone needs an immediate fix, roll back to 0.1.* and just use px units throughout.

Originally flagged at madeleineostoja/rucksack#15

Responsive line-height with default parameters does not work

Hi. I'm using [email protected].
I just wanted to use responsive line-height.

html {
  line-height: responsive;
}

Unfortunately it generates an error:

postcss-responsive-type: $FILE_PATH/default.css:5:1: this combination of units is not supported

and invalid css:

html {
  line-height: calc(1.2em + 0.6000000000000001 * ((100vw - undefined) / NaN));
}

@media screen and (min-width: 1280px) {
  html {
    line-height: 1.8em;
  }
}

@media screen and (max-width: 420px) {
  html {
    line-height: 1.2em;
  }
}

Percentage root font sizes don't work

I had the min and max font size set with rem units and been using the 62.5% hack to reset font-size to 10px. What I noticed is that using this setup made the plugin wrongly calculated the responsive resizing of the fonts.
After searching in the source code I noticed the rootSize variable that was set to 16px and all calculations for the resizing of the font were made with that value and the fonts kept on growing/shrinking beyond the max/min size set for the font.
Further down the code I found where an override for the rootSize is set. Basically it searches for the html tag and then checks if the font-size is explicitly set with px units.
So in order everything to work with the 62.5% hack first set the font-size with px and then use the hack or just leave it with 10px:

html { font-size: 10px; font-size: 62.5%; }

Would be also a good thing to add this to the readme file for future reference.

BTW, great plugin 👍

Support rem units

Will have to check what font-size declared on html (if any, default to 16px), then calculate all rem values before mixing them into responsive declaration.

Mixing up px and rem units will be a pain (eg: rem font sizes and px media queries).

Default media queries being added when using rems to set font sizes

When using rems to set font sizes, (and ems to set breakpoints), two media queries are being generated for each selector. One using the defaults, and a second using the declared breakpoints. If I switch to using ems, the media queries only get declared once, but I want to size font in rems.

Use 'em' instead of 'px' for 'font-range'

A lot of modern CSS frameworks define breakpoints in 'em's and usually defined as variable somewhere in '_variables.scss'.
It will be great if plugin can calculate 'em' as good as it does for 'px'.

Huge Thank you!

Compatibility with PostCSS 8

Thanks for your PostCSS plugin!

As you probably know, PostCSS 8 was released a few days ago. It would be great if you could make this plugin compatible with the latest version. The creators of the project have even released this migration guide.

based on height?

Is there any way to configure this so the font sizing is based on the height and not the width?

Responsive 'px' restriction is not working

Hi, when using font-size: responsive; it works fine

however adding font-size: responsive 12px 16px; generates the following error in my terminal

events.js:141
      throw er; // Unhandled 'error' event
      ^
TypeError: Cannot read property 'toString' of null
    at getUnit (/d/stupid-starter/node_modules/postcss-responsive-type/index.js:77:36)
    at buildRules (/d/stupid-starter/node_modules/postcss-responsive-type/index.js:95:20)
    at /d/stupid-starter/node_modules/postcss-responsive-type/index.js:185:20
    at /d/stupid-starter/node_modules/gulp-postcss/node_modules/postcss/lib/container.js:102:34
    at /d/stupid-starter/node_modules/gulp-postcss/node_modules/postcss/lib/container.js:73:26
    at Rule.each (/d/stupid-starter/node_modules/gulp-postcss/node_modules/postcss/lib/container.js:60:22)
    at Rule.walk (/d/stupid-starter/node_modules/gulp-postcss/node_modules/postcss/lib/container.js:72:21)
    at Rule.walkDecls (/d/stupid-starter/node_modules/gulp-postcss/node_modules/postcss/lib/container.js:100:25)
    at /d/stupid-starter/node_modules/postcss-responsive-type/index.js:174:12
    at /d/stupid-starter/node_modules/gulp-postcss/node_modules/postcss/lib/container.js:115:34
╭─sebastian@SebastiansMBP2 /d/stupid-starter  ‹master*› 
╰─➤    

Anyideas why it might be happening?

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.