Media queries in CSS allow you to apply different styles based on the characteristics of the output device. For printing, we use the print
media type. This way, you can have specific styles that are only applied when the page is being printed.
In portrait printing mode, the typical page dimensions in common paper sizes like A4 (210mm x 297mm) or Letter (8.5 inches x 11 inches) have a taller height than width. You can use CSS to set margins and page breaks to optimize the content for these dimensions.
Controlling page breaks is essential to ensure that content is split appropriately across printed pages. CSS provides properties like page - break - before
, page - break - after
, and page - break - inside
to manage this.
To define styles that are only applied during printing, you can use a media query in your CSS. For example:
@media print {
/* All styles inside this block will be applied only when printing */
body {
font - size: 12pt;
}
}
You can set the margins for the printed page using the margin
property. This helps in providing some white space around the content.
@media print {
body {
margin: 2cm;
}
}
To control where page breaks occur, you can use the page - break - *
properties. For example, to force a page break before a particular section:
@media print {
.section - to - break {
page - break - before: always;
}
}
When printing, some elements that are useful for the web page’s interactive experience may not be needed. For example, navigation menus, sidebars, and buttons. You can hide these elements using the display: none
property.
@media print {
nav, button {
display: none;
}
}
On the screen, you might use a wide range of font sizes for visual appeal. But for printing, it’s better to use a standard and legible font size. A common choice is 12pt for body text.
@media print {
body {
font - size: 12pt;
}
}
Before finalizing your print styles, test them on different browsers and printers. Different browsers may interpret CSS slightly differently, and printers may have their own quirks.
While relative units like percentages and ems are great for the web, they can sometimes lead to inconsistent results when printing. It’s better to use absolute units like pt
(points) or mm
(millimeters) for font sizes and dimensions.
Ensure that page breaks occur at logical places in your content, such as between sections or after headings. This makes the printed document easier to read.
/* General styles for the web page */
body {
font - family: Arial, sans - serif;
font - size: 16px;
}
/* Print - specific styles */
@media print {
/* Hide unnecessary elements */
nav, button, footer {
display: none;
}
/* Set margins */
body {
margin: 2cm;
}
/* Set font size */
body {
font - size: 12pt;
}
/* Force page break before sections */
.section - to - break {
page - break - before: always;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device - width, initial - scale = 1.0">
<link rel="stylesheet" href="styles.css">
<title>Print - Ready HTML Page</title>
</head>
<body>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
</ul>
</nav>
<h1>My Article</h1>
<p>This is the content of my article...</p>
<div class="section - to - break">
<h2>New Section</h2>
<p>More content in the new section...</p>
</div>
<button>Click me</button>
<footer>
<p>© 2024 My Website</p>
</footer>
</body>
</html>
Setting up HTML pages for portrait printing mode with CSS is a valuable skill that can enhance the usability of your web content. By understanding the fundamental concepts of media queries, page dimensions, and page breaks, and following common and best practices, you can ensure that your pages print correctly and are easy to read. Always test your print styles thoroughly to account for browser and printer variations.