Print stylesheets help make web pages look good when printed on paper. Organizing them well keeps your code clean and easy to update.
Print stylesheet organization in SASS
@media print { // print-specific styles here } // Or create a separate print stylesheet and link it with: // <link rel="stylesheet" href="print.css" media="print">
The @media print block applies styles only when printing.
You can also use a separate CSS file linked with media="print" for print styles.
@media print { body { font-size: 12pt; color: black; background: none; } }
@media print { nav, .button { display: none; } }
/* In a separate file print.scss */ body { font-family: serif; line-height: 1.4; } @media print { h1 { page-break-before: always; } }
This example shows a webpage with a navigation bar and a button. When printed, the background becomes white, text turns black, and navigation and button are hidden. The heading starts on a new printed page.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Print Stylesheet Example</title> <style> body { font-family: Arial, sans-serif; background-color: #f0f0f0; color: #333; padding: 1rem; } nav { background-color: #007acc; color: white; padding: 0.5rem; margin-bottom: 1rem; } button { background-color: #007acc; color: white; border: none; padding: 0.5rem 1rem; cursor: pointer; } @media print { body { background: white; color: black; font-size: 12pt; padding: 0; } nav, button { display: none; } h1 { page-break-before: always; } } </style> </head> <body> <nav>Site Navigation</nav> <h1>Welcome to the Print Stylesheet Demo</h1> <p>This paragraph will appear in black text on white background when printed.</p> <button>Click Me</button> </body> </html>
Always test print styles by using your browser's print preview.
Keep print styles simple for faster printing and better readability.
Use page-break-before and page-break-after to control page breaks.
Print stylesheets make your webpage look good on paper.
Use @media print or separate print CSS files to organize print styles.
Hide unnecessary elements and adjust fonts and layout for printing.
Practice
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 AQuick Check:
Print stylesheets = good print appearance [OK]
- Confusing print stylesheets with screen stylesheets
- Thinking print stylesheets add animations
- Assuming print stylesheets speed up loading
Solution
Step 1: Identify the print media query syntax
The correct syntax for print styles is@media print { ... }.Step 2: Match the option with print media query
@media print { body { font-size: 12pt; } } uses@media printcorrectly with a style inside.Final Answer:
@media print { body { font-size: 12pt; } } -> Option AQuick Check:
Print media query syntax = @media print [OK]
- Using '@media screen' instead of '@media print'
- Forgetting curly braces after '@media print'
- Using unsupported media types like 'handheld'
@media print {
p {
font-size: 14pt;
}
}
p {
font-size: 16px;
}Solution
Step 1: Understand media query effect on print
The@media printblock applies only when printing, overriding other styles.Step 2: Compare font sizes inside and outside print query
Outside print, paragraphs have 16px font size; inside print, 14pt is set, which applies when printing.Final Answer:
14pt -> Option DQuick Check:
Print font size = 14pt [OK]
- Confusing px and pt units
- Ignoring media query overrides
- Assuming screen styles apply when printing
@media print {
.hide-on-print {
display: none
}
}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 CQuick Check:
CSS properties need semicolons [OK]
- Omitting semicolons in CSS blocks
- Confusing class selectors with ID selectors
- Using wrong media types for print styles
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; } } hidesnavwithdisplay: none;and sets font size onbodyinside print media query, which is correct.Final Answer:
@media print { nav { display: none; } body { font-size: 12pt; } } -> Option BQuick Check:
Print styles inside '@media print' with correct selectors [OK]
- Placing print styles outside '@media print'
- Using '@media screen' instead of print
- Mixing display and font-size on wrong selectors
Ready to prove it?
Pass the test and this pattern moves unknown
