The utility API is a Sass-based tool to generate utility classes.
Chassis CSS generates its utilities with the utility API: a Sass map of definitions and the mixins that turn each definition into a family of classes. The same API adds utilities to a project and changes the default ones. The examples are written in Sass maps, which the Sass documentation describes.
The $utilities map holds every utility, keyed by name. Each entry is a map that accepts these options:
| Option | Type | Default value | Description |
|---|---|---|---|
property | Required | – | Name of the property. Can be a string, a space-separated list of strings (e.g., horizontal paddings or margins), or a map of property-to-default-value pairs (see Property value mapping). |
values | Required | – | List of values, or a map when the class name differs from the value. If null is used as map key, class is not prepended to the class name. |
selector | Optional | class | Type of CSS selector in the generated CSS ruleset. Can be class, attr-starts, or attr-includes. |
child-selector | Optional | null | A child/descendant selector appended to the utility’s selector, wrapped in :where() for zero specificity. Use to target children instead of the element itself. |
class | Optional | null | Name of the generated class. If not provided and property is an array of strings, class will default to the first element of the property array. If not provided and property is a string, the values keys are used for the class names. |
variables | Optional | null | List or map of CSS custom properties to generate within each utility class. When a list, each variable receives the utility value. When a map, the provided static values are used. |
group | Optional | false | Boolean. Requires property to be a map. Emits a single shared rule for all generated selectors containing the static (non-null) property values, then emits per-value rules with only the dynamic (null-valued) properties. Keeps repeated static declarations out of every per-value class. |
state | Optional | null | List of pseudo-class variants (e.g., :hover or :focus) to generate as prefix-style classes. |
responsive | Optional | false | Boolean indicating if a breakpoint variant of each class (.md:opacity-25) should be generated. |
container | Optional | false | Boolean indicating if a container variant of each class (.@md:opacity-25) should be generated. |
important | Optional | false | Boolean indicating if !important should be added to the utility’s CSS rules. |
print | Optional | false | Boolean indicating if print classes need to be generated. |
dark | Optional | false | Boolean indicating if dark: prefixed classes should be generated, scoped to @media (prefers-color-scheme: dark). |
enabled | Optional | true | Set to false to suppress output for a utility while keeping its definition in the $utilities map. |
API explained
Every utility is an entry of the $utilities map of scss/utilities/_index.scss, which merges the maps of the partials beside it (_flex.scss, _sizing.scss, and the others) with its own. One entry generates one group of classes:
$utilities: (
"opacity": (
property: opacity,
values: (
0: 0,
25: .25,
50: .5,
75: .75,
100: 1,
)
)
);Output:
.opacity-0 { opacity: 0; }
.opacity-25 { opacity: .25; }
.opacity-50 { opacity: .5; }
.opacity-75 { opacity: .75; }
.opacity-100 { opacity: 1; }property
Required
Every utility needs the property key, with a valid CSS property as its value. The generated rules declare that property. When the class key is omitted, the property is also the default class name. Consider the text-decoration utility:
$utilities: (
"text-decoration": (
property: text-decoration,
values: none underline line-through
)
);Output:
.text-decoration-none { text-decoration: none; }
.text-decoration-underline { text-decoration: underline; }
.text-decoration-line-through { text-decoration: line-through; }Property value mapping
The property key also accepts a map of CSS property names to default values. A single utility class then sets several properties at once, and a value that is itself a map overrides individual properties.
When a property's default value in the map is null, the utility value is used directly for that property. When a default value is set, it is used unless the utility's value map contains a matching key.
A common pattern is pairing a CSS custom property (which receives the utility value) with a regular property that references it:
$utilities: (
"bg-color": (
property: (
"--bg-color": null,
"background-color": var(--bg-color)
),
class: bg,
values: (
primary: var(--primary),
danger: var(--danger),
)
)
);Output:
.bg-primary { --cx-bg-color: var(--cx-primary); background-color: var(--cx-bg-color); }
.bg-danger { --cx-bg-color: var(--cx-danger); background-color: var(--cx-bg-color); }The Sass source names custom properties without the --cx- prefix. The PostCSS preset of the package, @chassis-ui/css/postcss, adds it to the compiled CSS — see Apply the --cx- prefix.
A value can also override individual properties. When values contains a nested map whose keys match properties in the property map, those values are used for the matching property:
$utilities: (
"text-size": (
property: (
"font-size": 1rem,
"line-height": 1.5
),
class: text,
values: (
"sm": ("font-size": .875rem, "line-height": 1.25),
"lg": ("font-size": 1.25rem, "line-height": 1.75),
)
)
);Output:
.text-sm { font-size: .875rem; line-height: 1.25; }
.text-lg { font-size: 1.25rem; line-height: 1.75; }values
Required
The values key sets the values of the property that the generated class names and rules use. It takes a list or a map, written in the utility or held in a Sass variable.
As a list, like with text-decoration utilities:
values: none underline line-throughAs a map, like with opacity utilities:
values: (
0: 0,
25: .25,
50: .5,
75: .75,
100: 1,
)As a Sass variable that sets the list or map, as in the position utilities:
values: $position-valuesselector
Use the selector option to change the CSS selector used in the generated CSS ruleset. The default is a class selector. With an attribute selector — attr-starts or attr-includes — the class option is required. The aspect ratio utilities are built this way.
| Option | Description |
|---|---|
| No selector | Defaults to "class" |
"class" | Matches elements with a class that matches the specified value |
"attr-starts" | Matches elements whose class attribute starts with a string |
"attr-includes" | Matches elements whose class attribute includes a string |
attr-includes is the safer of the two attribute selectors. A starts-with selector tests the whole value of the class attribute, so [class^="name"] does not match class="example name".
As an example, to change from .ratio-* to [class*="ratio-"]:
$utilities: (
// Create an attribute selector utility
"aspect-ratio-attr": (
selector: "attr-includes",
class: "ratio-",
property: aspect-ratio,
values: var(--ratio),
),
// Create a CSS variable utility that matches the attribute selector utility
"aspect-ratio": (
property: --ratio,
class: ratio,
values: $aspect-ratios
),
);PostCSS prefixes the --ratio custom property to --cx-ratio in the compiled CSS.
Output:
[class*="ratio-"] { aspect-ratio: var(--cx-ratio); }
.ratio-auto { --cx-ratio: auto; }
.ratio-1x1 { --cx-ratio: 1 / 1; }
.ratio-4x3 { --cx-ratio: 4 / 3; }
.ratio-16x9 { --cx-ratio: 16 / 9; }
.ratio-21x9 { --cx-ratio: 21 / 9; }child-selector
Use the child-selector option to apply a CSS property to children of the element with the utility class rather than the element itself. The generated selector is wrapped in :where() for zero specificity, so any class overrides it. The built-in space-x/y and divide-x/y utilities use it.
The space-x utility uses child-selector to add horizontal spacing between direct children:
$utilities: (
"space-x": (
property: margin-inline-end,
class: space-x,
child-selector: "> :not(:last-child)",
values: (
1: .25rem,
2: .5rem,
3: 1rem,
)
)
);Output:
:where(.space-x-1 > :not(:last-child)) { margin-inline-end: .25rem; }
:where(.space-x-2 > :not(:last-child)) { margin-inline-end: .5rem; }
:where(.space-x-3 > :not(:last-child)) { margin-inline-end: 1rem; }The child-selector value can be any valid CSS selector. A striped row utility uses :nth-child():
$utilities: (
"striped-bg": (
property: background-color,
class: striped,
child-selector: "> :nth-child(odd)",
values: (
null: var(--bg-even),
)
)
);Output:
:where(.striped > :nth-child(odd)) { background-color: var(--cx-bg-even); }Or target all direct children with > *:
$utilities: (
"child-rounded": (
property: border-radius,
class: child-rounded,
child-selector: "> *",
values: (
null: var(--border-radius),
0: 0,
)
)
);Output:
:where(.child-rounded > *) { border-radius: var(--cx-border-radius); }
:where(.child-rounded-0 > *) { border-radius: 0; }class
Use the class option to change the class prefix used in the compiled CSS. For example, to change from .opacity-* to .o-*:
$utilities: (
"opacity": (
property: opacity,
class: o,
values: (
0: 0,
25: .25,
50: .5,
75: .75,
100: 1,
)
)
);Output:
.o-0 { opacity: 0; }
.o-25 { opacity: .25; }
.o-50 { opacity: .5; }
.o-75 { opacity: .75; }
.o-100 { opacity: 1; }With class: null, the keys of values are the class names:
$utilities: (
"visibility": (
property: visibility,
class: null,
values: (
visible: visible,
invisible: hidden,
)
)
);Output:
.visible { visibility: visible; }
.invisible { visibility: hidden; }Variables
Use the variables option to generate CSS custom properties within each utility class’s ruleset. The value can be either a list or a map.
When variables is a list, each variable receives the current utility value:
$utilities: (
"link-opacity": (
property: color,
class: link,
variables: link-color,
values: (
10: 10%,
50: 50%,
100: 100%,
)
),
);Output:
.link-10 { --cx-link-color: 10%; color: 10%; }
.link-50 { --cx-link-color: 50%; color: 50%; }
.link-100 { --cx-link-color: 100%; color: 100%; }When variables is a map, the provided static values are used on every generated class:
$utilities: (
"link-underline": (
property: text-decoration-color,
class: link-underline,
variables: (
"link-underline-opacity": 1
),
values: (...)
)
);Output:
.link-underline-primary {
--cx-link-underline-opacity: 1;
text-decoration-color: ...;
}Group
Use group: true together with a property map to split the generated output into two sets of rules:
- One shared rule targeting every generated selector — containing the static (non-null) property values.
- One rule per value — containing only the dynamic (null-valued) properties.
This keeps repeated static declarations out of every per-value class, which reduces the size of the output. group: true requires property to be a map; if it is a string or list the option is ignored with a warning.
A typical use case is a CSS custom property (set per value) paired with a regular property that references it (never changes):
$utilities: (
"text-context": (
property: (
"--fg-color": null,
"color": var(--fg-color),
),
class: text,
group: true,
values: (
primary: var(--primary),
danger: var(--danger),
)
)
);Output:
/* Shared rule — static properties written once */
.text-primary,
.text-danger {
color: var(--cx-fg-color);
}
/* Per-value rules — only the dynamic custom property */
.text-primary { --cx-fg-color: var(--cx-primary); }
.text-danger { --cx-fg-color: var(--cx-danger); }Group with state
When state is also set, all state-variant selectors are included in the shared rule as well as in each per-value rule:
$utilities: (
"text-context": (
property: (
"--fg-color": null,
"color": var(--fg-color),
),
class: text,
group: true,
state: hover,
values: (
primary: var(--primary),
danger: var(--danger),
)
)
);Output:
.text-primary,
.hover\:text-primary:hover,
.text-danger,
.hover\:text-danger:hover {
color: var(--cx-fg-color);
}
.text-primary,
.hover\:text-primary:hover { --cx-fg-color: var(--cx-primary); }
.text-danger,
.hover\:text-danger:hover { --cx-fg-color: var(--cx-danger); }Group with static variables
When variables is a map (static values), those variables are emitted in the shared rule rather than repeated on every per-value class:
$utilities: (
"text-context": (
property: (
"--fg-color": null,
"color": var(--fg-color),
),
class: text,
group: true,
variables: (
fg-opacity: 1,
),
values: (
primary: var(--primary),
danger: var(--danger),
)
)
);Output:
.text-primary,
.text-danger {
color: var(--cx-fg-color);
--cx-fg-opacity: 1;
}
.text-primary { --cx-fg-color: var(--cx-primary); }
.text-danger { --cx-fg-color: var(--cx-danger); }States
Use the state option to generate pseudo-class variants, such as :hover and :focus. Each state generates a class with the state as its prefix, in the pattern of the breakpoint prefixes: with state: hover, the opacity utility also compiles to .hover\:opacity-50:hover.
For several pseudo-classes, pass a space-separated list: state: hover focus.
$utilities: (
"opacity": (
property: opacity,
class: opacity,
state: hover,
values: (
0: 0,
25: .25,
50: .5,
75: .75,
100: 1,
)
)
);Output:
.hover\:opacity-0:hover { opacity: 0; }
.hover\:opacity-25:hover { opacity: .25; }
.hover\:opacity-50:hover { opacity: .5; }
.hover\:opacity-75:hover { opacity: .75; }
.hover\:opacity-100:hover { opacity: 1; }Responsive
Set responsive: true to generate a breakpoint variant of each class (.md\:opacity-25) for every breakpoint.
$utilities: (
"opacity": (
property: opacity,
responsive: true,
values: (
0: 0,
25: .25,
50: .5,
75: .75,
100: 1,
)
)
);Output:
.opacity-0 { opacity: 0; }
.opacity-25 { opacity: .25; }
.opacity-50 { opacity: .5; }
.opacity-75 { opacity: .75; }
.opacity-100 { opacity: 1; }
@media (width >= 36rem) {
.sm\:opacity-0 { opacity: 0; }
.sm\:opacity-25 { opacity: .25; }
.sm\:opacity-50 { opacity: .5; }
.sm\:opacity-75 { opacity: .75; }
.sm\:opacity-100 { opacity: 1; }
}
@media (width >= 48rem) {
.md\:opacity-0 { opacity: 0; }
.md\:opacity-25 { opacity: .25; }
.md\:opacity-50 { opacity: .5; }
.md\:opacity-75 { opacity: .75; }
.md\:opacity-100 { opacity: 1; }
}
@media (width >= 64rem) {
.lg\:opacity-0 { opacity: 0; }
.lg\:opacity-25 { opacity: .25; }
.lg\:opacity-50 { opacity: .5; }
.lg\:opacity-75 { opacity: .75; }
.lg\:opacity-100 { opacity: 1; }
}
@media (width >= 80rem) {
.xl\:opacity-0 { opacity: 0; }
.xl\:opacity-25 { opacity: .25; }
.xl\:opacity-50 { opacity: .5; }
.xl\:opacity-75 { opacity: .75; }
.xl\:opacity-100 { opacity: 1; }
}
@media (width >= 96rem) {
.\32 xl\:opacity-0 { opacity: 0; }
.\32 xl\:opacity-25 { opacity: .25; }
.\32 xl\:opacity-50 { opacity: .5; }
.\32 xl\:opacity-75 { opacity: .75; }
.\32 xl\:opacity-100 { opacity: 1; }
}Container
Set container: true to generate a container variant of each class under the @ prefix (.\@md\:opacity-25). Where a breakpoint variant follows the width of the viewport, a container variant follows the width of the nearest query container, at the same breakpoints. The option is independent of responsive: a utility can have either set of variants, or both.
$utilities: (
"opacity": (
property: opacity,
container: true,
values: (
0: 0,
50: .5,
100: 1,
)
)
);Output:
.opacity-0 { opacity: 0; }
.opacity-50 { opacity: .5; }
.opacity-100 { opacity: 1; }
@container (width >= 36rem) {
.\@sm\:opacity-0 { opacity: 0; }
.\@sm\:opacity-50 { opacity: .5; }
.\@sm\:opacity-100 { opacity: 1; }
}
@container (width >= 48rem) {
.\@md\:opacity-0 { opacity: 0; }
.\@md\:opacity-50 { opacity: .5; }
.\@md\:opacity-100 { opacity: 1; }
}
/* …and the same for lg, xl, and 2xl */The first breakpoint has no container variant: the unprefixed class applies at every width. The container rules come after the breakpoint ones, so where a breakpoint variant and a container variant both apply to an element, the container variant wins.
The layout utilities set container: true by default: display, flex (direction, grow, shrink, wrap, fill), order, justify-content, align-items, align-content, align-self, the place-* and justify-* alignment utilities, the gap utilities, grid-cols-*, grid-rows-*, grid-flow-*, auto-cols-*, auto-rows-*, space-x-*, space-y-*, divide-x, divide-y, the fraction widths with w-100 and w-auto, text-align, float, and object-fit. The other responsive utilities — margin, padding, font size, and icon size — set container: false, which keeps five more sets of them out of the CSS. Enable the option for the utilities that need it, or set $enable-container-queries to false to generate none.
The print option also generates a print: variant of each class, which applies only within @media print { ... }.
$utilities: (
"opacity": (
property: opacity,
print: true,
values: (
0: 0,
25: .25,
50: .5,
75: .75,
100: 1,
)
)
);Output:
.opacity-0 { opacity: 0; }
.opacity-25 { opacity: .25; }
.opacity-50 { opacity: .5; }
.opacity-75 { opacity: .75; }
.opacity-100 { opacity: 1; }
@media print {
.print\:opacity-0 { opacity: 0; }
.print\:opacity-25 { opacity: .25; }
.print\:opacity-50 { opacity: .5; }
.print\:opacity-75 { opacity: .75; }
.print\:opacity-100 { opacity: 1; }
}Dark
The dark option generates a dark: variant of each class, scoped to @media (prefers-color-scheme: dark). The prefix follows the convention of the breakpoint and print variants.
$utilities: (
"bg-color": (
property: background-color,
class: bg,
dark: true,
values: (
white: #fff,
black: #000,
)
)
);Output:
.bg-white { background-color: #fff; }
.bg-black { background-color: #000; }
@media (prefers-color-scheme: dark) {
.dark\:bg-white { background-color: #fff; }
.dark\:bg-black { background-color: #000; }
}Enabled
Set enabled: false to suppress the output of a utility without removing it from the $utilities map. The definition stays available to map.get() calls while the utility emits no CSS.
$utilities-overrides: (
"float": (enabled: false),
);Using the API shows the file this map belongs to.
Importance
Utilities carry no !important. The utilities cascade layer is declared last, so a utility overrides the components and helpers of the earlier layers whatever their specificity. Set important: true on a utility whose declarations must also win against unlayered CSS.
$utilities: (
"opacity": (
property: opacity,
important: true,
values: (
0: 0,
25: .25,
50: .5,
75: .75,
100: 1,
)
)
);Output:
.opacity-0 { opacity: 0 !important; }
.opacity-25 { opacity: .25 !important; }
.opacity-50 { opacity: .5 !important; }
.opacity-75 { opacity: .75 !important; }
.opacity-100 { opacity: 1 !important; }Using the API
scss/_utilities-api.scss generates the classes: it loads the $utilities map of scss/utilities and passes it to the generate-utilities-loop() mixin inside @layer utilities. A project changes the map before the classes are generated, with the $utilities-overrides variable of the same module:
// custom.scss
@use "@chassis-ui/css/scss/utilities" with (
$utilities-overrides: (
// Remove the `float` utilities
"float": null,
// Make the `border` utilities responsive
"border": (responsive: true),
// Add a utility
"cursor": (
property: cursor,
class: cursor,
values: auto pointer grab,
),
)
);
@use "@chassis-ui/css/scss/chassis";$utilities-overrides is a map keyed by utility name, merged over the default map:
nullremoves the utility.- A map under the name of a default utility is merged into its definition, option by option. An option the map leaves out keeps its default.
- A map under a new name adds a utility.
The name of a utility is its key in the maps of scss/utilities/, which the CSS section of each utility page shows.
Sass configures a module once, where it first loads. The @use … with (…) rule comes before @use "@chassis-ui/css/scss/chassis", and after the @use "@chassis-ui/css/scss/config" with (…) rule of a project that sets options. In another place, the compile stops with an error.
A file has one rule per module. A bundle that already loads scss/utilities among the parts of the framework adds the with (…) to that rule.
Modify utilities
A map under the name of a default utility changes the options it lists. The sections below show only the $utilities-overrides map; the file around it is the one above.
Enable responsive
responsive: true generates the breakpoint variants of a utility. This makes the border classes responsive:
$utilities-overrides: (
"border": (responsive: true),
);The build then has a variant of .border and .border-0 for each breakpoint:
.border { ... }
.border-0 { ... }
@media (width >= 36rem) {
.sm\:border { ... }
.sm\:border-0 { ... }
}
@media (width >= 48rem) {
.md\:border { ... }
.md\:border-0 { ... }
}
@media (width >= 64rem) {
.lg\:border { ... }
.lg\:border-0 { ... }
}
@media (width >= 80rem) {
.xl\:border { ... }
.xl\:border-0 { ... }
}
@media (width >= 96rem) {
.\32 xl\:border { ... }
.\32 xl\:border-0 { ... }
}A class name cannot start with a digit in CSS, so the 2 of 2xl is written as the escape \32 in the stylesheet. The class in the markup is 2xl:border.
Enable container variants
container: true adds container variants to a utility that does not have them by default. This generates .@md:p-lg and .@md:px-lg:
$utilities-overrides: (
"padding": (container: true),
"padding-x": (container: true),
);Rename utilities
The class option renames the classes of a utility. This renames the .ms-* utilities to .ml-*:
$utilities-overrides: (
"margin-start": (class: ml),
);Change values
values is the one option that is merged with its default, when the override gives a map: a new key adds a class, the key of a default value changes that value, and null removes the class. A key is matched by the class it names, so 25 and "25" are the same key. This adds .w-10 to the width utilities and removes .w-25:
$utilities-overrides: (
"width": (
values: (
10: 10%,
25: null,
)
),
);A list of values replaces the default ones. This leaves .user-select-none and .user-select-text as the only user-select utilities:
$utilities-overrides: (
"user-select": (values: none text),
);Every other option is replaced as a whole, a property map included.
Remove utilities
null removes a utility from the map, with every variant of its classes:
$utilities-overrides: (
"width": null,
"float": null,
);A name that is not in the map is ignored, since an option such as $enable-grid-system can leave a utility out. enabled: false suppresses the classes and keeps the definition.
Add utilities
A map under a new name adds a utility. It needs property and values, and the compile stops without them, as it does for a misspelled name of a default utility. The value can use the variables, maps, and functions of the framework that the file loads first:
// custom.scss
@use "@chassis-ui/css/scss/maps" as *;
@use "@chassis-ui/css/scss/utilities" with (
$utilities-overrides: (
"scroll-margin-top": (
property: scroll-margin-top,
class: scroll-mt,
values: $spaces,
),
)
);
@use "@chassis-ui/css/scss/chassis";An added utility comes after the default ones in the utilities layer, and a changed one keeps its place.
A second call of generate-utilities-loop() adds utilities too, from any stylesheet that loads after the framework. The mixin takes a map of definitions and the $breakpoints map; @layer utilities puts the new classes in the layer of the default ones.
// custom.scss
@use "@chassis-ui/css/scss/chassis";
@use "@chassis-ui/css/scss/config" as *;
@use "@chassis-ui/css/scss/mixins" as *;
@layer utilities {
@include generate-utilities-loop(
(
"cursor": (
property: cursor,
class: cursor,
responsive: true,
values: auto pointer grab,
)
),
$breakpoints
);
}Assign the map
An override cannot read the default map: Sass evaluates a with (…) configuration before the module loads. For a change computed from the defaults, load scss/utilities, assign to its $utilities variable, and load the framework after the assignment. This build keeps four utilities of the map and drops every other one:
// custom.scss
@use "@chassis-ui/css/scss/functions" as *;
@use "@chassis-ui/css/scss/utilities" as utilities;
utilities.$utilities: map-get-multiple(
utilities.$utilities,
("display", "margin", "padding", "gap")
);
@use "@chassis-ui/css/scss/chassis";The classes are generated when scss/chassis loads. An assignment after that rule changes nothing in the build, and Sass reports nothing.
Replace the map
$utilities is a !default variable of scss/utilities. Configuring it before the framework loads replaces the whole map: the build has the utilities of the configured map and none of the default ones. $utilities-overrides is merged over a configured map too.
// custom.scss
@use "@chassis-ui/css/scss/utilities" with (
$utilities: (
"opacity": (
property: opacity,
values: (
0: 0,
50: .5,
100: 1,
)
)
)
);
@use "@chassis-ui/css/scss/chassis";Tailwind entry
The Tailwind entry generates its @utility rules from the same map. A project that compiles scss/tailwind with Sass configures $utilities-overrides in the same way, before @use "@chassis-ui/css/scss/tailwind".
A file that loads scss/tailwind/utilities on its own, one of the separate modules of the Tailwind guide, has two rules for the namespace utilities, and Sass stops on that. Give the configured rule another:
@use "@chassis-ui/css/scss/utilities" as chassis-utilities with (
$utilities-overrides: (
"float": null,
)
);
@use "@chassis-ui/css/scss/tailwind/utilities";The prebuilt @chassis-ui/css/tailwind stylesheet, the class groups of tailwind/merge.js, and the prebuilt tailwind/safelist.css are built from the default map, and an override does not reach them. The clash check of the build covers the default class names only: Custom utilities describes what a renamed or added utility needs.