📖 Scenario: You are designing a simple webpage with colored boxes. You want to control the transparency of these boxes using RGBA colors and opacity in Sass.
🎯 Goal: Create a Sass stylesheet that defines base colors, sets opacity levels using RGBA, and applies these styles to boxes on the webpage.
📋 What You'll Learn
Define base colors as variables in Sass
Create opacity variables for different transparency levels
Use RGBA function to combine base colors with opacity
Apply the RGBA colors to CSS classes for colored boxes
💡 Why This Matters
🌍 Real World
Web designers often need to control transparency of colors for overlays, buttons, and backgrounds to create visually appealing layouts.
💼 Career
Understanding how to manipulate RGBA colors and opacity in Sass is essential for front-end developers and UI designers to build modern, responsive, and accessible web interfaces.
Progress0 / 4 steps
1
Define base color variables
Create two Sass variables called $primary-color and $secondary-color with the exact hex values #3498db and #e74c3c respectively.
SASS
Hint
Use the syntax $variable-name: value; to define variables in Sass.
2
Create opacity variables
Add two Sass variables called $opacity-light and $opacity-heavy with the exact decimal values 0.3 and 0.7 respectively.
SASS
Hint
Opacity values are decimals between 0 (transparent) and 1 (opaque).
3
Create RGBA color variables using opacity
Create two new Sass variables called $primary-color-light and $secondary-color-heavy. Use the rgba() function to combine $primary-color with $opacity-light, and $secondary-color with $opacity-heavy respectively.
SASS
Hint
The rgba() function takes a color and an opacity value to create a transparent color.
4
Apply RGBA colors to CSS classes
Write CSS rules for classes .box-primary and .box-secondary that set the background-color property to $primary-color-light and $secondary-color-heavy respectively.
SASS
Hint
Use the variable names exactly as given to set the background colors.
Practice
(1/5)
1. What does the rgba(255, 0, 0, 0.5) color value represent in Sass?
easy
A. A semi-transparent red color
B. A fully opaque blue color
C. A transparent green color
D. A solid black color
Solution
Step 1: Understand RGBA components
The first three numbers (255, 0, 0) represent red, green, and blue. Here, red is full (255), green and blue are zero.
Step 2: Interpret the alpha value
The last number (0.5) is the alpha channel, which controls transparency. 0.5 means 50% transparent.
Final Answer:
A semi-transparent red color -> Option A
Quick Check:
RGBA = red + transparency [OK]
Hint: RGBA's last value controls transparency from 0 to 1 [OK]
Common Mistakes:
Confusing alpha with opacity property
Mixing up RGB values order
Thinking 0.5 means fully opaque
2. Which of the following is the correct Sass syntax to set a background color with 30% opacity using RGBA?
easy
A. background-color: rgba(0, 0, 255, 3);
B. background-color: rgba(0, 0, 255, 0.3);
C. background-color: rgba(0, 0, 255, 30);
D. background-color: rgba(0, 0, 255, '0.3');
Solution
Step 1: Check alpha value range
Alpha must be a decimal between 0 and 1. 0.3 means 30% opacity.
Step 2: Validate syntax correctness
Values must be numbers without quotes. So rgba(0, 0, 255, 0.3) is correct.
Final Answer:
background-color: rgba(0, 0, 255, 0.3); -> Option B
Quick Check:
Alpha decimal between 0 and 1 [OK]
Hint: Alpha must be decimal 0-1 without quotes [OK]