πŸ“„ Mastering the @page Rule in CSS

πŸ“Œ What Is @page?

The @page at-rule allows you to control the page boxwhen printing a webpage. It lets you style margins, size, orientation, and define different styles for left/right pages or first pages.
πŸ–¨οΈ In short: it’s CSS for printed documents.

>>β€œIf your website needs to print well, @page is essential.”

🎯 What You Can Style with @page

  • Page size (A4, Letter, custom dimensions)
  • Margins
  • Orientation (portrait/landscape)
  • Different rules for first, left, or right pages
  • Page bleed, marks (limited browser support)

🧩 Basic Syntax

Basic @page Rule

@page {
  margin: 20mm;
}

This sets the default margin for printed pages to 20mm.

πŸ“ Setting Page Size

Set Page Size

@page {
  size: A4;
}

Custom Size

@page {
  size: 8.5in 11in; /* width height */
}

βœ” Useful for invoices, tickets, resumes, certificates, and printable layouts.

πŸ”„ Orientation (Portrait / Landscape)

Landscape Orientation

@page {
  size: A4 landscape;
}

🎴 Styling Page Margins

Set Margins

@page {
  margin: 10mm 20mm;
}

βœ” Margins affect the entire printed page layout βœ” You can’t style inside the margin directly β€” only margin boxes (limited support)

πŸ“„ Special Pseudo-Classes for @page

@page supports special pseudo-classes allowing different styling for specific pages.

1️⃣ First Page

First Page Styling

@page :first {
  margin-top: 40mm;
}

2️⃣ Left Page

Left Page Styling

@page :left {
  margin-left: 30mm;
}

3️⃣ Right Page

Right Page Styling

@page :right {
  margin-right: 30mm;
}

πŸ’‘ Useful for book-style printing where left/right pages differ.

πŸ›‘ What You CANNOT Style in @page

  • CSS layout inside page content (use normal CSS instead)
  • Background images unless browser print settings allow it
  • Most CSS properties (only margin, size, marks, bleed allowed)
  • Selectors β€” only @page + pseudo-classes work

Note

⚠️ Support for advanced print features varies across browsers. Chrome and Firefox generally support the basics well.

πŸ“˜ Real-World Example: Invoice Printing

Invoice Print Layout

/* Print styles */
@media print {
  body {
    font-family: sans-serif;
  }

  @page {
    size: A4 portrait;
    margin: 15mm;
  }

  @page :first {
    margin-top: 30mm;
  }

  .no-print {
    display: none !important;
  }
}

βœ” Hides UI elements
βœ” Adjusts page margins
βœ” Ensures the first page has extra space for branding

πŸ›οΈ Using @page Inside @media print

Common Structure

@media print {
  @page {
    size: letter;
    margin: 20mm;
  }

  h1 {
    font-size: 24pt;
  }
}

πŸ” While @page can exist outside @media print, it’s generally best practice to keep it inside print media queries.

πŸ“ Best Practices

  • Use @page for print-specific layout only
  • Always test printed output in multiple browsers
  • Don’t rely on advanced print features β€” browser support varies
  • Combine with @media print for full control
  • Use millimeters (mm) for high precision

πŸ”— Helpful Resources

>>β€œWeb pages are interactive, but print is permanent β€” design your printed pages intentionally.” πŸ“„βœ¨