Skip to main content
Asset Processing
CSS
Bun’s bundler has built-in support for CSS with modern features
Bun’s bundler has built-in support for CSS with the following features:
Bun’s CSS bundler automatically converts this nested syntax into traditional flat CSS that works in all browsers:
You can also nest media queries and other at-rules inside selectors, eliminating the need to repeat selector patterns:
This compiles to:
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:
This feature is particularly useful for creating color systems with programmatically derived shades, tints, and accents without needing preprocessors or custom tooling.
Bun’s CSS bundler computes these relative color modifications at build time (when not using CSS variables) and generates static color values for browser compatibility:
This approach is extremely useful for theme generation, creating accessible color variants, or building color scales based on mathematical relationships instead of hard-coding each value.
Bun’s CSS bundler automatically converts these advanced color formats to backwards-compatible alternatives for browsers that don’t yet support them:
This layered approach ensures optimal color rendering across all browsers while allowing you to use the latest color technologies in your designs.
For browsers that don’t support these advanced color functions yet, Bun’s CSS bundler provides appropriate RGB fallbacks:
This functionality lets you use modern color spaces immediately while ensuring your designs remain functional across all browsers, with optimal colors displayed in supporting browsers and reasonable approximations elsewhere.
Bun’s CSS bundler automatically converts HWB colors to RGB for compatibility with all browsers:
The HWB model is well-suited for creating systematic color variations for design systems, providing a more direct approach to creating consistent tints and shades than working with RGB or HSL values.
Bun’s CSS bundler automatically converts these modern color formats to ensure compatibility with older browsers:
This conversion process lets you write cleaner, more modern CSS while ensuring your styles work correctly across all browsers.
For browsers that don’t support this feature yet, Bun’s CSS bundler converts it to use CSS variables with proper fallbacks:
This approach gives you a clean way to handle light and dark themes without duplicating styles or writing complex media queries, while maintaining compatibility with browsers that don’t yet support the feature natively.
For browsers that don’t fully support logical properties, Bun’s CSS bundler compiles them to physical properties with appropriate directional adjustments:
If the
For browsers that don’t support the
This conversion lets you write direction-aware CSS that works reliably across browsers, even those that don’t yet support the
For browsers that don’t support multiple arguments in the
If needed, Bun can provide additional fallbacks for
For browsers that don’t support
For browsers that don’t support multiple arguments in
And if
This conversion ensures your negative selectors work correctly across all browsers while maintaining the correct specificity and behavior of the original selector.
Bun’s CSS bundler evaluates these mathematical expressions at build time when all values are known constants (not variables), resulting in optimized output:
This approach lets you write more expressive and maintainable CSS with meaningful mathematical relationships, which then gets compiled to optimized values for maximum browser compatibility and performance.
Bun’s CSS bundler converts these modern range queries to traditional media query syntax for compatibility with all browsers:
This lets you write more readable media queries while ensuring your stylesheets work correctly across all browsers, including those that don’t support the modern range syntax.
For browsers that don’t support these modern shorthands, Bun converts them to their component longhand properties:
This conversion ensures your stylesheets remain clean and maintainable while providing the broadest possible browser compatibility.
For browsers that don’t support this syntax, Bun’s CSS bundler automatically converts it to the traditional format by duplicating color stops:
This conversion lets you use the cleaner double position syntax in your source code while ensuring gradients display correctly in all browsers.
For browsers that don’t support
This approach gives you the simplicity of writing just
You can then import this file, for example into a TSX file:
app.tsx
The styles object from importing the CSS module file will be an object with all class names as keys and their unique identifiers as values:
app.tsx
This will output:
app.tsx
As you can see, the class names are unique to each file, avoiding any collisions!
Would be the same as writing:
There are a couple rules to keep in mind when using
- Transpiling modern/future features to work on all browsers (including vendor prefixing)
- Minification
- CSS Modules
- Tailwind (via a native bundler plugin)
Transpiling
Bun’s CSS bundler lets you use modern/future CSS features without having to worry about browser compatibility — all thanks to its transpiling and vendor prefixing features which are enabled by default. Bun’s CSS parser and bundler is a direct Rust → Zig port of LightningCSS, with a bundling approach inspired by esbuild. The transpiler converts modern CSS syntax into backwards-compatible equivalents that work across browsers.A huge thanks goes to the amazing work from the authors of LightningCSS and esbuild.
Browser Compatibility
By default, Bun’s CSS bundler targets the following browsers:- ES2020
- Edge 88+
- Firefox 78+
- Chrome 87+
- Safari 14+
Syntax Lowering
Nesting
The CSS Nesting specification allows you to write more concise and intuitive stylesheets by nesting selectors inside one another. Instead of repeating parent selectors across your CSS file, you can write child styles directly within their parent blocks.styles.css
styles.css
styles.css
styles.css
Color mix
Thecolor-mix() function blends two colors together according to a specified ratio in a chosen color space. Use it to create color variations without manually calculating the resulting values.
styles.css
styles.css
Relative colors
CSS allows you to modify individual components of a color using relative color syntax. Create color variations by adjusting specific attributes like lightness, saturation, or individual channels without having to recalculate the entire color.styles.css
LAB colors
Modern CSS supports perceptually uniform color spaces like LAB, LCH, OKLAB, and OKLCH that offer significant advantages over traditional RGB. These color spaces can represent colors outside the standard RGB gamut, resulting in more vibrant and visually consistent designs.styles.css
styles.css
Color function
Thecolor() function provides a standardized way to specify colors in various predefined color spaces, expanding your design options beyond the traditional RGB space. This allows you to access wider color gamuts and create more vibrant designs.
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. Many designers find this approach more natural for creating color variations compared to manipulating RGB or HSL values.styles.css
styles.css
Color notation
Modern CSS has introduced more concise ways to express colors. Space-separated color syntax eliminates the need for commas in RGB and HSL values, while hex colors with alpha channels provide a compact way to specify transparency.styles.css
styles.css
light-dark() color function
Thelight-dark() function implements color schemes that respect the user’s system preference without requiring media queries. This function accepts two color values and automatically selects the appropriate one based on the current color scheme context.
styles.css
styles.css
Logical properties
CSS logical properties let you define layout, spacing, and sizing relative to the document’s writing mode and text direction rather than physical screen directions. This is crucial for creating truly international layouts that automatically adapt to different writing systems.styles.css
styles.css
:dir() selector isn’t supported, additional fallbacks are automatically generated to ensure your layouts work properly across all browsers and writing systems. This makes creating internationalized designs much simpler while maintaining compatibility with older browsers.
:dir() selector
The:dir() pseudo-class selector allows you to style elements based on their text direction (RTL or LTR), letting you create direction-aware designs without JavaScript. This selector matches elements based on their directionality as determined by the document or explicit direction attributes.
styles.css
:dir() selector yet, Bun’s CSS bundler converts it to the more widely supported :lang() selector with appropriate language mappings:
styles.css
:dir() selector natively. If multiple arguments to :lang() aren’t supported, further fallbacks are automatically provided.
:lang() selector
The:lang() pseudo-class selector targets elements based on the language they’re in, enabling language-specific styling. Modern CSS supports passing multiple language codes to :lang(), letting you group language-specific rules more efficiently.
styles.css
:lang() selector, Bun’s CSS bundler converts this syntax to use the :is() selector to maintain the same behavior:
styles.css
:is() as well, ensuring your language-specific styles work across all browsers. This approach simplifies creating internationalized designs with distinct typographic and styling rules for different language groups.
:is() selector
The:is() pseudo-class function (formerly :matches()) allows you to create more concise and readable selectors by grouping multiple selectors together. It accepts a selector list as its argument and matches if any of the selectors in that list match, significantly reducing repetition in your CSS.
styles.css
:is(), Bun’s CSS bundler provides fallbacks using vendor-prefixed alternatives:
:not() selector
The:not() pseudo-class allows you to exclude elements that match a specific selector. The modern version of this selector accepts multiple arguments, letting you exclude multiple patterns with a single, concise selector.
styles.css
:not(), Bun’s CSS bundler converts this syntax to a more compatible form while preserving the same behavior:
styles.css
:is() isn’t supported, Bun can generate further fallbacks:
styles.css
Math functions
CSS now includes a rich set of mathematical functions that let you perform complex calculations directly in your stylesheets. These include 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
Modern CSS supports range syntax for media queries, allowing you to specify breakpoints using comparison operators like<, >, <=, and >= instead of the more verbose min- and max- prefixes. This syntax is more readable and matches how you typically think about values and ranges.
styles.css
styles.css
Shorthands
CSS has introduced several modern shorthand properties that improve code readability and maintainability. Bun’s CSS bundler ensures these convenient shorthands work on all browsers by converting them to their longhand equivalents when needed.styles.css
styles.css
Double position gradients
The double position gradient syntax is a modern CSS feature that allows you to create hard color stops in gradients by specifying the same color at two adjacent positions. This creates a sharp transition rather than a smooth fade, which is useful for creating stripes, color bands, and other multi-color designs.styles.css
styles.css
system-ui font
Thesystem-ui generic font family lets you use the device’s native UI font, creating interfaces that feel more integrated with the operating system. This provides a more native look and feel without having to specify different font stacks for each platform.
styles.css
system-ui, Bun’s CSS bundler automatically expands it to a comprehensive cross-platform font stack:
styles.css
system-ui in your source code while ensuring your interface adapts correctly to all operating systems and browsers. The expanded font stack includes appropriate system fonts for macOS/iOS, Windows, Android, Linux, and fallbacks for older browsers.
CSS Modules
Bun’s bundler also supports bundling CSS modules in addition to regular CSS with support for the following features:- Automatically detecting CSS module files (
.module.css) with zero configuration - Composition (
composesproperty) - Importing CSS modules into JSX/TSX
- Warnings/errors for invalid usages of CSS modules
.module.css extension) where are 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.
Under the hood, 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
Composition
CSS modules allow you to compose class selectors together. This lets you 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
