Accoutrement 4.0.4

Sizes » CSS Variables

$size-var-prefix (string)

scss
$size-var-prefix: 'size-' !default;

Since CSS variables are often defined in a global space, it can be useful to prefix different variable types. Accoutrement maps help solve that problem in Sass, so we wanted to carry that over into our CSS variable support. Set a prefix for all size tokens, and we’ll apply it when setting or calling CSS variables based on your size maps. Set to null or '' (empty string) for no prefix.

Since 2.0.0:

  • NEW: Initial release

Example

scss
tools.$sizes: ('my-size': 3em);
tools.$size-var-prefix: 'prefix_';
html { @include tools.size--('my-size') }
css compiled
html {
  --prefix_my-size: 3em;
}

Used By

@mixin sizes--()

@mixin size--()

@function var-size()

@mixin sizes--()

Convert any size-map into CSS variables, using the global $size-var-prefix.

  • Size names that start with _ or - are considered “private” and will not be output as variables
  • Sizes that contain a simple alias with no adjustments will be output with a var() value, keeping the alias relationship intact

Since 2.0.0:

  • NEW: Initial release

Parameters & Output

$source: $sizes (map)

Optionally use a custom map of sizes to set as CSS variables

{CSS output} (code block)

Custom properties for all public sizes in the map

Example

scss
tools.$sizes: (
  '_root': 16px,
  'rhythm': '#_root' ('times': 1.5),
  'line': '#rhythm',
);
html {
  @include tools.sizes--;
}
css compiled
html {
  --size-rhythm: 24px;
  --size-line: var(--size-rhythm, 24px);
}

Requires

$size-var-prefix (string)

@mixin size--()

Set a single custom property based on a map-size, with optional alias and fallback

Since 2.0.0:

  • NEW: Initial release

Parameters

$size: (*)

Size name available in the $source map, or alias to use in naming the CSS variable

$value: null (string | null)

Optional value for the variable, if different from the given $size

$fallback: true (*)

The optional fallback value for a var() function:

  • true will generate a fallback based on the token value
  • A token name will fallback to the value of that CSS variable and then to the token’s calculated value
  • Any other fallback will be passed through unchanged

$source: $sizes (map)

Optional map of sizes to use as a palette source

Example

scss
tools.$sizes: (
  '_root': 16px,
  'rhythm': '#_root' ('times': 1.5),
  'line': '#rhythm',
);
.example {
  @include tools.size--('line');
  @include tools.size--('padding', 'line', 1.5rem);
}
css compiled
.example {
  --size-line: var(--size-rhythm, 24px);
  --size-padding: var(--size-line, 1.5rem);
}

Requires

$size-var-prefix (string)

@function var-size()

Access the CSS variable associated with a given token, along with a fallback value based on the token itself

Since 2.0.0:

  • NEW: Initial release

Parameters & Return

$size: (*)

Size name available in the $source map

$fallback: true (*)

The optional fallback value for a var() function:

  • true will generate a fallback based on the token value
  • A token name will fallback to the value of that CSS variable and then to the token’s calculated value
  • Any other fallback will be passed through unchanged

$source: (map)

Optional Accoutrement map of sizes to use as source

@return (string)

CSS variable call, in the format: var(--<token>, <fallback>)

Example

scss
tools.$sizes: (
  '_root': 16px,
  'rhythm': '#_root' ('times': 1.5),
  'line': '#rhythm',
);
.example {
  line-height: tools.var-size('line');
  padding: tools.var-size('rhythm', 1.5em);
}
css compiled
.example {
  line-height: var(--size-line, 24px);
  padding: var(--size-rhythm, 1.5em);
}

Requires

@function var-token()

$size-var-prefix (string)