Rules for Styling
Jaspr provides a native, type-safe CSS-in-Dart styling API via the Styles class.
1. Component-Level Styles (@css annotation)
The recommended approach for defining styles in Jaspr is inside your components to ensure better code locality.
- Rule 1: You MUST use the
@css annotation on a static getter returning List<StyleRule> to scope styles to a component.
- Rule 2: You MUST use specific CSS selectors (like
.main classes or #id ids) within css() to prevent your styles from bleeding into other components, as @css styles are globalized during rendering.
class App extends StatelessComponent {
const App({super.key});
@override
Component build(BuildContext context) {
return div(classes: 'main', [
p([.text('Hello World')]),
]);
}
@css
static List<StyleRule> get styles => [
css('.main', [
// Use css('&') to refer to the parent selector (.main)
css('&').styles(
width: 100.px, // Note: Use `.px`, `.rem`, `.percent` extensions on numbers.
padding: .all(10.rem),
display: .flex,
),
css('&:hover').styles(
backgroundColor: Colors.blue,
),
css('p').styles(
color: Colors.blue,
),
]),
// Responsive queries
css.media(MediaQuery.screen(maxWidth: 600.px), [
css('.main').styles(flexDirection: .column),
])
];
}
2. Inline Styles
You can pass Styles instances directly to native HTML components. This renders as the style="..." HTML attribute.
- Rule 1: You MUST ONLY use inline styles for dynamic styles (e.g., styles that change based on state or parameters).
- Rule 2: For static styles, you MUST use the component-level
@css annotation.
- Rule 3: You MUST NOT use inline styles for complex rules like media queries, hover states, or animations.
// Example of dynamic inline styling driven by state
class ColorBox extends StatelessComponent {
final Color boxColor;
const ColorBox({required this.boxColor, super.key});
@override
Component build(BuildContext context) {
return div(styles: Styles(backgroundColor: boxColor), []);
}
}
3. Global @css Styles
You can define a global set of styles directly from Dart using the @css annotation on a global variable or getter.
- Rule 1: Just like component-level
@css, global @css is ONLY supported in server and static modes.
- Rule 2: You MUST place global
@css annotations on top-level getters or static getters returning List<StyleRule>.
@css
List<StyleRule> get globalStyles => [
css('body').styles(
margin: .zero,
fontFamily: .list([FontFamily('Roboto'), FontFamilies.sansSerif]),
backgroundColor: Colors.white,
),
css('a').styles(
textDecoration: TextDecoration(line: .none),
color: Colors.blue,
),
];
4. Global External Stylesheets
- Rule: If you are using raw
.css files (or other css frameworks like sass/scss, tailwind, etc.), you MUST include them using a <link rel="stylesheet"> element inside the Document(head: [...]) component (for server/static mode) or in web/index.html (for client mode).
Jaspr Styles Properties
To avoid guessing CSS properties and overwhelming context, Jaspr maps CSS properties to strongly-typed Dart classes.
Here is the full list of properties both the Styles() class and .styles() method support:
Styles({ // or .styles({
All? all,
// Box Styles
String? content,
Display? display,
Position? position,
ZIndex? zIndex,
Unit? width,
Unit? height,
Unit? minWidth,
Unit? minHeight,
Unit? maxWidth,
Unit? maxHeight,
AspectRatio? aspectRatio,
Padding? padding,
Margin? margin,
BoxSizing? boxSizing,
Border? border,
BorderRadius? radius,
Outline? outline,
double? opacity,
Visibility? visibility,
Overflow? overflow,
Appearance? appearance,
BoxShadow? shadow,
Filter? filter,
Filter? backdropFilter,
Cursor? cursor,
UserSelect? userSelect,
PointerEvents? pointerEvents,
Animation? animation,
Transition? transition,
Transform? transform,
// Flexbox Styles
FlexDirection? flexDirection,
FlexWrap? flexWrap,
JustifyContent? justifyContent,
AlignItems? alignItems,
AlignContent? alignContent,
// Grid Styles
GridTemplate? gridTemplate,
List<TrackSize>? autoRows,
List<TrackSize>? autoColumns,
JustifyItems? justifyItems,
Gap? gap,
// Item Styles
Flex? flex,
int? order,
AlignSelf? alignSelf,
JustifySelf? justifySelf,
GridPlacement? gridPlacement,
// List Styles
ListStyle? listStyle,
ImageStyle? listImage,
ListStylePosition? listPosition,
// Text Styles
Color? color,
TextAlign? textAlign,
FontFamily? fontFamily,
Unit? fontSize,
FontWeight? fontWeight,
FontStyle? fontStyle,
TextDecoration? textDecoration,
TextTransform? textTransform,
Unit? textIndent,
Unit? letterSpacing,
Unit? wordSpacing,
Unit? lineHeight,
TextShadow? textShadow,
TextOverflow? textOverflow,
WhiteSpace? whiteSpace,
Quotes? quotes,
// Background Styles
Color? backgroundColor,
ImageStyle? backgroundImage,
BackgroundOrigin? backgroundOrigin,
BackgroundPosition? backgroundPosition,
BackgroundAttachment? backgroundAttachment,
BackgroundRepeat? backgroundRepeat,
BackgroundSize? backgroundSize,
BackgroundClip? backgroundClip,
// Raw Styles
Map<String, String>? raw,
})
- Rule 1: You MUST define all properties in the order they are defined in the
Styles class.
- Rule 2: You MUST use dot-shorthands for all style properties and values where applicable (e.g.,
padding: .all(10.px) instead of padding: Padding.all(Unit.pixels(10)), or justifyContent: .center instead of justifyContent: JustifyContent.center).
- Rule 3: You MUST use
raw for any properties that are not supported by the Styles class.
IMPORTANT: Before writing styles in one of the below areas, you MUST read the respective reference file provided alongside this skill:
references/sizing.md (Units like .px, .percent, dimensions, margin, padding, borders, border-radius)
references/color.md (Colors, HEX, RGB, HSL values)
references/box.md (Position, overflow, transform, background, cursor, filters, z-index, visibility)
references/typography.md (Text alignment, fonts, text decoration, letter spacing)
references/flexbox.md (Flexbox container/item, flex layout, alignments)
references/grid.md (Grid layout, track sizes, gaps, templates)
references/animation.md (Transitions, Animations, keyframes, curves, durations)
1---2name: jaspr-styling3description: Write type-safe CSS-in-Dart to style Jaspr components. Use this skill when styling components, implementing themes, or using CSS properties.4---56## Rules for Styling78Jaspr provides a native, type-safe **CSS-in-Dart** styling API via the `Styles` class.910### 1. Component-Level Styles (`@css` annotation)1112The recommended approach for defining styles in Jaspr is inside your components to ensure better code locality.1314- **Rule 1:** You MUST use the `@css` annotation on a static getter returning `List<StyleRule>` to scope styles to a component.15- **Rule 2:** You MUST use specific CSS selectors (like `.main` classes or `#id` ids) within `css()` to prevent your styles from bleeding into other components, as `@css` styles are globalized during rendering.1617```dart18class App extends StatelessComponent {19 const App({super.key});2021 @override22 Component build(BuildContext context) {23 return div(classes: 'main', [24 p([.text('Hello World')]),25 ]);26 }2728 @css29 static List<StyleRule> get styles => [30 css('.main', [31 // Use css('&') to refer to the parent selector (.main)32 css('&').styles(33 width: 100.px, // Note: Use `.px`, `.rem`, `.percent` extensions on numbers.34 padding: .all(10.rem),35 display: .flex,36 ),37 css('&:hover').styles(38 backgroundColor: Colors.blue,39 ),40 css('p').styles(41 color: Colors.blue,42 ),43 ]),44 // Responsive queries45 css.media(MediaQuery.screen(maxWidth: 600.px), [46 css('.main').styles(flexDirection: .column),47 ])48 ];49}50```5152### 2. Inline Styles5354You can pass `Styles` instances directly to native HTML components. This renders as the `style="..."` HTML attribute.5556- **Rule 1:** You MUST ONLY use inline styles for **dynamic styles** (e.g., styles that change based on state or parameters).57- **Rule 2:** For static styles, you MUST use the component-level `@css` annotation.58- **Rule 3:** You MUST NOT use inline styles for complex rules like media queries, hover states, or animations.5960```dart61// Example of dynamic inline styling driven by state62class ColorBox extends StatelessComponent {63 final Color boxColor;6465 const ColorBox({required this.boxColor, super.key});6667 @override68 Component build(BuildContext context) {69 return div(styles: Styles(backgroundColor: boxColor), []);70 }71}72```7374### 3. Global `@css` Styles7576You can define a global set of styles directly from Dart using the `@css` annotation on a global variable or getter.7778- **Rule 1:** Just like component-level `@css`, global `@css` is ONLY supported in **server** and **static** modes.79- **Rule 2:** You MUST place global `@css` annotations on top-level getters or static getters returning `List<StyleRule>`.8081```dart82@css83List<StyleRule> get globalStyles => [84 css('body').styles(85 margin: .zero,86 fontFamily: .list([FontFamily('Roboto'), FontFamilies.sansSerif]),87 backgroundColor: Colors.white,88 ),89 css('a').styles(90 textDecoration: TextDecoration(line: .none),91 color: Colors.blue,92 ),93];94```9596### 4. Global External Stylesheets9798- **Rule:** If you are using raw `.css` files (or other css frameworks like sass/scss, tailwind, etc.), you MUST include them using a `<link rel="stylesheet">` element inside the `Document(head: [...])` component (for server/static mode) or in `web/index.html` (for client mode).99100---101102## Jaspr Styles Properties103104To avoid guessing CSS properties and overwhelming context, Jaspr maps CSS properties to strongly-typed Dart classes.105106Here is the full list of properties both the `Styles()` class and `.styles()` method support:107108```dart109Styles({ // or .styles({110 All? all,111 // Box Styles112 String? content,113 Display? display,114 Position? position,115 ZIndex? zIndex,116 Unit? width,117 Unit? height,118 Unit? minWidth,119 Unit? minHeight,120 Unit? maxWidth,121 Unit? maxHeight,122 AspectRatio? aspectRatio,123 Padding? padding,124 Margin? margin,125 BoxSizing? boxSizing,126 Border? border,127 BorderRadius? radius,128 Outline? outline,129 double? opacity,130 Visibility? visibility,131 Overflow? overflow,132 Appearance? appearance,133 BoxShadow? shadow,134 Filter? filter,135 Filter? backdropFilter,136 Cursor? cursor,137 UserSelect? userSelect,138 PointerEvents? pointerEvents,139 Animation? animation,140 Transition? transition,141 Transform? transform,142 // Flexbox Styles143 FlexDirection? flexDirection,144 FlexWrap? flexWrap,145 JustifyContent? justifyContent,146 AlignItems? alignItems,147 AlignContent? alignContent,148 // Grid Styles149 GridTemplate? gridTemplate,150 List<TrackSize>? autoRows,151 List<TrackSize>? autoColumns,152 JustifyItems? justifyItems,153 Gap? gap,154 // Item Styles155 Flex? flex,156 int? order,157 AlignSelf? alignSelf,158 JustifySelf? justifySelf,159 GridPlacement? gridPlacement,160 // List Styles161 ListStyle? listStyle,162 ImageStyle? listImage,163 ListStylePosition? listPosition,164 // Text Styles165 Color? color,166 TextAlign? textAlign,167 FontFamily? fontFamily,168 Unit? fontSize,169 FontWeight? fontWeight,170 FontStyle? fontStyle,171 TextDecoration? textDecoration,172 TextTransform? textTransform,173 Unit? textIndent,174 Unit? letterSpacing,175 Unit? wordSpacing,176 Unit? lineHeight,177 TextShadow? textShadow,178 TextOverflow? textOverflow,179 WhiteSpace? whiteSpace,180 Quotes? quotes,181 // Background Styles182 Color? backgroundColor,183 ImageStyle? backgroundImage,184 BackgroundOrigin? backgroundOrigin,185 BackgroundPosition? backgroundPosition,186 BackgroundAttachment? backgroundAttachment,187 BackgroundRepeat? backgroundRepeat,188 BackgroundSize? backgroundSize,189 BackgroundClip? backgroundClip,190 // Raw Styles191 Map<String, String>? raw,192})193```194195- **Rule 1:** You MUST define all properties in the order they are defined in the `Styles` class.196- **Rule 2:** You MUST use dot-shorthands for all style properties and values where applicable (e.g., `padding: .all(10.px)` instead of `padding: Padding.all(Unit.pixels(10))`, or `justifyContent: .center` instead of `justifyContent: JustifyContent.center`).197- **Rule 3:** You MUST use `raw` for any properties that are not supported by the `Styles` class.198199**IMPORTANT:** Before writing styles in one of the below areas, you **MUST** read the respective reference file provided alongside this skill:200201- `references/sizing.md` (Units like `.px`, `.percent`, dimensions, margin, padding, borders, border-radius)202- `references/color.md` (Colors, HEX, RGB, HSL values)203- `references/box.md` (Position, overflow, transform, background, cursor, filters, z-index, visibility)204- `references/typography.md` (Text alignment, fonts, text decoration, letter spacing)205- `references/flexbox.md` (Flexbox container/item, flex layout, alignments)206- `references/grid.md` (Grid layout, track sizes, gaps, templates)207- `references/animation.md` (Transitions, Animations, keyframes, curves, durations)