Welcome to our comprehensive guide on CSS Print Styling! In this lesson, we'll explore how to customize your web page's print layout using CSS. By the end of this tutorial, you'll be able to create professional, well-formatted printouts that match your web designs. 🎯
When you design a web page, it's essential to consider the user experience for both online and offline viewing. Print Styling allows you to customize the appearance of your web content when it's printed, ensuring a clean and readable layout for your users. 📄
To apply CSS styles specifically for printing, we use the @media print rule. This rule contains CSS properties that only apply when the page is being printed.
@media print {
/* Your print-specific styles here */
}@media print 💡The @media print media query allows you to apply different styles based on the device or output type. In this case, we're focusing on the print output.
@media print {
body {
background-color: white;
color: black;
}
}In the example above, we're setting the background color to white and the text color to black when the page is printed.
Using CSS, you can control the page size, orientation, and margins for your printout.
@media print {
@page {
size: landscape; /* A4, A3, etc. */
margin: 0; /* Customize margins */
}
}Control where page breaks occur using the page-break-before and page-break-after properties.
@media print {
section {
page-break-before: always;
}
}When working with floated elements, don't forget to clear your floats for proper printing.
.clearfix::after {
content: "";
display: block;
clear: both;
}By applying the concepts learned above, we can create a printable recipe card that includes a recipe image, title, instructions, and ingredients. 🍴
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Printable Recipe Card</title>
<style>
/* CSS here */
</style>
</head>
<body>
<div class="recipe">
<img src="recipe-image.jpg" alt="Recipe Image">
<h1>Recipe Title</h1>
<h2>Ingredients</h2>
<ul>
<!-- List ingredients here -->
</ul>
<h2>Instructions</h2>
<ol>
<!-- List instructions here -->
</ol>
</div>
</body>
</html>What media query do we use for print-specific CSS styles?
What does the `page-break-before` property do?