SASS helps write CSS faster and easier by adding useful features that plain CSS does not have.
Why SASS exists
Introduction
Syntax
SASS
$variable-name: value; .selector { property: value; @include mixin-name; @if condition { property: value; } }
SASS uses variables starting with $ to store values like colors or sizes.
You can use @include to reuse groups of styles called mixins.
Examples
SASS
$primary-color: #3498db; .button { background-color: $primary-color; color: white; }
SASS
@mixin rounded-corners { border-radius: 10px; } .box { @include rounded-corners; background: lightgray; }
SASS
$theme: dark; @if $theme == 'dark' { body { background: black; color: white; } }
Sample Program
This example shows how SASS variables can store colors and spacing to keep styles consistent and easy to update.
SASS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>SASS Example</title> <style> $main-color: #e67e22; $padding: 1rem; .card { background-color: $main-color; padding: $padding; border-radius: 0.5rem; color: white; font-family: Arial, sans-serif; } </style> </head> <body> <div class="card">This is a card with SASS variables.</div> </body> </html>
Important Notes
SASS code needs to be converted (compiled) into regular CSS before browsers can use it.
Using variables and mixins helps keep your styles consistent and easier to maintain.
Summary
SASS adds features like variables, mixins, and conditions to CSS.
It helps write cleaner, reusable, and easier-to-change styles.
SASS must be compiled into normal CSS for browsers to understand.
Practice
1. Why was SASS created to be used with CSS?
easy
Solution
Step 1: Understand CSS limitations
CSS alone cannot use variables or reusable blocks, making styles repetitive and hard to maintain.Step 2: Identify SASS purpose
SASS adds variables, mixins, and conditions to make CSS easier to write and change.Final Answer:
To add features like variables and reusable code to CSS -> Option DQuick Check:
SASS adds features = B [OK]
Hint: Remember SASS adds power to CSS, not replaces it [OK]
Common Mistakes:
- Thinking SASS replaces HTML or JavaScript
- Believing SASS runs directly in browsers
- Confusing SASS with a programming language
2. Which of the following is the correct way to declare a variable in SASS?
easy
Solution
Step 1: Recall SASS variable syntax
SASS variables start with a dollar sign ($) followed by the name and a colon.Step 2: Match syntax to options
$main-color: #333; uses $main-color: #333; which is correct SASS syntax.Final Answer:
$main-color: #333; -> Option CQuick Check:
Variables start with $ in SASS = A [OK]
Hint: Variables in SASS always start with $ [OK]
Common Mistakes:
- Using JavaScript or CSS variable syntax
- Omitting the $ sign
- Using invalid characters in variable names
3. What will be the output CSS of this SASS code?
$color: red;
.button {
color: $color;
&:hover {
color: blue;
}
}medium
Solution
Step 1: Replace variable with value
The variable $color is red, so color: $color; becomes color: red; in CSS.Step 2: Understand nesting and hover
The &:hover means .button:hover in CSS, so hover color is blue.Final Answer:
.button { color: red; } .button:hover { color: blue; } -> Option AQuick Check:
Variables replaced and nesting expanded = C [OK]
Hint: Variables replaced, nesting becomes combined selectors [OK]
Common Mistakes:
- Leaving variables as $color in output
- Misunderstanding &:hover nesting
- Mixing selector names in output
4. Identify the error in this SASS code:
$font-size: 16px
body {
font-size: $font-size;
}medium
Solution
Step 1: Check variable declaration syntax
In SASS, each statement must end with a semicolon; the variable declaration misses it.Step 2: Verify other parts
Variable name with hyphen is allowed, font-size is valid CSS, and body selector is valid.Final Answer:
Missing semicolon after variable declaration -> Option AQuick Check:
Semicolon missing = D [OK]
Hint: Always end SASS statements with semicolon [OK]
Common Mistakes:
- Forgetting semicolon after variables
- Thinking hyphens are invalid in variable names
- Assuming selectors are restricted
5. You want to create a reusable button style with SASS that changes color based on a variable. Which SASS feature helps you do this efficiently?
hard
Solution
Step 1: Understand the need for reusable styles
You want to reuse button styles with different colors easily.Step 2: Identify SASS feature for reusable code blocks
Mixins allow you to write reusable style blocks that accept variables for customization.Step 3: Differentiate from variables and nesting
Variables store values, nesting organizes selectors, but mixins combine both for reuse.Final Answer:
Mixins -> Option BQuick Check:
Reusable style blocks = Mixins = A [OK]
Hint: Use mixins for reusable styles with variable inputs [OK]
Common Mistakes:
- Confusing variables with reusable style blocks
- Thinking nesting creates reusable code
- Assuming functions style CSS directly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
