Colors make websites look nice and easy to understand. Using color values and changing them helps you create beautiful designs.
Color values and manipulation in SASS
$color-name: #rrggbb; // Manipulate colors lighten($color, 20%) darken($color, 15%) rgba($color, 0.5) mix($color1, $color2, 50%)
Colors can be written as hex codes like #ff0000 for red.
Functions like lighten() and darken() change brightness by a percentage.
$primary-color: #3498db; .button { background-color: $primary-color; }
.button:hover { background-color: lighten($primary-color, 20%); }
.alert { background-color: rgba(231, 76, 60, 0.7); }
.mixed-color { background-color: mix(#3498db, #e74c3c, 50%); }
This example shows a green button that becomes lighter when hovered. It also shows an alert box with semi-transparent red background and a box with a mix of green and blue colors.
@use 'sass:color'; $base-color: #2ecc71; .button { background-color: $base-color; color: white; padding: 1rem 2rem; border: none; border-radius: 0.5rem; cursor: pointer; transition: background-color 0.3s ease; } .button:hover { background-color: color.change($base-color, $lightness: 15%); } .alert { background-color: color.change(#e74c3c, $alpha: 0.6); color: white; padding: 1rem; border-radius: 0.5rem; margin-top: 1rem; max-width: 300px; } .mixed { background-color: color.mix(#2ecc71, #3498db, 40%); color: white; padding: 1rem; border-radius: 0.5rem; margin-top: 1rem; max-width: 300px; }
Use @use 'sass:color'; to access color functions in modern Sass.
Percentages in lighten/darken control how much brighter or darker the color becomes.
RGBA lets you add transparency to colors, useful for overlays or subtle effects.
Colors in Sass can be set using hex codes or named variables.
You can change colors easily with functions like lighten(), darken(), rgba(), and mix().
Manipulating colors helps create interactive and visually appealing web designs.
Practice
Solution
Step 1: Understand the purpose of lighten()
Thelighten()function increases the lightness of a color, making it lighter.Step 2: Compare with other functions
darken()makes colors darker,rgba()changes transparency, andmix()blends two colors.Final Answer:
lighten() -> Option CQuick Check:
Make color lighter = lighten() [OK]
- Confusing lighten() with darken()
- Using rgba() to change brightness
- Mixing colors instead of lightening
Solution
Step 1: Check function syntax
Thedarken()function takes two arguments separated by a comma: a color and a percentage without quotes.Step 2: Validate each option
darken(#ff0000, 20%) uses a comma and percentage without quotes, which is correct. Options B and C miss the comma or use wrong separators. darken(#ff0000, '20%') uses quotes around percentage, which is invalid.Final Answer:
darken(#ff0000, 20%) -> Option DQuick Check:
Correct syntax uses comma and percentage without quotes [OK]
- Omitting comma between arguments
- Putting percentage in quotes
- Using semicolon instead of comma
$base: #0000ff; $mix-color: #ff0000; $result: mix($base, $mix-color, 50%);
Solution
Step 1: Understand mix() function
Themix()function blends two colors by a given weight. 50% means equal parts of both colors.Step 2: Mix blue and red equally
Mixing pure blue (#0000ff) and pure red (#ff0000) equally results in purple (#800080).Final Answer:
#800080 (purple) -> Option BQuick Check:
Mix blue + red equally = purple [OK]
- Thinking mix returns original color
- Confusing olive with purple
- Ignoring the 50% weight in mix()
$color: lighten(#123456 10%);
Solution
Step 1: Check lighten() function syntax
Thelighten()function requires two arguments separated by a comma: a color and a percentage.Step 2: Identify missing comma
The code uses a space instead of a comma between#123456and10%, causing a syntax error.Final Answer:
Missing comma between color and percentage -> Option AQuick Check:
Arguments must be comma-separated [OK]
- Using space instead of comma between arguments
- Thinking percentage must be negative
- Believing lighten() can't take percentages
$primary: #0066cc; and add 50% transparency. Which Sass code correctly does this?Solution
Step 1: Lighten the base color first
Uselighten($primary, 30%)to increase brightness by 30%.Step 2: Add transparency with rgba()
Wrap the lightened color inrgba(..., 0.5)to set 50% opacity.Step 3: Check other options
$button-bg: lighten(rgba($primary, 0.5), 30); uses amount without % unit, which is invalid. $button-bg: lighten($primary, 30%, 0.5); uses wrong argument count. $button-bg: rgba($primary, lighten(30%, 0.5)); misuses rgba() arguments.Final Answer:
$button-bg: rgba(lighten($primary, 30%), 0.5); -> Option AQuick Check:
Lighten first, then add transparency [OK]
- Adding transparency before lightening
- Passing wrong number of arguments
- Misordering function calls
Ready to prove it?
Pass the test and this pattern moves unknown
