Paged Media in CSS Language

Paged Media in CSS Language

Cascading Style Sheets (CSS) is a powerful language that web developers and designers utilize to control the presentation and layout of

web content. While CSS is predominantly associated with the web, it is a versatile tool that extends its capabilities to various media types, including print. This adaptation of CSS for printed documents is known as “Paged Media CSS.” In this article, we’ll explore the concept of paged media in CSS and provide examples of how it can be applied.

What is Paged Media CSS?

Paged Media CSS allows you to style content for printed materials, such as books, brochures, and reports. Unlike traditional web design, where content flows continuously, printed materials are divided into discrete pages. Paged Media CSS facilitates the creation of paginated layouts, controlling aspects like page size, margins, headers, footers, and even controlling the flow of content between pages.

Key Features of Paged Media CSS

  1. Page Size and Margins: You can define the size and margins of printed pages using properties like size and margin. For example, you can specify that pages should be A4-sized with a 1-inch margin all around.
   @page {
     size: A4;
     margin: 1in;
   }
  1. Page Breaks: Paged Media CSS allows you to control page breaks, ensuring that content flows correctly from one page to the next. You can use properties like page-break-before and page-break-after to control page breaks.
   h1 {
     page-break-before: always;
   }
  1. Headers and Footers: You can add headers and footers to your printed pages using @page and content properties. For instance, you can include page numbers or document titles in the headers and footers.
   @page {
     @top-center {
       content: "My Document Title";
     }
     @bottom-right {
       content: "Page " counter(page);
     }
   }

Practical Example: Creating a Printable Report

Imagine you have a web-based application that generates reports, and you want users to be able to print those reports. Using Paged Media CSS, you can ensure that the reports are well-formatted for printing. Here’s a simplified example:

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
  <h1>Monthly Sales Report</h1>
  <p>Report content goes here...</p>
  <!-- More content -->
</body>
</html>
/* styles.css */
@page {
  size: A4;
  margin: 1in;
}

h1 {
  page-break-before: always;
}

@page {
  @top-center {
    content: "Monthly Sales Report";
  }
  @bottom-right {
    content: "Page " counter(page);
  }
}

With this CSS, you ensure that the report starts on a new page for each section and includes a title in the header and page numbers in the footer when printed.


Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading