|
| 1 | +export const metadata = { |
| 2 | + title: 'Optimize CSS', |
| 3 | + alternates: { |
| 4 | + canonical: '/docs/core-concepts/optimize-css', |
| 5 | + }, |
| 6 | +} |
| 7 | + |
| 8 | +# Optimize CSS |
| 9 | + |
| 10 | +Devup UI automatically optimizes CSS output through micro-unit optimization, duplicate removal, and intelligent value conversion to minimize bundle size and improve performance. This document explains the various CSS optimization techniques used by Devup UI, including micro-unit optimization, color optimization, and global CSS handling. |
| 11 | + |
| 12 | +## Micro-unit Optimization |
| 13 | + |
| 14 | +### **Value Optimization** |
| 15 | + |
| 16 | +Devup UI optimizes CSS values by: |
| 17 | + |
| 18 | +- **Removing unnecessary zeros**: `0px` becomes `0` |
| 19 | +- **Shortening decimals**: `0.5000` becomes `0.5` |
| 20 | +- **Optimizing units**: `0px` becomes `0` when appropriate |
| 21 | +- **Converting to smallest units**: `px` values are converted to `%` when more appropriate |
| 22 | +- **Removing redundant spaces**: `margin: 0 0 0 0` becomes `margin:0` |
| 23 | + |
| 24 | +``` |
| 25 | +// Before optimization |
| 26 | +<Box |
| 27 | + margin="0px 0px 0px 0px" |
| 28 | + padding="16.0000px" |
| 29 | + width="0px" |
| 30 | +/> |
| 31 | +
|
| 32 | +// After optimization |
| 33 | +<Box |
| 34 | + margin="0" |
| 35 | + padding="16px" |
| 36 | + width="0" |
| 37 | +/> |
| 38 | +``` |
| 39 | + |
| 40 | +### **Aspect Ratio Optimization** |
| 41 | + |
| 42 | +Aspect ratios are simplified to their smallest equivalent ratio using the greatest common divisor (GCD). |
| 43 | +For example, 500/100 becomes 5/1, while 16/9 remains unchanged since GCD(16, 9) = 1. |
| 44 | + |
| 45 | +```tsx |
| 46 | +// Before: aspect-ratio: 500/100 |
| 47 | +<Box aspectRatio="500/100" /> |
| 48 | + |
| 49 | +// After: aspect-ratio: 5/1 (optimized) |
| 50 | +// The system calculates GCD(500, 100) = 100, simplifying the ratio to 5/1 |
| 51 | +``` |
| 52 | + |
| 53 | +## Color Optimization |
| 54 | + |
| 55 | +### **RGB/RGBA to Hex Conversion** |
| 56 | + |
| 57 | +All RGB and RGBA color values are normalized and converted into compact hexadecimal form. |
| 58 | + |
| 59 | +``` |
| 60 | +// Input |
| 61 | +<Box bg="rgb(255, 0, 0)" /> |
| 62 | +<Box bg="rgba(255, 0, 0, 0.5)" /> |
| 63 | +
|
| 64 | +// Output |
| 65 | +<Box className="bg-red" /> // #f00 |
| 66 | +<Box className="bg-red-50" /> // #ff000080 |
| 67 | +``` |
| 68 | + |
| 69 | +### **Hex Shortening** |
| 70 | + |
| 71 | +Hex shortening converts long hex codes into their shortest possible form. |
| 72 | +Six- or eight-digit values are reduced to three or four digits when safely equivalent. |
| 73 | + |
| 74 | +```tsx |
| 75 | +// 6-digit hex to 3-digit |
| 76 | +// #ffffff → #fff |
| 77 | +// #000000 → #000 |
| 78 | +// #ff0000 → #f00 |
| 79 | + |
| 80 | +// 8-digit hex to 4-digit (when alpha is FF) |
| 81 | +// #ffffffff → #ffff |
| 82 | +// #000000ff → #000f |
| 83 | +``` |
| 84 | + |
| 85 | +### **Alpha Channel Optimization** |
| 86 | + |
| 87 | +Alpha channel optimization removes unnecessary opacity information. |
| 88 | +Fully opaque colors (FF) are shortened, while partial transparency values are preserved for accuracy. |
| 89 | + |
| 90 | +```tsx |
| 91 | +// Full opacity is removed |
| 92 | +// #ff0000ff → #ff0000 |
| 93 | + |
| 94 | +// Partial opacity is preserved |
| 95 | +// #ff000080 → #ff000080 |
| 96 | +``` |
| 97 | + |
| 98 | +## Global CSS Optimization |
| 99 | + |
| 100 | +### **CSS Block Optimization** |
| 101 | + |
| 102 | +Global CSS blocks are optimized by: |
| 103 | + |
| 104 | +- **Removing comments**: `/* comment */` is stripped |
| 105 | +- **Minifying whitespace**: Multiple spaces become single spaces |
| 106 | +- **Removing semicolons**: Last property in a block doesn't need semicolon |
| 107 | +- **Optimizing selectors**: Redundant selectors are merged |
| 108 | + |
| 109 | +```css |
| 110 | +/* Before optimization */ |
| 111 | +div { |
| 112 | + /* comment */ |
| 113 | + background-color: red; |
| 114 | + /* color: blue; */ |
| 115 | +} |
| 116 | + |
| 117 | +/* After optimization */ |
| 118 | +div { |
| 119 | + background-color: red; |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +### **Keyframes Optimization** |
| 124 | + |
| 125 | +Keyframes are optimized for minimal output: |
| 126 | + |
| 127 | +```tsx |
| 128 | +// Input |
| 129 | +const fadeIn = keyframes({ |
| 130 | + from: { opacity: 0 }, |
| 131 | + to: { opacity: 1 }, |
| 132 | +}) |
| 133 | + |
| 134 | +// Output (optimized) |
| 135 | +// @keyframes k-fadeIn{from{opacity:0}to{opacity:1}} |
| 136 | +``` |
| 137 | + |
| 138 | +### **Font Face Optimization** |
| 139 | + |
| 140 | +Font faces are automatically added and optimized: |
| 141 | + |
| 142 | +```tsx |
| 143 | +// Input |
| 144 | +<Box fontFamily="Inter" /> |
| 145 | + |
| 146 | +// Output (optimized) |
| 147 | +// @font-face{font-family:Inter;src:url(./fonts/Inter.woff2)} |
| 148 | +``` |
| 149 | + |
| 150 | +## Value Conversion |
| 151 | + |
| 152 | +### **Unit Conversion** |
| 153 | + |
| 154 | +Values are converted to their most appropriate units: |
| 155 | +Numeric values are automatically interpreted as pixel units, while string-based values with explicit units (e.g., rem, %) are preserved for accurate rendering. |
| 156 | + |
| 157 | +``` |
| 158 | +// Numbers without units are treated as pixels |
| 159 | +<Box w={16} /> // width: 16px |
| 160 | +
|
| 161 | +// Strings with units are preserved |
| 162 | +<Box w="16rem" /> // width: 16rem |
| 163 | +<Box w="100%" /> // width: 100% |
| 164 | +``` |
| 165 | + |
| 166 | +### **Mathematical Optimization** |
| 167 | + |
| 168 | +Mathematical expressions are optimized to their most efficient form: |
| 169 | + |
| 170 | +``` |
| 171 | +// Before |
| 172 | +<Box w="calc(100% - 32px)" /> |
| 173 | +<Box w="calc(50% + 16px)" /> |
| 174 | +
|
| 175 | +// After (optimized) |
| 176 | +<Box w="calc(100% - 2rem)" /> // px converted to rem for better scalability |
| 177 | +<Box w="calc(50% + 1rem)" /> // px converted to rem for better scalability |
| 178 | +``` |
| 179 | + |
| 180 | +### **Zero Value Optimization** |
| 181 | + |
| 182 | +Zero values are optimized: |
| 183 | + |
| 184 | +``` |
| 185 | +// Before |
| 186 | +<Box margin="0px 0px 0px 0px" /> |
| 187 | +
|
| 188 | +// After |
| 189 | +<Box margin="0" /> |
| 190 | +``` |
| 191 | + |
| 192 | +## Duplicate Removal |
| 193 | + |
| 194 | +### **Style Deduplication** |
| 195 | + |
| 196 | +Identical style rules across multiple components are automatically merged into a single CSS rule, reducing redundancy and overall bundle size. |
| 197 | + |
| 198 | +```tsx |
| 199 | +// Multiple components with same styles |
| 200 | +<div> |
| 201 | + <Box bg="red" color="white" /> |
| 202 | + <Box bg="red" color="white" /> |
| 203 | + <Box bg="red" color="white" /> |
| 204 | +</div> |
| 205 | + |
| 206 | +// Output: Only one CSS rule generated |
| 207 | +// .red-white{background:red;color:white} |
| 208 | +``` |
| 209 | + |
| 210 | +### **Property Deduplication** |
| 211 | + |
| 212 | +Duplicate CSS properties within the same rule are automatically removed, keeping only the last valid declaration for consistency and cleaner output. |
| 213 | + |
| 214 | +``` |
| 215 | +// Input |
| 216 | +<Box |
| 217 | + style={{ |
| 218 | + margin: '0', |
| 219 | + padding: '0', |
| 220 | + margin: '0', // duplicate |
| 221 | + }} |
| 222 | +/> |
| 223 | +
|
| 224 | +// Output: Duplicate margin removed |
| 225 | +<Box style={{ margin: '0', padding: '0' }} /> |
| 226 | +``` |
| 227 | + |
| 228 | +### **Advantages** |
| 229 | + |
| 230 | +Devup UI offers complete flexibility in how you implement and optimize styles — while maintaining measurable performance gains. |
| 231 | + |
| 232 | +- **Flexible syntax support** – Use CSS utility objects, template literals, or prop-based expressions seamlessly in one system |
| 233 | +- **Smaller bundle size** – Reduced CSS output through color, value, and duplicate optimization |
| 234 | +- **Faster parsing** – Lightweight CSS loads and parses quickly in browsers |
| 235 | +- **Better caching** – Compact, consistent values improve cache efficiency |
| 236 | +- **Lower memory usage** – Reduced CSS footprint minimizes runtime memory consumption |
| 237 | + |
| 238 | +This unified approach ensures both **maximum developer freedom** and **optimized performance**, |
| 239 | +allowing teams to write CSS their way without compromising efficiency. |
0 commit comments