AtlatestRepositorysigil-css
1
# CSS3
> S-expression CSS with nesting, themes, and media queries.5
```scheme6
(import (sigil css))7
```9
## Basic Rules11
The `css` macro creates CSS rules. Properties are written as `(property-name value)` where the property name is captured literally (not evaluated).13
```scheme14
(css->string15
(css ".button"16
(background "#007bff")17
(color "white")18
(padding "0.5rem 1rem")19
(border-radius "4px")))20
```22
Output:24
```css25
.button {26
background: #007bff;27
color: white;28
padding: 0.5rem 1rem;29
border-radius: 4px;30
}31
```33
## Nested Selectors35
Use `&` to create nested rules, similar to Sass/SCSS. The suffix is appended directly to the parent selector.37
```scheme38
(css->string39
(css ".card"40
(padding "1rem")41
(border "1px solid #ddd")42
(& ":hover"43
(box-shadow "0 2px 8px rgba(0,0,0,0.15)"))44
(& " .title"45
(font-weight "bold")46
(font-size "1.25rem"))))47
```49
Output:51
```css52
.card {53
padding: 1rem;54
border: 1px solid #ddd;55
}57
.card:hover {58
box-shadow: 0 2px 8px rgba(0,0,0,0.15);59
}61
.card .title {62
font-weight: bold;63
font-size: 1.25rem;64
}65
```67
Nested `css` forms create child selectors (space-separated):69
```scheme70
(css ".nav"71
(display "flex")72
(css ".item"73
(padding "0.5rem")))74
```76
## css->string78
Render any number of CSS fragments (rules, variables, media queries, keyframes) into a single CSS string.80
```scheme81
(css->string82
(css-vars (--primary "#007bff"))83
(css ".btn" (background "var(--primary)"))84
(css ".btn-lg" (padding "1rem 2rem")))85
```87
## CSS Variables89
Define CSS custom properties with `css-vars`. Creates a `:root` block.91
```scheme92
(css->string93
(css-vars94
(--bg "#0a0a0a")95
(--fg "#e0e0e0")96
(--accent "#f0c040"))97
(css "body"98
(background "var(--bg)")99
(color "var(--fg)")))100
```102
## Media Queries104
Wrap rules in a `@media` block with `css-media`.106
```scheme107
(css->string108
(css ".sidebar" (width "250px"))109
(css-media "(max-width: 768px)"110
(css ".sidebar" (display "none"))111
(css ".content" (width "100%"))))112
```114
## Keyframes116
Define CSS animations with `css-keyframes`. Step names are symbols (`from`, `to`, or percentage symbols).118
```scheme119
(css->string120
(css-keyframes "fade-in"121
(from (opacity "0"))122
(to (opacity "1")))123
(css ".fade" (animation "fade-in 0.3s ease-in")))124
```126
## Themes128
Create reusable theme definitions and convert them to CSS custom properties.130
```scheme131
(define dark-theme132
(make-theme133
'(bg "#0a0a0a")134
'(fg "#e0e0e0")135
'(accent "#f0c040")))137
;; Look up a theme value138
(theme-ref dark-theme 'bg) ; => "#0a0a0a"140
;; Reference as CSS variable141
(theme-var 'bg) ; => "var(--bg)"143
;; Convert to :root CSS variables144
(css->string145
(theme->css-vars dark-theme)146
(css "body"147
(background (theme-var 'bg))148
(color (theme-var 'fg))))149
```151
## Unit and Color Helpers153
Helpers for common CSS units and color functions.155
| Helper | Example | Result |156
|--------|---------|--------|157
| `px` | `(px 16)` | `"16px"` |158
| `em` | `(em 1.5)` | `"1.5em"` |159
| `rem` | `(rem 2)` | `"2rem"` |160
| `%` | `(% 50)` | `"50%"` |161
| `rgb` | `(rgb 255 128 0)` | `"rgb(255, 128, 0)"` |162
| `rgba` | `(rgba 0 0 0 0.5)` | `"rgba(0, 0, 0, 0.5)"` |163
| `hsl` | `(hsl 200 50 60)` | `"hsl(200, 50%, 60%)"` |164
| `important` | `(important "red")` | `"red !important"` |166
```scheme167
(css->string168
(css ".box"169
(width (% 100))170
(padding (rem 1))171
(font-size (px 14))172
(color (important "red"))))173
```175
## Additional Utilities177
### css-import179
Add `@import` rules for external stylesheets or fonts.181
```scheme182
(css->string183
(css-import "https://fonts.googleapis.com/css2?family=Inter")184
(css "body" (font-family "Inter, sans-serif")))185
```187
### css-raw189
Insert raw CSS strings for edge cases not covered by the DSL.191
```scheme192
(css->string193
(css-raw "/* Browser-specific hack */")194
(css ".main" (display "grid")))195
```197
### css-combine199
Group CSS fragments into a list for modular organization.201
```scheme202
(define button-styles203
(css-combine204
(css ".btn" (padding "0.5rem 1rem") (border "none"))205
(css ".btn-primary" (background "blue") (color "white"))206
(css ".btn-danger" (background "red") (color "white"))))208
(css->string button-styles)209
```211
## Common Patterns213
### Component Styles215
```scheme216
(import (sigil css))218
(define card-styles219
(css-combine220
(css ".card"221
(border "1px solid #e0e0e0")222
(border-radius "8px")223
(padding "1.5rem")224
(& ":hover"225
(box-shadow "0 4px 12px rgba(0,0,0,0.1)"))226
(& " .card-title"227
(font-size "1.25rem")228
(margin-bottom "0.5rem"))229
(& " .card-body"230
(color "#666")))))231
```233
### Responsive Design235
```scheme236
(css->string237
(css ".grid"238
(display "grid")239
(grid-template-columns "repeat(3, 1fr)")240
(gap (rem 1)))241
(css-media "(max-width: 768px)"242
(css ".grid"243
(grid-template-columns "1fr"))))244
```246
### Themed Application248
```scheme249
(import (sigil css)250
(sigil sxml))252
(define theme253
(make-theme254
'(bg "#ffffff")255
'(fg "#1a1a1a")256
'(primary "#007bff")))258
(define app-css259
(css->string260
(theme->css-vars theme)261
(css "body"262
(background (theme-var 'bg))263
(color (theme-var 'fg)))264
(css ".btn-primary"265
(background (theme-var 'primary))266
(color "white"))))268
(sxml->html `(style (*raw* ,app-css)))269
```