- Transpiling modern/future features to work on all browsers (including vendor prefixing)
- Minification
- CSS Modules
- Tailwind (through a native bundler plugin)
Transpiling
Transpiling and vendor prefixing are enabled by default, so you can use modern and future CSS features without worrying about browser compatibility. Bun’s CSS parser and bundler is a direct port of LightningCSS, with a bundling approach inspired by esbuild. The transpiler converts modern CSS syntax into backwards-compatible equivalents that work across browsers.Thanks to the authors of LightningCSS and esbuild for their work.
Browser Compatibility
By default, Bun’s CSS bundler targets the following browsers:- ES2020
- Edge 88+
- Firefox 78+
- Chrome 87+
- Safari 14+
Syntax Lowering
Nesting
With CSS Nesting, you write child styles directly inside their parent blocks instead of repeating parent selectors across your CSS file.styles.css
styles.css
styles.css
styles.css
Color mix
Thecolor-mix() function blends two colors at a given ratio in a chosen color space. Use it to create color variations without calculating the resulting values yourself.
styles.css
styles.css
Relative colors
Relative color syntax modifies individual components of an existing color. Adjust attributes like lightness, saturation, or individual channels without recalculating the entire color.styles.css
LAB colors
Modern CSS supports the perceptually uniform color spaces LAB, LCH, OKLAB, and OKLCH, which can represent colors outside the standard RGB gamut.styles.css
styles.css
Color function
Thecolor() function specifies colors in predefined color spaces beyond traditional RGB, giving you access to wider color gamuts.
styles.css
styles.css
HWB colors
The HWB (Hue, Whiteness, Blackness) color model expresses colors based on how much white or black is mixed with a pure hue. This makes tints and shades more direct to create than with RGB or HSL values.styles.css
styles.css
Color notation
Modern CSS supports space-separated RGB and HSL values (no commas) and hex colors with an alpha channel.styles.css
styles.css
light-dark() color function
Thelight-dark() function takes two colors and applies one based on the current color scheme, so styles respect the user’s system preference without media queries.
styles.css
light-dark(), Bun’s CSS bundler converts it to CSS variables with fallbacks:
styles.css
Logical properties
CSS logical properties define layout, spacing, and sizing relative to the document’s writing mode and text direction rather than physical screen directions, so layouts adapt to different writing systems.styles.css
styles.css
:dir() selector isn’t supported, Bun generates additional fallbacks.
:dir() selector
The:dir() pseudo-class styles elements based on their text direction (RTL or LTR), as determined by the document or explicit direction attributes. Use it to write direction-aware styles without JavaScript.
styles.css
:dir() selector, Bun’s CSS bundler converts it to the more widely supported :lang() selector with appropriate language mappings:
styles.css
:lang() aren’t supported, Bun generates further fallbacks.
:lang() selector
The:lang() pseudo-class targets elements based on their language. To group rules for related languages, pass multiple language codes to a single :lang().
styles.css
:lang() selector, Bun’s CSS bundler converts this syntax to the :is() selector with the same behavior:
styles.css
:is() as well.
:is() selector
The:is() pseudo-class function (formerly :matches()) takes a selector list and matches if any selector in the list matches.
styles.css
:is(), Bun’s CSS bundler provides fallbacks using vendor-prefixed alternatives:
:not() selector
The:not() pseudo-class excludes elements that match a selector. The modern version accepts multiple arguments to exclude several patterns with one :not().
styles.css
:not(), Bun’s CSS bundler converts this syntax to a more compatible form with the same behavior:
styles.css
:is() isn’t supported, Bun can generate further fallbacks:
styles.css
Math functions
CSS includes standard math functions (round(), mod(), rem(), abs(), sign()), trigonometric functions (sin(), cos(), tan(), asin(), acos(), atan(), atan2()), and exponential functions (pow(), sqrt(), exp(), log(), hypot()).
styles.css
styles.css
Media query ranges
Media query range syntax expresses breakpoints with comparison operators (<, >, <=, >=) instead of the more verbose min- and max- prefixes.
styles.css
styles.css
Shorthands
CSS has introduced several shorthand properties that combine multiple longhand properties.styles.css
styles.css
Double position gradients
Double position gradient syntax specifies the same color at two adjacent positions to create a hard color stop: a sharp transition instead of a smooth fade. Use it for stripes, color bands, and other multi-color designs.styles.css
styles.css
system-ui font
Thesystem-ui generic font family uses the device’s native UI font.
styles.css
system-ui, Bun’s CSS bundler expands it to a cross-platform font stack:
styles.css
CSS Modules
Bun’s bundler also supports CSS modules, with the following features:- Detecting CSS module files (
.module.css) with no configuration - Composition (
composesproperty) - Importing CSS modules into JSX/TSX
- Warnings/errors for invalid usages of CSS modules
.module.css extension) where all class names and animations are scoped to the file. This helps you avoid class name collisions, as CSS declarations are globally scoped by default.
Bun’s bundler transforms locally scoped class names into unique identifiers.
Getting started
Create a CSS file with the.module.css extension:
styles.module.css
other-styles.module.css
app.tsx
app.tsx
app.tsx
Composition
CSS modules can compose class selectors together to reuse style rules across multiple classes. For example:styles.module.css
styles.module.css
composes:
Composition Rules: - A
composes property must come before any regular CSS properties or declarations - You can
only use composes on a simple selector with a single class namestyles.module.css
Composing from a separate CSS module file
You can also compose from a separate CSS module file:background.module.css
styles.module.css