npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lehoczky/postcss-fluid

v1.0.3

Published

PostCSS plugin that makes it easier to work with fluid css values.

Downloads

2,664

Readme

postcss-fluid

NPM version test status formatted with prettier

PostCSS plugin that makes it easier to work with fluid, responsive values.

Want to make your font-size 1rem on screens smaller than 40rem and scale it up to 1.25rem on desktops (120rem)?
It's trivial with this plugin:

/* Input */
.fluid-typography {
  font-size: fluid(1rem, 1.25rem, 40rem, 120rem);
}
/* Output */
.fluid-typography {
  font-size: clamp(1rem, 0.875rem + 0.3125vw, 1.25rem);
}

This is called fluid typography, you can read more about the topic in this excellent blog post: smashingmagazine.com/2016/05/fluid-typography.

Coming up with the right clamp value is hard, that's why we have sites like responsive font calculator, fluid typography calculator or frontend tools that take your min and max value, min and max viewport size and output the right expression. You just have to copy it into your css.

The purpose of this plugin is to bring this functionality into your stylesheets without the need of any external website.

Demo

Demo is available at https://lehoczky.github.io/postcss-fluid/

Setup

Step 1: Install plugin:

npm install --save-dev postcss @lehoczky/postcss-fluid

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

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

Step 3: Add the plugin to plugins list:

module.exports = {
  plugins: [
+   require('@lehoczky/postcss-fluid'),
    require('autoprefixer')
  ]
}

Usage

The function takes 4 parameters: min value, max value, min viewport width, max viewport width.

.foo {
  margin: fluid(1rem, 2rem, 40rem, 120rem);
}
.foo {
  margin: clamp(1rem, 0.5rem + 1.25vw, 2rem);
}

The values can be pixels too, in this case the output will use pixels as well:

.foo {
  margin: fluid(16px, 32px, 640px, 1920px);
}
.foo {
  margin: clamp(16px, 8px + 1.25vw, 32px);
}

you can even mix the two examples above and use pixels for the values and rem for the viewport width or vice versa:

.foo {
  margin: fluid(1rem, 2rem, 640px, 1920px);
  letter-spacing: fluid(1px, 2px, 40rem, 120rem);
}
.foo {
  /* Notice that the output uses the same unit that's given to the value parameters */
  margin: clamp(1rem, 0.5rem + 1.25vw, 2rem);
  letter-spacing: clamp(1px, 0.5px + 0.0781vw, 2px);
}

however don't try to mix units for the values or the viewports. The following won't work:

.foo {
  margin: fluid(1rem, 10px, 640px, 1920px);
}

The function can only be called with px and rem units. %, vh, dpi, etc. are not supported.

Options

It's possible to set a default min and max viewport width in the plugin options.

plugins: [
  require("@lehoczky/postcss-fluid")({ min: "40rem", max: "120rem" }),
  ...
],

Now you can call the fluid function with only 2 parameters:

.foo {
  /* same as fluid(1rem, 2rem, 40rem, 120rem) */
  margin: fluid(1rem, 2rem);

  /* Calling with all 4 parameters overrides the globally set options */
  padding: fluid(1rem, 2rem, 60rem, 80rem);
}

The values can be given as numbers (number of pixels), or as strings with either rem or px units. The following options are all valid:

require("@lehoczky/postcss-fluid")({
  min: "640px",
  max: "1920px",
})

require("@lehoczky/postcss-fluid")({
  min: "40rem",
  max: "120rem",
})

require("@lehoczky/postcss-fluid")({
  // same as "640px" and "1920px"
  min: 640,
  max: 1920,
})

Stylelint

If you use stylelint in your project and it warns about the function, add this to your .stylelintrc:

{
  "rules": {
    "function-no-unknown": [
      true,
      {
        "ignoreFunctions": ["fluid"]
      }
    ]
  }
}