A tiny Sass helper that ports the beloved include-media API to Container Queries.
'>=md', '<lg', 'height >= 40ch'@supports guard for container-type (enabled by default)container-name) or unnamed onesch)Author: @PaulLVG
License: MIT
This project is a respectful fork and adaptation of the wonderful include-media library originally created by:
All credits go to them for their elegant Sass design and documentation style.
This project merely extends their ideas to CSS Container Queries.
Maintained by @PaulLVG
npm i -D include-container
# or
pnpm add -D include-container
Import it in your Sass folder:
// Override defaults BEFORE importing (optional)
$ic-breakpoints: (
'xxs': 320px,
'xs': 480px,
'sm': 640px,
'md': 768px,
'lg': 1024px,
'xl': 1280px,
'xxl': 1536px
);
// Optionally disable @supports guard (not recommended)
$ic-guard-supports: true;
// Now import the library
@use "include-container";
If you vend this file directly (not via npm), just
@use "include-container";with the file in your load path.
// Inline-size by default
@include container('>=md') {
.card { display: grid; gap: 1rem; }
}
// Height-based container query
@include container('height >= 40ch') {
.teaser { display: block; }
}
// Named container
@include container('>=lg', $name: 'layout') {
.sidebar { position: sticky; top: 1rem; }
}
// Combine conditions (min AND max)
@include container('>=sm', '<=lg') {
.grid { grid-template-columns: repeat(2, 1fr); }
}
// Custom tweakpoint in scope:
@include container-context(($tall: 70ch)) {
@include container('height>=tall') {
.hero { align-items: center; }
}
}
Just in case your project already includes a mixin called “container”, this syntax also works:
@include include-container('>sm') { /* ... */ }
Relies on CSS Container Queries, supported by all modern browsers:
Use the @supports (container-type: inline-size) guard to prevent errors on older engines.
include-container brings the same developer experience as include-media — but for Container Queries. No reinvented syntax, no JavaScript runtime, just pure Sass sugar syntax for modern responsive components.
Container Queries are contextual. Falling back to viewport-based media queries can break layouts when a component is designed to react to its container (e.g., a small card in a wide desktop page).
This library does not emit any media fallback by design. It optionally wraps output with:
@supports (container-type: inline-size) { ... }
So unsupported browsers just don’t apply those rules (safer default).
@include container($conditions..., $name: null, $type: null, $supports: null)'>=md', '<lg''height >= 40ch', 'block-size < 60ch'container-name in your CSS)'size', the @supports guard uses (container-type: size).
@supports rule. Defaults to $ic-guard-supports (true).The mixin supports multiple conditions by nesting @container rules (equivalent to logical AND).
Default map (override before @use):
$ic-breakpoints: (
'xxs': 320px,
'xs': 480px,
'sm': 640px,
'md': 768px,
'lg': 1024px,
'xl': 1280px,
'xxl': 1536px
);
You can refer to named breakpoints in conditions, e.g. '>=md', '<xl'.
For exclusive ranges (> / <), include-container adjusts the numeric value with a small unit interval (like include-media). Default:
$ic-unit-intervals: (
'px': 1,
'em': 0.01,
'rem': 0.1, // common when :root font-size is 62.5%
'ch': 0.1, // convenient for typographic widths
'': 0
);
So '> 40ch' becomes (min-width: 40.1ch) and '< 60ch' becomes (max-width: 59.9ch).
Enabled by default for safety:
$ic-guard-supports: true;
$type: 'size', guard uses (container-type: size).(container-type: inline-size).Set $ic-guard-supports: false only if you’re certain about your target browsers.
ch units (typographic containers)ch is roughly the width of the “0” glyph in the current font. It’s great for readable text widths. Common guidelines:
Examples:
// Make a card layout once the container is at least a comfortable text width
@include container('>= 60ch') {
.article-card { display: grid; grid-template-columns: 2fr 1fr; gap: 1.25rem; }
}
// Collapse to a single column below 45ch
@include container('< 45ch') {
.article-card { grid-template-columns: 1fr; }
}
Remember: the parent must establish a container context, e.g.:
.content-area {
container-type: inline-size; // or: size
container-name: layout;
}
MIT © @PaulLVG