Fluid sizes without hand-writing clamp() math.
A lightweight SCSS utility for generating responsive sizes using the clamp() function. This package includes helper functions for dynamic sizing based on breakpoints or custom values.
Resulted clamp() would have rem, not px units. Please use csize() function to use cqi units to work with container queries.
// Hand-written: you compute the slope/intersection yourself, and
// px→rem conversion, breakpoint lookup, and direction all live in your head.
font-size: clamp(1rem, 0.3076923077rem + 1.9230769231vw, 1.5rem);
// clamp-size: breakpoints referenced by key, px auto-converted to rem,
// and size(24px, 16px, ...) inverts correctly if min > max — no hand math.
font-size: clamp.size(16px, 24px, 'sm', 'lg');Install the package via npm:
npm install clamp-sizeImport the package into your project:
@use 'clamp-size' as clamp;Generates a responsive size using the clamp() function. This function works with both breakpoints (defined in the $breakpoints map) and custom numeric values.
-
Parameters:
$min-size: Size at$min-width(required). Unitless number orpx— anything else is a build error.$max-size: Size at$max-width(optional). Same unit rules as$min-size. If$max-sizeis greater than$min-sizethe value grows with the viewport; if smaller, it shrinks. May equal$min-sizefor a constant size.$min-width: Viewport width the size ramp starts at (optional). Either a key from$breakpoints(e.g.'sm') or a unitless/pxnumber. Defaults to the first entry in$breakpoints('xs'/ 320 by default) — not the numerically smallest one, so an unordered custom map changes this default.$max-width: Viewport width the size ramp ends at (optional). Same rules as$min-width, defaults to the last entry in$breakpoints('xl'/ 1200 by default). Must resolve to a value strictly greater than$min-width, or it's a build error.
$min-size/$max-sizeare output asrem(converted via$base-rem-size) regardless of whether you passedpxor a unitless number.$min-width/$max-widthare only used as unitless pixel counts for the slope math — they never appear asremin the output. -
Example:
Using breakpoints:
.my-class {
font-size: clamp.size(16px, 24px, 'sm', 'lg');
}Using numeric values:
.my-class {
font-size: clamp.size(10px, 20px, 360px, 1400px);
}Identical to size() — same parameter rules, same breakpoint-key-or-number widths — except the output uses container query units (cqi) instead of vw. Unlike size(), all four parameters are required (no defaults).
-
Parameters: see
size()above;$min-size,$max-size,$min-width,$max-widthall required here. -
Example:
.my-class {
font-size: clamp.csize(16px, 24px, 360px, 1400px);
}Converts a pixel value to rem based on the global base size. Accepts a unitless number or a px value; any other unit (e.g. rem, em, %) is a build error.
- Example:
.my-class {
margin: clamp.px-to-rem(16px);
}The utility uses a default $breakpoints map and $base-rem-size:
$breakpoints: (
'xs': 320,
'sm': 576,
'md': 768,
'lg': 992,
'xl': 1200
) !default;
$base-rem-size: 16 !default;Both are @use-configurable — override them with with (...) on the same
statement that imports the module (this must be the first @use of
clamp-size in your build):
@use 'clamp-size' as clamp with (
$breakpoints: (
'mobile': 360,
'tablet': 768,
'desktop': 1024
),
$base-rem-size: 16
);This project is licensed under the MIT License.