Performance: Print stylesheet organization
This affects page load speed and rendering performance when users print a page or use print preview.
@import 'screen-styles'; // separate print stylesheet loaded only for print /* <link rel="stylesheet" href="print.css" media="print"> */
@import 'print-styles'; // print styles included in main stylesheet body { font-size: 1rem; } @media print { body { font-size: 0.8rem; color: black; } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Print styles inline in main CSS | No extra DOM nodes | Triggers 1 reflow on load | Increases paint cost due to larger CSS | [X] Bad |
| Separate print stylesheet with media="print" | No extra DOM nodes | No reflow on screen load | Paint cost only when printing | [OK] Good |
@media print { ... }.@media print correctly with a style inside.@media print {
p {
font-size: 14pt;
}
}
p {
font-size: 16px;
}@media print block applies only when printing, overriding other styles.@media print {
.hide-on-print {
display: none
}
}@media print { ... } to apply only when printing.nav with display: none; and sets font size on body inside print media query, which is correct.Pass the test and this pattern moves unknown