Logic in stylesheets helps make designs flexible and easier to manage. It lets you change styles based on conditions or reuse code without repeating yourself.
When you want to change colors or sizes based on a theme or user choice.
When you need to create responsive designs that adjust to screen size.
When you want to reuse style patterns with small changes.
When you want to avoid repeating the same style code multiple times.
When you want to organize your styles better for large projects.
Syntax
SASS
// Example of logic in Sass
$theme: dark;
body {
@if $theme == dark {
background-color: black;
color: white;
} @else {
background-color: white;
color: black;
}
}
Sass lets you use variables to store values like colors or sizes.
You can use @if and @else to add conditions in your styles.
Examples
This example changes the container background based on the mode variable.
Using a variable to set text color makes it easy to update later.
SASS
$color: blue;
.text {
color: $color;
}
Sample Program
This Sass code uses logic to change the background and text colors based on the theme variable. It also adjusts the button color for dark mode and adds a hover effect.
$is-primary is set to true, so the @if condition is true.
Step 2: Determine which block runs
Since the condition is true, background-color: blue; is applied to button.
Final Answer:
Blue -> Option A
Quick Check:
@if true applies first block = Blue [OK]
Hint: True condition runs @if block [OK]
Common Mistakes:
Choosing else block color
Ignoring the variable value
Thinking no color applies
4. Identify the error in this Sass code snippet:
$font-size: 16px
body {
font-size: $font-size;
}
medium
A. Variable name should not start with $
B. Missing semicolon after variable declaration
C. font-size property is invalid
D. body selector is incorrect
Solution
Step 1: Check variable declaration syntax
In Sass, each statement must end with a semicolon; here it is missing after 16px.
Step 2: Verify other parts
Variable name and usage are correct; body selector is valid.
Final Answer:
Missing semicolon after variable declaration -> Option B
Quick Check:
Every Sass statement ends with ; [OK]
Hint: Always end Sass lines with semicolon [OK]
Common Mistakes:
Forgetting semicolon after variable
Removing $ from variable name
Misunderstanding CSS property syntax
5. You want to create a theme system in Sass that changes button colors based on a variable $theme. Which approach best uses logic to handle multiple themes?
hard
A. Use JavaScript to change button colors only
B. Set background color directly without conditions
C. Write separate CSS files for each theme without variables