Performance: Min and max functions
Min and max functions affect layout calculation and can impact rendering speed when used extensively or with complex expressions.
Jump into concepts and practice - no test required
width: min(100vw, 600px); /* width never exceeds 600px */
width: 100vw; /* always full viewport width, no limits */
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Fixed width without min/max | Low | Multiple on resize | Medium | [X] Bad |
| Simple min() or max() usage | Low | Single on resize | Low | [OK] Good |
| Nested complex min/max | Medium | Multiple | Medium | [!] OK |
min() do when used in a style rule?min()min() function compares all values inside it and picks the smallest one.max() which picks the largest, min() picks the smallest value.min() = smallest value [OK]max() function in CSS?max(100px, 50%).width: min(300px, max(50%, 200px));max() functionmax(50%, 200px) compares 50% of 400px (which is 200px) and 200px. Both are equal, so result is 200px.min() functionmin(300px, 200px) picks the smaller value, which is 200px.height: min(100px max(50%, 150px));min() argumentsmin() must be separated by commas. Here, 100px and max(50%, 150px) are missing a comma.max() inside min() is allowed. Mixing units like px and % is valid in these functions.min() and max() to achieve this?max(150px, min(40vw, 100%)) means: pick the larger between 150px and the smaller of 40vw or 100%. This ensures width is at least 150px but no more than 40vw.