LEARNING OBJECTIVES โต
- Define physical sheet dimensions and orientations using the
@pagesizeproperty. - Configure precise physical margin boxes using millimeters (
mm), inches (in), and typographical points (pt). - Target specific pages dynamically using pseudo-classes:
@page :first,@page :left, and@page :right. - Apply named pages (
page: landscape-report;) to switch individual sections between portrait and landscape orientations.
๐ The Mental Model & Story (Intuitive Foundation)
Imagine a master bookbinder crafting a collector's hardcover edition of an encyclopedia. The bookbinder does not treat every sheet of paper identically:
- The Cover Page has zero margin on the outer edges for full bleed artwork and no header text.
- The Left Pages (Verso) have extra margin on their right side (the spine gutter) so words don't get sucked into the binding seam when the book is opened.
- The Right Pages (Recto) have extra margin on their left side for that same gutter.
- A wide 16-column spreadsheet in Chapter 4 needs the binder to physically rotate the sheet 90 degrees into Landscape orientation without rotating the rest of the book.
In the CSS Paged Media Module, the @page rule is your digital bookbinder. It operates above the <html> and <body> elements, controlling the physical boundaries of the paper itself.
+-----------------------------------------------------------------------------------+
| THE CSS PAGE BOX |
| |
| +-----------------------------------------------------------------------------+ |
| | @top-center margin box | |
| +-----------------------------------------------------------------------------+ |
| | @top-left | | @top-right | |
| |-------------+-----------------------------------------------+---------------| |
| | | | | |
| | Left | PAGE AREA | Right | |
| | Margin | (HTML/Body Content) | Margin | |
| | | | | |
| |-------------+-----------------------------------------------+---------------| |
| | @bottom-l | | @bottom-r | |
| +-----------------------------------------------------------------------------+ |
| | @bottom-center margin box | |
| +-----------------------------------------------------------------------------+ |
+-----------------------------------------------------------------------------------+
Technical Deep Dive & Specifications
1. The @page Rule Syntax & Dimensions
The @page at-rule establishes properties for the page box. It accepts dimensions, orientation keywords, and margin values:
@page {
/* Standard paper size keyword + optional orientation */
size: A4 portrait; /* or 'letter landscape', 'legal', etc. */
/* Exact physical dimensions */
/* size: 210mm 297mm; */
/* size: 8.5in 11in; */
/* Page margins */
margin-top: 20mm;
margin-right: 15mm;
margin-bottom: 20mm;
margin-left: 15mm;
}
Standard Paper Size Reference Table
| Keyword | Physical Dimensions (Metric) | Physical Dimensions (Imperial) | CSS 96 DPI Pixels | Common Region |
|---|---|---|---|---|
A4 |
$210\text{ mm} \times 297\text{ mm}$ | $8.27\text{ in} \times 11.69\text{ in}$ | $793.7\text{ px} \times 1122.5\text{ px}$ | International Standard (ISO 216) |
letter |
$215.9\text{ mm} \times 279.4\text{ mm}$ | $8.5\text{ in} \times 11.0\text{ in}$ | $816\text{ px} \times 1056\text{ px}$ | North America (US, Canada) |
legal |
$215.9\text{ mm} \times 355.6\text{ mm}$ | $8.5\text{ in} \times 14.0\text{ in}$ | $816\text{ px} \times 1344\text{ px}$ | Legal Contracts (North America) |
A3 |
$297\text{ mm} \times 420\text{ mm}$ | $11.69\text{ in} \times 16.54\text{ in}$ | $1122.5\text{ px} \times 1587.4\text{ px}$ | Large Format Posters / Blueprints |
2. Page Pseudo-Classes (:first, :left, :right, :blank)
The CSS Paged Media specification provides pseudo-classes to customize specific sheets in a multi-page document:
/* Universal defaults */
@page {
size: A4;
margin: 20mm;
}
/* 1. Cover Page (Sheet 1) */
@page :first {
margin-top: 0;
margin-bottom: 0;
/* Often zero margin for full-bleed cover background */
}
/* 2. Left-hand / Even pages (Verso) in a bound book */
@page :left {
margin-left: 15mm; /* Outer edge */
margin-right: 25mm; /* Inner gutter for bookbinding spine */
}
/* 3. Right-hand / Odd pages (Recto) in a bound book */
@page :right {
margin-left: 25mm; /* Inner gutter for bookbinding spine */
margin-right: 15mm; /* Outer edge */
}
/* 4. Blank pages generated by page break padding */
@page :blank {
@top-center { content: none; }
@bottom-center { content: none; }
}
LEFT-HAND PAGE (:left) RIGHT-HAND PAGE (:right)
+-------------------+-------------------+ +-------------------+-------------------+
| Margin: 15mm | Gutter: 25mm | | Gutter: 25mm | Margin: 15mm |
| (Outer) | (Spine) | | (Spine) | (Outer) |
| | | S | | |
| Text Body | | P | | Text Body |
| Even Page | | I | | Odd Page |
| | | N | | |
| | | E | | |
+-------------------+-------------------+ +-------------------+-------------------+
3. Named Pages (Switching Orientations Mid-Document)
You can assign named @page contexts to specific HTML elements to dynamically switch paper size or orientation within the same document:
/* Default page is Portrait */
@page {
size: A4 portrait;
margin: 20mm;
}
/* Define a specialized landscape named page */
@page landscape-sheet {
size: A4 landscape;
margin: 15mm;
}
/* Attach the named page to an element */
.financial-ledger-table {
page: landscape-sheet;
break-before: page;
break-after: page;
}
๐ป Interactive Code Playground
Below is a complete, runnable HTML document demonstrating named pages, cover page margin resets, and portrait-to-landscape transitions.
Starter Code
Line-by-Line Code Breakdown
- Lines 50โ53 (
@page): Establishes standard A4 portrait ($210\text{mm} \times 297\text{mm}$) with a standard $20\text{mm}$ border margin. - Lines 56โ58 (
@page :first): Targets the very first sheet in the document stack and sets margin to0, allowing cover graphics to bleed directly to the sheet boundary. - Lines 61โ64 (
@page wide-table-page): Defines a custom named page withsize: A4 landscapeand reduced $15\text{mm}$ margins. - Lines 79โ81 (
.landscape-section): Assigns thepage: wide-table-page;property to the container, instructing the print layout engine to trigger a page break and rotate the virtual paper by 90 degrees. - Lines 72โ77 (
.page-sheet): Enforcesbreak-after: page;(with legacy fallbackpage-break-after: always;) so every visual sheet starts on a fresh physical page.
Expected Browser Render Output
- Screen View: Displays three neat, centered paper sheets with drop shadows over a slate-gray background. Sheet 1 is dark, Sheet 2 is portrait white, and Sheet 3 is a wide landscape sheet.
- Print Preview (
Cmd/Ctrl + P):- Page 1 prints in portrait with edge-to-edge dark theme.
- Page 2 prints in portrait with $20\text{mm}$ margins.
- Page 3 automatically rotates into landscape format, cleanly displaying all 8 columns without horizontal clipping or font shrinking.
๐๏ธ Hands-On Exercise
๐ฏ The Challenge: Bookbinding Gutter Layout
Scenario: You are publishing an academic whitepaper that will be printed double-sided and spiral-bound. If you use equal $15\text{mm}$ left and right margins, the spiral binding will drill holes directly through the text on the inside edges.
Instructions:
- Configure an
@pagebase rule with standardletter portraitpaper. - Use
@page :firstto remove headers and provide a balanced $20\text{mm}$ margin for the cover title. - Configure
@page :left(even pages) with a $25\text{mm}$ right margin (inner spine) and a $15\text{mm}$ left margin (outer edge). - Configure
@page :right(odd pages) with a $25\text{mm}$ left margin (inner spine) and a $15\text{mm}$ right margin (outer edge).
๐ Starter Code Sandbox
โ ๏ธ Common Pitfalls
- Mixing
marginonbodywith@pageMargins: If you setmargin: 20mmin@pageANDmargin: 20pxorpadding: 2remonbody, they add together! In print stylesheets, always setbody { margin: 0; padding: 0; }and let@page { margin: ... }exclusively control the paper margins. - Assuming
@pageApplies to Screen Media: The@pagerule has zero effect on standard browser window rendering. It is only evaluated when generating print previews, physical prints, or PDFs. - Relying on Default Browser Margins: If you omit
@page { margin: ... }, browsers apply unpredictable default margins ($0.4\text{in}$ to $0.75\text{in}$) depending on the user's operating system and printer driver. Always declare explicit@pagemargins for deterministic rendering.
๐ก Pro Tips
- Use Millimeters for International Layouts: While US developers default to
in, international document specifications (ISO 216) are defined in millimeters. Setting@page { size: A4; margin: 15mm; }avoids rounding inaccuracies across international rendering engines. - Implement Bleed and Crop Marks for Professional Presses: If you are outputting HTML/CSS for professional offset printing presses, use
@page { bleed: 3mm; marks: crop cross; }(supported in commercial PDF engines like PrinceXML and WeasyPrint). - Combine Named Pages with Landscape Charts: Keep the majority of your corporate document portrait, but isolate wide financial tables, architectural diagrams, and Gantt charts onto named landscape pages using
page: landscape-page; break-before: page;.
๐ Key Takeaways
- The
@pagerule configures paper size (A4,letter,legal), orientation (portrait,landscape), and page-level margins. @page :firsttargets the initial document page (ideal for zero-margin full-bleed covers).@page :leftand@page :rightenable asymmetric gutter margins for duplex bookbinding.- Named pages (
page: page-name;) permit switching orientation mid-document for wide data tables. - Always reset
body { margin: 0; padding: 0; }in print mode to prevent margin double-stacking. - --