Skip to main content

A JS utility for generating fluid CSS length values.

Works with
It is unknown whether this package works with Bun
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
It is unknown whether this package works with Deno
This package works with Browsers
JSR Score
100%
Published
4 weeks ago (1.2.1)

css-fluid-length

A JS utility for generating fluid CSS length values.

👀 🔠 Want fluid font sizes in Tailwind? You could use css-fluid-length, but the tailwindcss-fluid-font-size plugin will give you a better dev experience.

Installation

Install the plugin from JSR. Use one of the below, depending on your runtime and package manager:

bunx jsr add @olets/css-fluid-length
# or
deno add @olets/css-fluid-length
# or
npx jsr add @olets/css-fluid-length
# or
pnpm dlx jsr add @olets/css-fluid-length
# or
yarn dlx jsr add @olets/css-fluid-length

Setup

Import the fluid length function:

// tailwind.config.js
const { fluidLength } = require("@olets/css-fluid-length");
// or
import { fluidLength } from "@olets/css-fluid-length";

/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    // ...
    extend: {
      spacing: {
        fluid: fluidLength(/* … */),
      }
    }
  },
}

Usage

Use fluidLength anywhere you would use a non-fluid length.

Props

Prop Type Notes
fromLength string The length when options.relativeDimension is greater than or equal to fromBound. px or rem (e.g. '47px'). Required
toLength string The length when options.relativeDimension is less than or equal to toBound. px or rem (e.g. '74px'). Required
options object See Options

Options

Option Type Default Notes
fromBound string Tailwind's default theme's screens.sm ^1 px or rem (e.g. '400px')
toBound string Tailwind's default theme's screens['2xl'] ^1 px or rem (e.g. '700px')
relativeDimension string 'vw' The dimension the length is relative to. cqb, cqh, cqi, cqmax, cqmin, cqw, dvh, dvw, vb, vh, vi, vmax, vmin, or vw

Example

css-fluid-length fits in any CSS-in-JS setup. Here's a Tailwind v3 example.

// tailwind.config.js
// …
  theme: {
    extend: {
      spacing: {
        'from-16px-to-32px': fluidLength('16px', '32px'),
      },
      width: ({ theme }) => ({
        'from-4@md-to-20': fluidLength(
          theme('spacing.4'),
          theme('spacing.20'),
          {
            fromBound: theme('screens.md'),
          }
        ),
      }),
    },
  },
// …

Changelog

See the CHANGELOG file.

Contributing

Thanks for your interest. Contributions are welcome!

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Check the Issues to see if your topic has been discussed before or if it is being worked on.

Please read CONTRIBUTING.md before opening a pull request.

License

css-fluid-length by Henry Bley-Vroman is licensed under a license which is the unmodified text of CC BY-NC-SA 4.0 and the unmodified text of a Hippocratic License 3. It is not affiliated with Creative Commons or the Organization for Ethical Source.

Human-readable summary of (and not a substitute for) the LICENSE file:

You are free to

  • Share — copy and redistribute the material in any medium or format
  • Adapt — remix, transform, and build upon the material

Under the following terms

  • Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • Non-commercial — You may not use the material for commercial purposes.
  • Ethics - You must abide by the ethical standards specified in the Hippocratic License 3 with Ecocide, Extractive Industries, US Tariff Act, Mass Surveillance, Military Activities, and Media modules.
  • Preserve terms — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
  • No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.

 

Footnotes

Add Package

deno add @olets/css-fluid-length

Import symbol

import * as mod from "@olets/css-fluid-length";

Add Package

npx jsr add @olets/css-fluid-length

Import symbol

import * as mod from "@olets/css-fluid-length";

Add Package

yarn dlx jsr add @olets/css-fluid-length

Import symbol

import * as mod from "@olets/css-fluid-length";

Add Package

pnpm dlx jsr add @olets/css-fluid-length

Import symbol

import * as mod from "@olets/css-fluid-length";

Add Package

bunx jsr add @olets/css-fluid-length

Import symbol

import * as mod from "@olets/css-fluid-length";