Accoutrement 2.1.1

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
$sizes: ('my-size': 3em);
$size-var-prefix: 'prefix_';
:root { @include size--('my-size') }
css compiled
:root {
  --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
$sizes: (
  '_root': 16px,
  'rhythm': '#_root' ('times': 1.5),
  'line': '#rhythm',
);
:root {
  @include sizes--;
}
css compiled
:root {
  --size-rhythm: 24px;
  --size-line: var(--size-rhythm, 24px);
}

Requires

@mixin tokens--()

$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
$sizes: (
  '_root': 16px,
  'rhythm': '#_root' ('times': 1.5),
  'line': '#rhythm',
);
.example {
  @include size--('line');
  @include size--('padding', 'line', 1.5rem);
}
css compiled
.example {
  --size-line: var(--size-rhythm, 24px);
  --size-padding: var(--size-line, 1.5rem);
}

Requires

@mixin token--()

$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
$sizes: (
  '_root': 16px,
  'rhythm': '#_root' ('times': 1.5),
  'line': '#rhythm',
);
.example {
  line-height: var-size('line');
  padding: 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)