Utilities

Sizing

Create sizing utilities for width, height, min/max dimensions with full type safety.

Overview

Sizing utilities help you control element dimensions including width, height, and their minimum and maximum constraints.

Why Use Sizing Utilities?

Sizing utilities help you:

  • Control dimensions: Set precise widths and heights for elements
  • Create responsive layouts: Use percentage-based and viewport-based values
  • Integrate with design tokens: Reference spacing values for consistent sizing
  • Set constraints: Define minimum and maximum dimensions

useWidthUtility

The useWidthUtility() function creates utility classes for setting element width.

styleframe.config.ts
import { styleframe } from "styleframe";
import { useWidthUtility } from "@styleframe/theme";

const s = styleframe();

useWidthUtility(s, {
    '0': '0px',
    '1': '0.25rem',
    '2': '0.5rem',
    '4': '1rem',
    '8': '2rem',
    '16': '4rem',
    '32': '8rem',
    '64': '16rem',
    auto: 'auto',
    '1/2': '50%',
    '1/3': '33.333333%',
    '2/3': '66.666667%',
    '1/4': '25%',
    '3/4': '75%',
    full: '100%',
    screen: '100vw',
    min: 'min-content',
    max: 'max-content',
    fit: 'fit-content',
});

export default s;

useMinWidthUtility & useMaxWidthUtility

Control minimum and maximum width constraints.

styleframe.config.ts
import { styleframe } from "styleframe";
import { useMinWidthUtility, useMaxWidthUtility } from "@styleframe/theme";

const s = styleframe();

useMinWidthUtility(s, {
    '0': '0px',
    full: '100%',
    min: 'min-content',
    max: 'max-content',
    fit: 'fit-content',
});

useMaxWidthUtility(s, {
    '0': '0px',
    none: 'none',
    xs: '20rem',
    sm: '24rem',
    md: '28rem',
    lg: '32rem',
    xl: '36rem',
    '2xl': '42rem',
    '3xl': '48rem',
    '4xl': '56rem',
    '5xl': '64rem',
    '6xl': '72rem',
    '7xl': '80rem',
    full: '100%',
    prose: '65ch',
    'screen-sm': '640px',
    'screen-md': '768px',
    'screen-lg': '1024px',
    'screen-xl': '1280px',
});

export default s;
Pro tip: Use max-width: prose (65ch) for optimal reading line length in text-heavy content.

useHeightUtility

The useHeightUtility() function creates utility classes for setting element height.

styleframe.config.ts
import { styleframe } from "styleframe";
import { useHeightUtility } from "@styleframe/theme";

const s = styleframe();

useHeightUtility(s, {
    '0': '0px',
    '1': '0.25rem',
    '2': '0.5rem',
    '4': '1rem',
    '8': '2rem',
    '16': '4rem',
    '32': '8rem',
    '64': '16rem',
    auto: 'auto',
    '1/2': '50%',
    '1/3': '33.333333%',
    '2/3': '66.666667%',
    full: '100%',
    screen: '100vh',
    svh: '100svh',
    lvh: '100lvh',
    dvh: '100dvh',
    min: 'min-content',
    max: 'max-content',
    fit: 'fit-content',
});

export default s;

useMinHeightUtility & useMaxHeightUtility

Control minimum and maximum height constraints.

import { useMinHeightUtility, useMaxHeightUtility } from "@styleframe/theme";

useMinHeightUtility(s, {
    '0': '0px',
    full: '100%',
    screen: '100vh',
    svh: '100svh',
    lvh: '100lvh',
    dvh: '100dvh',
    min: 'min-content',
    max: 'max-content',
    fit: 'fit-content',
});

useMaxHeightUtility(s, {
    '0': '0px',
    none: 'none',
    full: '100%',
    screen: '100vh',
    svh: '100svh',
    lvh: '100lvh',
    dvh: '100dvh',
    min: 'min-content',
    max: 'max-content',
    fit: 'fit-content',
});

useSizeUtility

Set both width and height simultaneously.

styleframe.config.ts
import { styleframe } from "styleframe";
import { useSizeUtility } from "@styleframe/theme";

const s = styleframe();

useSizeUtility(s, {
    '4': '1rem',
    '6': '1.5rem',
    '8': '2rem',
    '10': '2.5rem',
    '12': '3rem',
    '16': '4rem',
    full: '100%',
});

export default s;

Examples

Responsive Container

styleframe.config.ts
import { styleframe } from "styleframe";
import { useWidthUtility, useMaxWidthUtility } from "@styleframe/theme";

const s = styleframe();

useWidthUtility(s, { full: '100%' });
useMaxWidthUtility(s, {
    'screen-sm': '640px',
    'screen-md': '768px',
    'screen-lg': '1024px',
    'screen-xl': '1280px',
});

export default s;

Usage in HTML:

<div class="_width:full _max-width:screen-lg" style="margin: 0 auto;">
    Container content
</div>

Icon Sizes

styleframe.config.ts
import { styleframe } from "styleframe";
import { useSizeUtility } from "@styleframe/theme";

const s = styleframe();

useSizeUtility(s, {
    '4': '1rem',      // 16px - small icon
    '5': '1.25rem',   // 20px - default icon
    '6': '1.5rem',    // 24px - medium icon
    '8': '2rem',      // 32px - large icon
    '10': '2.5rem',   // 40px - extra large
});

export default s;

Usage in HTML:

<svg class="_size:6"><!-- icon --></svg>
<svg class="_size:8"><!-- larger icon --></svg>

Best Practices

  • Use percentage widths for responsive layouts: 1/2, 1/3, 2/3 scale with parent
  • Use max-width for containers: Prevents content from becoming too wide on large screens
  • Use dvh for mobile: 100dvh accounts for mobile browser chrome better than 100vh
  • Integrate with design tokens: Reference your spacing scale for consistent sizing
  • Use fit-content sparingly: It can cause unexpected layouts; test thoroughly

FAQ