Introduction
Print stylesheets optimize web pages for printing, ensuring readability, removing unnecessary elements, and improving paper usage efficiency.
1. Creating a Print Stylesheet
Use the @media print
query to define styles specific to printing.
@media print {
body {
font-size: 12pt;
color: black;
background: none;
}
}
CSSLinking an External Print Stylesheet
<link rel="stylesheet" href="print.css" media="print">
HTML2. Hiding Unnecessary Elements
Elements like navigation, ads, and footers are unnecessary in print.
@media print {
nav, .sidebar, .ads {
display: none;
}
}
CSS3. Optimizing Text and Layout
- Use readable fonts like
serif
for better legibility. - Set appropriate margins using
@page
.
@page {
margin: 1in;
}
CSS- Force page breaks where needed.
h1 {
page-break-before: always;
}
CSS4. Handling Links and Colors
Convert links to text for better clarity in print.
a::after {
content: " (" attr(href) ")";
}
CSSRemove background colors and images.
* {
background: none !important;
color: black !important;
}
CSSConclusion
Using print stylesheets ensures a clean, professional print layout, enhancing usability for users who need hard copies.