Spectre.css
  • Introduction
  • ❤ Benefits
  • General concepts
  • Getting started
    • Skeleton
    • Installation
      • npm
    • Usage
  • Variables
    • Color scheme
    • CSS variable name
    • Using CSS variables
    • Colors
      • Settable SCSS
      • Based on settable SCSS
      • CSS variable color
      • Using CSS variable color
      • Color code
    • Other
  • CSS Color Classes
    • Background colors
    • Label colors
    • ⚠ Text colors
    • ⚠ Toast colors
  • Function
    • Color
      • alpha-var()
      • color()
      • color-name()
      • hsla-color()
      • hue-var()
      • lightness-level-var()
      • lightness-var()
      • saturation-var()
    • Var
      • css-var()
      • get-var()
      • size-var()
      • unit-var()
      • var-adjust()
      • var-name()
      • var-negative()
      • var-unit()
    • get-border()
    • get-from-list()
    • important()
    • map-get-default()
    • strip-unit()
    • typeof()
  • Mixin
    • Border
      • border()
      • border-hover()
      • border-variant()
    • Color
      • background()
      • background-color()
      • bg-color-variant()
      • color-active()
      • color-focus()
      • color-hover()
      • color-picker()
      • color-visited()
      • set-color()
      • set-var-alpha()
      • text-color-variant()
    • Label
      • label-base()
      • label-variant()
      • label-class-variant()
    • Margin
      • margin-size-variants()
      • margin-variant()
    • Padding
      • padding-size-variants()
      • padding-variant()
    • Text
      • text-ellipsis()
    • Toast
      • toast-variant()
      • ⚠ toast-class-variant()
    • Var
      • set-var()
      • set-vars()
      • ⚠ define-color()
      • ⚠ define-color-based-on()
    • avatar-base()
    • box-shadow-side()
    • button-variant()
    • clearfix()
    • control-shadow()
    • disabled()
    • hide-scrollbar()
    • property()
    • shadow-variant()
    • transition()
    • z-index()
  • Change log
    • Keep a changelog
    • CHANGELOG.md
  • GIT
    • Commit
    • Semantic Versioning
  • License
    • MIT
  • Social
    • Gettr
    • Twitter
    • YouTube
  • Contact
    • ⋯ Chat
    • @ Email
    • ✆ Phone
  • Donate
    • ฿ Cryptocurrency
    • $ Fiat
Powered by GitBook
On this page
  1. Variables

Using CSS variables

PreviousCSS variable nameNextColors

Last updated 2 years ago

To inject prefixed CSS variables with the help comes function, which takes four parameters: $name $prefix $suffix and $unit.

There is only $name parameter required, and the $prefix default value is equal to , $suffix default value is an empty string, and $unit default value is set to 0.

The function returns CSS function with the CSS variable of the $name parameter with defaults or returns CSS function that multiplies by one unit CSS variable value of the same name.

For example, there is SCSS variable $layout-spacing equal to 0.4rem with equivalent prefixed CSS variable --s-layout-spacing equal to 0.4 without rem unit to have the ability to multiply it by the px unit value and It's defined as follows.

// layout-spacing
--s-layout-spacing: 0.4;
--s-layout-spacing-lg: 0.8;
--s-layout-spacing-sm: 0.2;
--s-layout-spacing-unit: 1rem;

To inject into any style the layout-spacing CSS variable value there is a need to provide $unit parameter to 1.

// Get function.
@use 'node_modules/@angular-package/spectre.css/src/functions/get-var' as *;

// Inject `get-var()` simply by given `$name` parameter and `$unit`.
.layout-spacing {
    margin: get-var($name: 'layout-spacing', $unit: 1);
}

// Inject `get-var()` with the smaller spacing without `$name` parameter but
// with -sm added.
.layout-spacing-sm {
    margin: get-var('layout-spacing-sm', $unit: 1);
}

// Inject `get-var()` with the larger spacing by using `$suffix` parameter equal
// to `lg`.
.layout-spacing-lg {
    margin: get-var('layout-spacing', $unit: 1, $suffix: `lg`);
}
get-var()
var()
calc()
$var-prefix