📖 Scenario: You are creating a website that looks great on screen and also prints nicely on paper. To do this, you will organize your Sass code to separate screen styles from print styles. This helps keep your styles clean and easy to manage.
🎯 Goal: Build a Sass file that defines screen styles and print styles separately using @media queries. You will create variables for colors, write styles for the body and headings for screen, then add print-specific styles that hide navigation and change text color for printing.
📋 What You'll Learn
Create a Sass variable $primary-color with the value #3498db.
Write styles for body and h1 for screen display using @media screen.
Write print-specific styles inside @media print that hide nav and set body text color to black.
Organize the Sass code clearly separating screen and print styles.
💡 Why This Matters
🌍 Real World
Websites often need to look good on screen and print well on paper. Organizing styles with media queries helps maintain clean code and better user experience.
💼 Career
Front-end developers must write maintainable CSS/Sass that supports multiple devices and media, including print. This skill is essential for professional web development.
Progress0 / 4 steps
1
Create a Sass variable for the primary color
Create a Sass variable called $primary-color and set it to #3498db.
SASS
Hint
Use the syntax $variable-name: value; to create a variable in Sass.
2
Add screen styles using @media screen
Write a @media screen block. Inside it, style the body with font-family: Arial, sans-serif; and set the h1 color to $primary-color.
SASS
Hint
Use @media screen { ... } to target screen styles.
3
Add print styles using @media print
Write a @media print block. Inside it, hide the nav element by setting display: none; and set the body text color to black.
SASS
Hint
Use @media print { ... } to write styles that apply only when printing.
4
Organize and finalize the print stylesheet
Ensure your Sass code clearly separates screen and print styles by having two distinct @media blocks: one for screen and one for print. Confirm the variable $primary-color is declared at the top.
SASS
Hint
Check that your variable is at the top and that screen and print styles are in separate @media blocks.
Practice
(1/5)
1. What is the main purpose of a print stylesheet in web development?
easy
A. To make the webpage look good when printed on paper
B. To add animations to the webpage
C. To improve website loading speed
D. To change the webpage's background color on screen
Solution
Step 1: Understand what print stylesheets do
Print stylesheets adjust the webpage's appearance specifically for printing on paper.
Step 2: Compare options to the purpose
Only To make the webpage look good when printed on paper matches the purpose of print stylesheets, which is to make pages look good on paper.
Final Answer:
To make the webpage look good when printed on paper -> Option A
4. Identify the error in this Sass print stylesheet code:
@media print {
.hide-on-print {
display: none
}
}
medium
A. Cannot use 'display: none' in print styles
B. Wrong media type, should be '@media screen'
C. Missing semicolon after 'display: none'
D. Class selector should start with '#' instead of '.'
Solution
Step 1: Check CSS syntax inside '@media print'
Every CSS property must end with a semicolon; here, 'display: none' misses it.
Step 2: Verify other options
Media type '@media print' is correct, class selectors use '.', and 'display: none' is valid in print styles.
Final Answer:
Missing semicolon after 'display: none' -> Option C
Quick Check:
CSS properties need semicolons [OK]
Hint: Always end CSS declarations with semicolons [OK]
Common Mistakes:
Omitting semicolons in CSS blocks
Confusing class selectors with ID selectors
Using wrong media types for print styles
5. You want to hide navigation menus and change font size only when printing. Which Sass structure correctly organizes these print styles?
hard
A. nav { display: none; }
body { font-size: 12pt; }
B. @media print {
nav { display: none; }
body { font-size: 12pt; }
}
C. @media screen {
nav { display: none; }
body { font-size: 12pt; }
}
D. @media print {
nav { font-size: 12pt; }
body { display: none; }
}
Solution
Step 1: Identify correct media query for print styles
Print styles must be inside @media print { ... } to apply only when printing.
Step 2: Check style rules for hiding nav and changing font size
@media print {
nav { display: none; }
body { font-size: 12pt; }
} hides nav with display: none; and sets font size on body inside print media query, which is correct.
Final Answer:
@media print {
nav { display: none; }
body { font-size: 12pt; }
} -> Option B
Quick Check:
Print styles inside '@media print' with correct selectors [OK]
Hint: Group all print styles inside '@media print' block [OK]
Common Mistakes:
Placing print styles outside '@media print'
Using '@media screen' instead of print
Mixing display and font-size on wrong selectors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.