CSS Pagination (Print)
In this page:
The @page Rule
@page defines styling that applies specifically to printed pages, most commonly setting page margins and page size, and is only meaningful inside a print stylesheet or print media query -- it has no effect on normal screen rendering.
Example: The @page Rule
@page {
margin: 2cm;
size: A4;
}
Named Pages and :first
The @page rule supports a :first pseudo-class for styling only the first printed page differently (e.g. a larger top margin for a cover page), and named page selectors let different sections of a document use different @page rules.
Example: Named Pages and :first
@page :first {
margin-top: 5cm;
}
Controlling Breaks Before and After an Element
break-before and break-after (the modern replacements for the older page-break-before/page-break-after properties) force or avoid a page break immediately before or after a given element, commonly used to always start a new chapter or section on a fresh page.
Example: Controlling Breaks Before and After an Element
h1 {
break-before: page;
}
Preventing an Element From Splitting: break-inside
break-inside: avoid tells the printer not to split a given element's content across two pages at all -- essential for tables, figures, or code blocks where a mid-content split would make the printed page unreadable.
Example: Preventing an Element From Splitting: break-inside
table {
break-inside: avoid;
}
Widows and Orphans
widows and orphans control the minimum number of lines of a paragraph that must appear together at the top (widows) or bottom (orphans) of a page split, preventing the awkward typographic case of a single stray line left alone on a page.
Example: Widows and Orphans
p {
widows: 3;
orphans: 3;
}
Chapter Quiz — Complete all 25 topics to unlock
0/25 topics done
Complete these topics first:
- CSS Container Queries
- CSS Subgrid
- CSS Logical Properties
- CSS Logical Sizing
- CSS Writing Modes
- CSS Aspect Ratio
- CSS Object Fit
- CSS object-position
- CSS Masking
- CSS Math Functions
- CSS Motion Path
- CSS @supports
- CSS @property
- CSS At-Rules
- CSS Cursor
- CSS Will Change
- CSS User Interface
- CSS Print Styles
- CSS Pagination (Print)
- CSS Dark Mode
- CSS Accessibility
- CSS Performance
- CSS Preprocessors
- CSS Frameworks Overview
- CSS Interview Prep