Skip to main content
Bun’s bundler has built-in support for CSS with the following features:
  • Transpile modern/future features for compatibility with all browsers (including vendor prefixes)
  • Minification
  • CSS Modules
  • Tailwind (via native bundler plugin)

Transpilation

Bun’s CSS bundler lets you use modern/future CSS features without worrying about browser compatibility—thanks to transpilation and vendor prefixing enabled by default. Bun’s CSS parser and bundler is a direct port of LightningCSS from Rust to Zig, with bundling methodology inspired by esbuild. The transpiler converts modern CSS syntax into backwards-compatible equivalents for a range of browsers.
Special thanks to the authors of LightningCSS and esbuild for their excellent work.

Browser compatibility

By default, Bun’s CSS bundler targets browsers that support:
  • ES2020
  • Edge 88+
  • Firefox 78+
  • Chrome 87+
  • Safari 14+

Syntax downleveling

Nesting

The CSS nesting specification allows you to write more concise and intuitive stylesheets by nesting selectors within each other. Instead of repeating parent selectors throughout your CSS file, you can write child styles directly inside parent blocks.
styles.css
Bun’s CSS bundler automatically converts this nested syntax into traditional flat CSS for compatibility with all browsers:
styles.css
You can also nest media queries and other at-rules within selectors, avoiding repeated selector patterns:
styles.css
Compiles to:
styles.css

Color mixing

The color-mix() function lets you easily blend two colors in a specified proportion within a chosen color space. This powerful feature lets you create color variations without manually calculating result values.
styles.css
Bun’s CSS bundler evaluates these color mixes at build time (when all color values are known, not CSS variables), generating static color values that work in all browsers:
styles.css
This feature is particularly useful for creating programmatic color systems like tints, shades, and accents without a preprocessor or custom tooling.

Relative colors

CSS now allows you to modify individual components of colors using relative color syntax. This powerful feature lets you create color variants by adjusting lightness, saturation, or individual channels without recalculating the entire color.
styles.css
Bun’s CSS bundler calculates these relative color modifications at build time (when not using CSS variables), generating static color values for browsers:
This is excellent for theme generation, creating accessible color variants, or building color scales based on mathematical relationships rather than hard-coded values.

LAB colors

Modern CSS supports perceptually uniform color spaces like LAB, LCH, OKLAB, and OKLCH, which offer significant advantages over traditional RGB. These color spaces can represent colors beyond the standard RGB gamut, bringing more vibrant and visually consistent designs.
styles.css
Bun’s CSS bundler automatically converts these advanced color formats to backwards-compatible fallbacks for browsers that haven’t implemented them yet:
styles.css
This layered approach ensures the best color rendering across all browsers while letting you use the latest color technology in your designs.

color function

The color() function provides a standardized way to specify colors using various predefined color spaces, expanding your design options beyond traditional RGB space. This enables access to wider color gamuts, creating more vibrant designs.
styles.css
Bun’s CSS bundler provides appropriate fallbacks:
styles.css

CSS Modules

CSS Modules provide local scoping for CSS by default, preventing naming conflicts and making styles more maintainable.
Button.module.css
Button.tsx
The CSS bundler will:
  1. Generate unique class names
  2. Export a mapping object
  3. Bundle the transformed CSS
Output

Importing CSS

CSS can be imported in JavaScript/TypeScript files:
app.ts
All imported CSS files are bundled into a single CSS file in the output directory:

@import statements

CSS @import statements are resolved and bundled:
main.css
All imports are inlined into a single CSS file.

url() references

Asset references in url() are resolved and copied to the output directory:
styles.css
Assets are hashed and copied:
Output

Minification

Enable CSS minification:
This removes whitespace, shortens color values, and optimizes the CSS.

Vendor prefixes

Vendor prefixes are automatically added based on the browser targets:
Input
Output

Custom properties

CSS custom properties (variables) are preserved:

Media queries

Media queries are preserved and can be nested:

CSS chunking

When code splitting is enabled, CSS is also split into chunks:
Shared CSS is extracted into separate chunk files.

Tailwind CSS

Bun provides a native bundler plugin for Tailwind CSS. See the Tailwind documentation for setup instructions.

Performance

Bun’s CSS bundler is extremely fast:
  • Written in Zig for maximum performance
  • Parallel processing of CSS files
  • Efficient memory usage
  • Fast source map generation

Limitations

  • No PostCSS support yet (use plugins as an alternative)
  • No SASS/SCSS support (but nesting is supported natively)
  • CSS layers are preserved but not optimized