Performance: Lighten and darken functions
These functions affect CSS rendering by changing color values, impacting paint and composite stages during style updates.
$primary-color: #3498db; $primary-dark: #2a80b9; // precomputed darkened color $primary-darker: #1f5f82; // precomputed darker color .button { background-color: $primary-dark; border-color: $primary-darker; }
$primary-color: #3498db; .button { background-color: darken($primary-color, 20%); border-color: darken($primary-color, 30%); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Repeated lighten/darken calls in CSS | 0 | 0 | Increases paint cost due to larger CSS and style recalculations | [X] Bad |
| Precomputed color variables | 0 | 0 | Minimal paint cost with smaller CSS and simpler style calculations | [OK] Good |
lighten() function do in Sass?lighten() function increases the lightness of a color, making it appear lighter.lighten() does.darken() function takes two arguments: a color and a percentage, in that order, separated by a comma.darken(#3498db, 20%). Others have wrong order or missing comma/semicolon.$base-color: #800000;
$light-color: lighten($base-color, 50%);
body { background-color: $light-color; }$color: #123456;
$dark: darken($color 30%);
.button { background: $dark; }darken() function requires a comma between the color and percentage arguments.darken($color 30%) lacks a comma, causing a syntax error.#007BFF. On hover, the button should become 15% darker. Which Sass code correctly achieves this with variables and hover style?$btn-color is set to the base blue color #007BFF.$btn-color normally, and on hover uses darken($btn-color, 15%) to make it 15% darker.$btn-color: #007BFF;
.button {
background-color: $btn-color;
&:hover {
background-color: darken($btn-color, 15%);
}
} correctly darkens on hover. $btn-color: #007BFF;
.button {
background-color: darken($btn-color, 15%);
&:hover {
background-color: $btn-color;
}
} reverses colors. $btn-color: #007BFF;
.button {
background-color: lighten($btn-color, 15%);
&:hover {
background-color: darken($btn-color, 15%);
}
} lightens base color incorrectly. $btn-color: #007BFF;
.button {
background-color: $btn-color;
&:hover {
background-color: lighten($btn-color, 15%);
}
} lightens on hover instead of darkening.$btn-color: #007BFF;
.button {
background-color: $btn-color;
&:hover {
background-color: darken($btn-color, 15%);
}
} [OK]Pass the test and this pattern moves unknown