|
| 1 | +# CSS Architecture: Intrinsic and Container-Driven Layout |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This theme uses a modern, intrinsic CSS architecture that eliminates fixed breakpoints in favor of fluid, container-driven layouts. This approach provides better adaptability across all viewport sizes without hardcoded media query breakpoints. |
| 6 | + |
| 7 | +## Key Principles |
| 8 | + |
| 9 | +### 1. Fluid Typography with `clamp()` |
| 10 | + |
| 11 | +Instead of fixed font sizes at specific breakpoints, we use `clamp()` to create fluid typography that scales smoothly: |
| 12 | + |
| 13 | +```css |
| 14 | +font-size: clamp(min, preferred, max); |
| 15 | +``` |
| 16 | + |
| 17 | +Example: |
| 18 | +```css |
| 19 | +--body-font-size: clamp(1.0625em, 1rem + 0.25vw, 1.125em); |
| 20 | +/* Fluid between 17px and 18px based on viewport */ |
| 21 | +``` |
| 22 | + |
| 23 | +### 2. Container Queries |
| 24 | + |
| 25 | +Where supported, we use CSS Container Queries to make components responsive to their container size rather than viewport size: |
| 26 | + |
| 27 | +```css |
| 28 | +@supports (container-type: inline-size) { |
| 29 | + main { |
| 30 | + container-type: inline-size; |
| 31 | + container-name: main-content; |
| 32 | + } |
| 33 | + |
| 34 | + @container main-content (min-width: 1360px) { |
| 35 | + /* Styles applied based on container width */ |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +This allows components to adapt based on available space, making them truly modular and reusable. |
| 41 | + |
| 42 | +### 3. Fallback Support |
| 43 | + |
| 44 | +For browsers that don't support container queries, we provide fallback media queries: |
| 45 | + |
| 46 | +```css |
| 47 | +@supports not (container-type: inline-size) { |
| 48 | + @media screen and (min-width: 1360px) { |
| 49 | + /* Fallback styles */ |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +### 4. Fluid Spacing and Sizing |
| 55 | + |
| 56 | +We use `clamp()` and `min()` for spacing and sizing to create layouts that adapt smoothly: |
| 57 | + |
| 58 | +```css |
| 59 | +max-width: min(800px, 95vw); |
| 60 | +padding: 0 clamp(0.5rem, 2vw, 1rem); |
| 61 | +gap: clamp(0.4rem, 1vw, 0.6rem); |
| 62 | +``` |
| 63 | + |
| 64 | +### 5. Semantic Breakpoints |
| 65 | + |
| 66 | +When media queries are necessary, we use semantic rem-based breakpoints that relate to content needs rather than device sizes: |
| 67 | + |
| 68 | +- `48rem` (~768px) - Typical tablet/small desktop threshold |
| 69 | +- `64rem` (~1024px) - Desktop threshold |
| 70 | + |
| 71 | +## File Structure |
| 72 | + |
| 73 | +### Core Variables (`vars.css`) |
| 74 | + |
| 75 | +Contains all CSS custom properties with fluid sizing: |
| 76 | +- Fluid font sizes using `clamp()` |
| 77 | +- Responsive spacing using viewport units |
| 78 | +- Flexible width constraints |
| 79 | + |
| 80 | +### Foundation (`base.css`) |
| 81 | + |
| 82 | +Base element styles without media queries. Typography scales fluidly based on CSS variables. |
| 83 | + |
| 84 | +### Layout Components |
| 85 | + |
| 86 | +- **`body.css`**: Main layout structure with container support |
| 87 | +- **`main.css`**: Content area with container queries for TOC display |
| 88 | +- **`nav.css`**: Navigation with progressive enhancement |
| 89 | +- **`header.css`**: Navbar with semantic breakpoints |
| 90 | +- **`doc.css`**: Document content with fluid typography |
| 91 | + |
| 92 | +### UI Components |
| 93 | + |
| 94 | +- **`toc.css`**: Table of contents with container query support |
| 95 | +- **`toolbar.css`**: Toolbar with fluid sizing |
| 96 | +- **`pagination.css`**: Pagination controls |
| 97 | +- **`breadcrumbs.css`**: Breadcrumb navigation |
| 98 | +- **`preview.css`**: Blog preview cards with container queries |
| 99 | +- **`share.css`**: Share buttons with fluid sizing |
| 100 | + |
| 101 | +## Benefits |
| 102 | + |
| 103 | +1. **No Fixed Breakpoints**: Layout adapts smoothly at any viewport size |
| 104 | +2. **Container-Aware**: Components respond to available space, not viewport |
| 105 | +3. **Better Maintainability**: Fewer media queries to manage |
| 106 | +4. **Progressive Enhancement**: Fallbacks for older browsers |
| 107 | +5. **Fluid Scaling**: Typography and spacing scale smoothly |
| 108 | +6. **Component Modularity**: Components work in any context |
| 109 | + |
| 110 | +## Browser Support |
| 111 | + |
| 112 | +- **Container Queries**: Supported in Chrome 105+, Safari 16+, Firefox 110+ |
| 113 | +- **Fallback**: Media queries for older browsers |
| 114 | +- **clamp()**: Widely supported (IE excluded) |
| 115 | +- **CSS Custom Properties**: All modern browsers |
| 116 | + |
| 117 | +## Development Guidelines |
| 118 | + |
| 119 | +### Adding New Components |
| 120 | + |
| 121 | +1. Start with intrinsic sizing (no media queries) |
| 122 | +2. Use fluid units (`clamp()`, `%`, `vw`, `vh`) |
| 123 | +3. Add container queries if component needs responsive behavior |
| 124 | +4. Provide media query fallback for older browsers |
| 125 | +5. Test at various sizes, not just common breakpoints |
| 126 | + |
| 127 | +### Updating Existing Components |
| 128 | + |
| 129 | +1. Identify hardcoded breakpoints |
| 130 | +2. Replace with fluid sizing where possible |
| 131 | +3. Use container queries for container-responsive behavior |
| 132 | +4. Keep media query fallback |
| 133 | +5. Test smooth transitions between sizes |
| 134 | + |
| 135 | +## Examples |
| 136 | + |
| 137 | +### Before (Fixed Breakpoints) |
| 138 | + |
| 139 | +```css |
| 140 | +.component { |
| 141 | + font-size: 14px; |
| 142 | +} |
| 143 | + |
| 144 | +@media screen and (min-width: 768px) { |
| 145 | + .component { |
| 146 | + font-size: 16px; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +@media screen and (min-width: 1024px) { |
| 151 | + .component { |
| 152 | + font-size: 18px; |
| 153 | + } |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +### After (Intrinsic) |
| 158 | + |
| 159 | +```css |
| 160 | +.component { |
| 161 | + font-size: clamp(0.875rem, 0.8rem + 0.5vw, 1.125rem); |
| 162 | + /* Smoothly scales from 14px to 18px */ |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +### Using Container Queries |
| 167 | + |
| 168 | +```css |
| 169 | +/* Make container queryable */ |
| 170 | +.parent { |
| 171 | + container-type: inline-size; |
| 172 | + container-name: my-component; |
| 173 | +} |
| 174 | + |
| 175 | +/* Respond to container size */ |
| 176 | +@container my-component (min-width: 40rem) { |
| 177 | + .child { |
| 178 | + display: grid; |
| 179 | + grid-template-columns: 1fr 1fr; |
| 180 | + } |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +## Resources |
| 185 | + |
| 186 | +- [MDN: CSS Container Queries](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Container_Queries) |
| 187 | +- [MDN: clamp()](https://developer.mozilla.org/en-US/docs/Web/CSS/clamp) |
| 188 | +- [Every Layout: The principles of intrinsic web design](https://every-layout.dev/) |
| 189 | +- [Utopia: Fluid Responsive Design](https://utopia.fyi/) |
| 190 | + |
| 191 | +## Migration Notes |
| 192 | + |
| 193 | +This refactoring maintains backward compatibility through: |
| 194 | +1. Fallback media queries for browsers without container query support |
| 195 | +2. Graceful degradation of fluid sizing |
| 196 | +3. No HTML changes required |
| 197 | +4. Same visual appearance across supported browsers |
| 198 | + |
| 199 | +The main difference users will notice is smoother responsive behavior without "jumps" at specific breakpoints. |
0 commit comments