Skip to main content Skip to docs navigation

Utilize our source Sass files to take advantage of variables, maps, mixins, and functions to help you build faster and customize your project.

Utilize our source Sass files to take advantage of variables, maps, mixins, and more.

Sass deprecation warnings are shown when compiling source Sass files with the latest versions of Dart Sass. This does not prevent compilation or usage of Chassis. We’re working on a long-term fix, but in the meantime these deprecation notices can be ignored.

File structure

Whenever possible, avoid modifying Chassis CSS’s core files. For Sass, that means creating your own stylesheet that imports Chassis CSS so you can modify and extend it. Assuming you’re using a package manager like npm, you’ll have a file structure that looks like this:

your-project/
├── scss/
│   └── custom.scss
└── node_modules/
│   └── chassis-css/
│       ├── js/
│       └── scss/
└── index.html

If you’ve downloaded our source files and aren’t using a package manager, you’ll want to manually create something similar to that structure, keeping Chassis CSS’s source files separate from your own.

your-project/
├── scss/
│   └── custom.scss
├── chassis-css/
│   ├── js/
│   └── scss/
└── index.html

Importing

In your custom.scss, you’ll import Chassis CSS’s source Sass files. You have two options: include all of Chassis, or pick the parts you need. We encourage the latter, though be aware there are some requirements and dependencies across our components. You also will need to include some JavaScript for our plugins.

// Custom.scss
// Option A: Include all of Chassis CSS

// Include any default variable overrides here (though functions won’t be available)

@import "../node_modules/chassis-css/scss/chassis";

// Then add additional custom code here
// Custom.scss
// Option B: Include parts of Chassis CSS

// 1. Include functions first (so you can manipulate colors, SVGs, calc, etc)
@import "../node_modules/chassis-css/scss/functions";

// 2. Include any default variable overrides here

// 3. Include remainder of required Chassis CSS stylesheets (including any separate color mode stylesheets)
@import "../node_modules/chassis-css/scss/variables";

// 4. Include any default map overrides here

// 5. Include remainder of required parts
@import "../node_modules/chassis-css/scss/maps";
@import "../node_modules/chassis-css/scss/mixins";
@import "../node_modules/chassis-css/scss/root";

// 6. Include any other optional stylesheet partials as desired; list below is not inclusive of all available stylesheets
@import "../node_modules/chassis-css/scss/utilities";
@import "../node_modules/chassis-css/scss/reboot";
@import "../node_modules/chassis-css/scss/type";
@import "../node_modules/chassis-css/scss/images";
@import "../node_modules/chassis-css/scss/containers";
@import "../node_modules/chassis-css/scss/grid";
@import "../node_modules/chassis-css/scss/helpers";
// ...

// 7. Optionally include utilities API last to generate classes based on the Sass map in `_utilities.scss`
@import "../node_modules/chassis-css/scss/utilities-api";

// 8. Add additional custom code here

With that setup in place, you can begin to modify any of the Sass variables and maps in your custom.scss. You can also start to add parts of Chassis CSS under the // Optional section as needed. We suggest using the full import stack from our chassis.scss file as your starting point.

Compiling

In order to use your custom Sass code as CSS in the browser, you need a Sass compiler. Sass ships as a CLI package, but you can also compile it with other build tools like Gulp or Webpack, or with GUI applications. Some IDEs also have Sass compilers built in or as downloadable extensions.

We like to use the CLI to compile our Sass, but you can use whichever method you prefer. From the command line, run the following:

# Install Sass globally
npm install -g sass

# Watch your custom Sass for changes and compile it to CSS
sass --watch ./scss/custom.scss ./css/custom.css

Learn more about your options at sass-lang.com/install and compiling with VS Code.

Using Chassis CSS with another build tool? Consider reading our guides for compiling with Webpack, Parcel, or Vite. We also have production-ready demos in our examples repository on GitHub.

Including

Once your CSS is compiled, you can include it in your HTML files. Inside your index.html you’ll want to include your compiled CSS file. Be sure to update the path to your compiled CSS file if you’ve changed it.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Custom Chassis CSS</title>
    <link href="/css/custom.css" rel="stylesheet">
  </head>
  <body>
    <h1>Hello, world!</h1>
  </body>
</html>

Variable defaults

Every Sass variable in Chassis CSS includes the !default flag allowing you to override the variable’s default value in your own Sass without modifying Chassis CSS’s source code. Copy and paste variables as needed, modify their values, and remove the !default flag. If a variable has already been assigned, then it won’t be re-assigned by the default values in Chassis.

You will find the complete list of Chassis CSS’s variables in scss/_variables.scss. Some variables are set to null, these variables don’t output the property unless they are overridden in your configuration.

Variable overrides must come after our functions are imported, but before the rest of the imports.

Here’s an example that changes the background-color and color for the <body> when importing and compiling Chassis CSS via npm:

// Required
@import "../node_modules/chassis-css/scss/functions";

// Default variable overrides
$bg-color: #000;
$fg-color: #111;

// Required
@import "../node_modules/chassis-css/scss/variables";
@import "../node_modules/chassis-css/scss/variables-dark";
@import "../node_modules/chassis-css/scss/maps";
@import "../node_modules/chassis-css/scss/mixins";
@import "../node_modules/chassis-css/scss/root";

// Optional Chassis CSS components here
@import "../node_modules/chassis-css/scss/reboot";
@import "../node_modules/chassis-css/scss/type";
// etc

Repeat as necessary for any variable in Chassis, including the global options below.

Get started with Chassis CSS via npm with our starter project! Head to the Sass & JS example template repository to see how to build and customize Chassis CSS in your own npm project. Includes Sass compiler, Autoprefixer, Stylelint, PurgeCSS, and Chassis CSS Icons.

Maps and loops

Chassis CSS includes a handful of Sass maps, key value pairs that make it easier to generate families of related CSS. We use Sass maps for our colors, grid breakpoints, and more. Just like Sass variables, all Sass maps include the !default flag and can be overridden and extended.

Some of our Sass maps are merged into empty ones by default. This is done to allow easy expansion of a given Sass map, but comes at the cost of making removing items from a map slightly more difficult.

Modify map

All variables in the $context-colors map are defined as standalone variables. To modify an existing color in our $context-colors map, add the following to your custom Sass file:

$primary: #0074d9;
$danger: #ff4136;

Later on, these variables are set in Chassis CSS’s $context-colors map:

$context-colors: (
  "primary": $primary,
  "danger": $danger
);

Add to map

Add new colors to $context-colors, or any other map, by creating a new Sass map with your custom values and merging it with the original map. In this case, we'll create a new $custom-colors map and merge it with $context-colors.

// Create your own map
$custom-colors: (
  "custom-color": #900
);

// Merge the maps
$context-colors: map-merge($context-colors, $custom-colors);

Remove from map

To remove colors from $context-colors, or any other map, use map-remove. Be aware you must insert $context-colors between our requirements just after its definition in variables and before its usage in maps:

// Required
@import "../node_modules/chassis-css/scss/functions";
@import "../node_modules/chassis-css/scss/variables";
@import "../node_modules/chassis-css/scss/variables-dark";

$context-colors: map-remove($context-colors, "info", "light", "dark");

@import "../node_modules/chassis-css/scss/maps";
@import "../node_modules/chassis-css/scss/mixins";
@import "../node_modules/chassis-css/scss/root";

// Optional
@import "../node_modules/chassis-css/scss/reboot";
@import "../node_modules/chassis-css/scss/type";
// etc

Required keys

Chassis CSS assumes the presence of some specific keys within Sass maps as we used and extend these ourselves. As you customize the included maps, you may encounter errors where a specific Sass map’s key is being used.

For example, we use the primary, success, and danger keys from $context-colors for links, buttons, and form states. Replacing the values of these keys should present no issues, but removing them may cause Sass compilation issues. In these instances, you’ll need to modify the Sass code that makes use of those values.

Functions

Colors

Chassis uses oklch as its default color space. For a full overview of how the color system works — including design tokens, CSS custom properties, and palette maps — see Color System and Design Tokens.

The following functions in scss/functions/_color.scss are the primary tools for working with colors in Sass.

to-color()

Converts any Sass-compatible color value to a rounded oklch() string. This is used internally to normalize colors into the oklch space before they are written to CSS custom properties.

/// Convert a color to an oklch() value with rounded channel values
/// Converts any Sass-supported color to the OKLCh color space, rounding lightness
/// to 2 decimal places, chroma to 3 decimal places, and hue to 2 decimal places.
/// Alpha is preserved when less than 1.
///
/// @param {Color} $color - Any Sass-compatible color value
/// @return {Color} - An oklch() color with rounded L, C, H channels and preserved alpha
/// @example
///   to-color(#ff0000) => oklch(0.63 0.258 29.23)
///   to-color(rgba(#ff0000, 0.5)) => oklch(0.63 0.258 29.23 / 0.5)
@function to-color($color) {
  $oklch: color.to-space($color, oklch);

  // Extract and round channels
  $l: math.div(math.round(color.channel($oklch, "lightness", oklch) * 100), 100);
  $c: math.div(math.round(color.channel($oklch, "chroma", oklch) * 1000), 1000);
  $a: color.alpha($color);

  // Achromatic colors (chroma rounds to 0) have no meaningful hue;
  // skip math on the 'none' keyword to avoid calc(NaN * 1deg) output.
  $h: none;
  @if $c != 0 {
    $h: math.div(math.round(color.channel($oklch, "hue", oklch) * 100), 100);
  }

  // Use string interpolation so the CSS '/' is a channel separator, not Sass division.
  @if $a < 1 {
    @return #{"oklch(#{$l} #{$c} #{$h} / #{$a})"};
  }

  @return #{"oklch(#{$l} #{$c} #{$h})"};
}
// Input: any Sass color
$converted: to-color(#0d6efd);
// Output: oklch(0.55 0.215 262.88)

$with-alpha: to-color(rgba(#0d6efd, 0.5));
// Output: oklch(0.55 0.215 262.88 / 0.5)

Because to-color() outputs a plain string using the oklch color space, it works in any browser that supports oklch. If you want to use a different color space as your system default, replace calls to to-color() with a function that outputs your preferred space (e.g. color(display-p3 ...) or lch(...)).

to-opacity()

Wraps a color or CSS variable in a CSS relative color expression that applies an opacity value via the oklch channel syntax. This is how Chassis applies contextual opacity to tokens at runtime without generating separate color variants.

/// Generate a CSS relative color expression that applies an opacity variable to a CSS custom property
/// Uses the CSS relative color syntax to derive an oklch color from an existing
/// CSS variable and apply a separate opacity (alpha) CSS variable to it.
///
/// @param {Color|String} $color - Color value or `var()` expression used as the relative color origin
/// @param {Number|String} $opacity - Alpha value (0–1) or `var()` expression for the opacity channel
/// @return {String} - Unquoted CSS relative color string: oklch(from {color} l c h / {opacity})
/// @example
///   to-opacity(var(--cx-primary), 0.5)  => oklch(from var(--cx-primary) l c h / 0.5)
///   to-opacity($white, .15)  => oklch(from #fff l c h / 0.15)
///   to-opacity(var(--cx-fg-main), var(--cx-fg-opacity, 1))  => oklch(from var(--cx-fg-main) l c h / var(--cx-fg-opacity, 1))
@function to-opacity($color, $opacity) {
  @return #{"oklch(from #{$color} l c h / #{$opacity})"};
}
// Static alpha on a CSS variable
to-opacity(var(--cx-primary), 0.5)
// => oklch(from var(--cx-primary) l c h / 0.5)

// Static alpha on a Sass color
to-opacity($white, .15)
// => oklch(from #fff l c h / 0.15)

// Dynamic alpha via another CSS variable
to-opacity(var(--cx-fg-main), var(--cx-fg-opacity, 1))
// => oklch(from var(--cx-fg-main) l c h / var(--cx-fg-opacity, 1))

The from keyword and channel names (l c h) are specific to oklch. If you change the default color space, update the channel names accordingly — for example, lch(from ... l c h) stays the same, but color(display-p3 from ... r g b) would differ.

opacity-var()

A shorthand variant of to-opacity() designed to work as a map-loop callback. It takes a color token identifier and an opacity variable name and returns a relative color expression using the --cx- prefix convention.

/// Generate a CSS relative color expression that applies a component opacity variable to a color token
/// Uses CSS relative color syntax to derive an oklch color from a CSS custom property and apply
/// a separate opacity CSS variable to it. Designed to work as a `map-loop` callback.
///
/// @param {String} $identifier - Color CSS variable name without prefix (e.g., "primary", "fg-main")
/// @param {String} $target - Opacity variable component name without prefix (e.g., "fg", "bg", "icon")
/// @return {String} - Unquoted CSS relative color: oklch(from var(--cx-{identifier}) l c h / var(--cx-{target}-opacity, 1))
/// @example
///   opacity-var("primary", "bg")  => oklch(from var(--cx-primary) l c h / var(--cx-bg-opacity, 1))
///   opacity-var("fg-main", "fg")  => oklch(from var(--cx-fg-main) l c h / var(--cx-fg-opacity, 1))
@function opacity-var($identifier, $target) {
  @return #{"oklch(from var(--#{$prefix}#{$identifier}) l c h / var(--#{$prefix}#{$target}-opacity, 1))"};
}
opacity-var("primary", "bg")
// => oklch(from var(--cx-primary) l c h / var(--cx-bg-opacity, 1))

opacity-var("fg-main", "fg")
// => oklch(from var(--cx-fg-main) l c h / var(--cx-fg-opacity, 1))

This function is used when generating utility classes and component styles that need to respect a runtime opacity variable.

Escape SVG

We use the escape-svg function to escape the <, > and # characters for SVG background images. When using the escape-svg function, data URIs must be quoted.

Add and Subtract functions

We use the add and subtract functions to wrap the CSS calc function. The primary purpose of these functions is to avoid errors when a “unitless” 0 value is passed into a calc expression. Expressions like calc(10px - 0) will return an error in all browsers, despite being mathematically correct.

Example where the calc is valid:

$border-radius: .25rem;
$border-width: 1px;

.element {
  // Output calc(.25rem - 1px) is valid
  border-radius: calc($border-radius - $border-width);
}

.element {
  // Output the same calc(.25rem - 1px) as above
  border-radius: subtract($border-radius, $border-width);
}

Example where the calc is invalid:

$border-radius: .25rem;
$border-width: 0;

.element {
  // Output calc(.25rem - 0) is invalid
  border-radius: calc($border-radius - $border-width);
}

.element {
  // Output .25rem
  border-radius: subtract($border-radius, $border-width);
}

Mixins

Our scss/mixins/ directory has a ton of mixins that power parts of Chassis CSS and can also be used across your own project.

Color schemes

A shorthand mixin for the prefers-color-scheme media query is available with support for light and dark color schemes. See the color modes documentation for information on our color mode mixin.

/// Wrap `@content` in a `@media (prefers-color-scheme: ...)` query.
///
/// @param {String} $name - Color scheme name (`light` or `dark`)
/// @content Styles to apply under this color scheme preference
@mixin color-scheme($name) {
  @media (prefers-color-scheme: #{$name}) {
    @content;
  }
}
.custom-element {
  @include color-scheme(light) {
    // Insert light mode styles here
  }

  @include color-scheme(dark) {
    // Insert dark mode styles here
  }
}