How to HTML To PDF Css Guide

CSS controls how your HTML-to-PDF output looks — page margins, fonts, and print-specific layouts. SublimePDF uses Chromium rendering, so standard CSS works plus @page rules for PDF control. Mastering print CSS lets you create pixel-perfect invoices, reports, and documents that look identical on screen and in the generated PDF.

Follow the step-by-step instructions below, then use the free tool directly — no registration or download required.

Open Tool →

How to HTML To PDF Css Guide — Step by Step

1

Set page dimensions with @page

Use the @page CSS rule to control page size and margins: @page { size: A4; margin: 2cm; }. Supported size values include A4, A3, Letter, Legal, or custom dimensions like 'size: 210mm 297mm'. Margin shorthand works normally.

2

Control page breaks

Use break-before: page to force an element to start on a new page, break-after: page to insert a break after it, and break-inside: avoid to prevent an element from being split across pages. Apply these to headings, sections, and tables.

3

Style for print media

Wrap PDF-specific styles in @media print { } to separate screen and print layouts. Hide navigation, sidebars, and interactive elements. Adjust colors — switch from light-on-dark to dark-on-light for print readability and ink savings.

4

Handle tables across pages

For long tables, use thead { display: table-header-group; } to repeat the header row on every page. Add break-inside: avoid on <tr> elements to prevent rows from splitting awkwardly between pages.

5

Set backgrounds and colors for PDF

By default, Chromium's print mode strips background colors and images. Add -webkit-print-color-adjust: exact (or print-color-adjust: exact) to preserve colored backgrounds, shading, and background images in your PDF output.

Pro Tips

  • 💡 Always set box-sizing: border-box on all elements when designing PDF layouts — this prevents unexpected width calculations that push content off the page edge.
  • 💡 Use cm or mm units instead of px for margins and spacing in print layouts. These translate directly to physical dimensions, making your PDF look consistent across different viewer zoom levels.
  • 💡 Test your CSS at different page sizes by changing the @page size rule — content that fits perfectly on Letter may overflow on A4 (narrower width) and vice versa.
  • 💡 Use overflow: hidden on the body element to catch content that accidentally extends beyond page bounds, preventing a mysterious extra blank page at the end of your PDF.

Privacy & Security

All processing happens directly in your browser. Your files are never uploaded to any server — they remain on your device throughout the entire process. SublimePDF uses WebAssembly technology for fast, secure, client-side processing.

Works Everywhere

This tool works on any modern browser — Chrome, Firefox, Safari, or Edge — on desktop, tablet, or mobile. No software to install. PDF is an open ISO standard supported by all major platforms.

How to HTML To PDF Css Guide — FAQ

Why do my background colors disappear in the PDF?
Chromium's print mode removes backgrounds by default to save ink. Add 'print-color-adjust: exact' (or '-webkit-print-color-adjust: exact') to the element to force backgrounds to render in the PDF.
Can I use CSS Grid and Flexbox in PDF templates?
Yes. SublimePDF renders with a full Chromium engine that supports CSS Grid, Flexbox, custom properties (variables), calc(), clamp(), and all modern CSS features. The only CSS that doesn't apply is interactive pseudo-classes like :hover.
How do I prevent a blank page at the end of my PDF?
This usually happens when content slightly overflows the page area. Check for margin/padding that pushes the total height past the page break threshold. Set body { margin: 0; } and ensure your @page margins account for all content spacing.
Can I have different margins on the first page?
Yes. Use @page :first { margin-top: 4cm; } to set different margins on the first page — useful for leaving room for a letterhead or logo above the main content.

Ready to get started?

Use SublimePDF's free tools right now.

Open Tool