How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Set A4 Paper Size in CSS for Print
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Set A4 Paper Size in CSS for Print
Web Development

How to Set A4 Paper Size in CSS for Print

how7o
By how7o
Last updated: May 23, 2026
5 Min Read
Set A4 paper size in CSS for printing
SHARE

To set A4 paper size in CSS for printing, declare @page { size: A4; } at the top of your stylesheet. Pair it with a @media print block that sets the body’s width and height to A4 dimensions (210mm × 297mm) so the layout matches the paper. Browsers honor this when printing or exporting to PDF.

Contents
  • The CSS
  • Other common paper sizes
  • Sensible print margins
  • Page breaks
  • Test it
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 in Chrome 124, Firefox 125, Safari 17. Originally published 2022-10-20, rewritten and updated 2026-05-17.

The CSS

@page {
    size:   A4;
    margin: 0;
}

@media print {
    html, body {
        width:   210mm;
        height:  297mm;
        margin:  0;
        padding: 0;
    }
}
  • @page defines the printed page’s box — paper size and margins. size: A4 is the keyword the browser maps to 210 × 297 mm.
  • margin: 0 removes the browser’s default printable-area margin so content fills the sheet edge-to-edge (most printers still keep a small unprintable border).
  • @media print sets explicit dimensions on the layout root, so your content’s measurements match the page. Useful when the on-screen design uses different sizes.
CSS @page A4 — size keyword, margin reset, landscape orientation, common paper sizes

Other common paper sizes

@page { size: A4; }          /* 210 × 297 mm   — international standard */
@page { size: Letter; }      /* 8.5 × 11 in    — US/Canada */
@page { size: Legal; }       /* 8.5 × 14 in    — US legal */
@page { size: A3; }          /* 297 × 420 mm */
@page { size: A5; }          /* 148 × 210 mm */
@page { size: A4 landscape; }/* A4 sideways */

/* Custom — explicit dimensions */
@page { size: 100mm 150mm; }

Sensible print margins

@page {
    size:   A4;
    margin: 20mm 15mm;       /* top/bottom 20mm, left/right 15mm */
}

For documents (not edge-to-edge invoices), keep a reasonable margin. Browsers and printers tend to give better results when the margin is set explicitly rather than left to the default.

Page breaks

/* Force a break before this element */
.new-page {
    break-before: page;
}

/* Avoid breaking inside */
.invoice-row {
    break-inside: avoid;
}

/* Avoid breaking right after a heading */
h2 {
    break-after: avoid;
}

Use break-* properties (modern names for the legacy page-break-*) to control where the printer breaks pages. Especially important for tables and multi-section documents.

Test it

  • Open the page in Chrome/Firefox/Safari.
  • Press Ctrl+P (Cmd+P on macOS) to open the print preview.
  • Confirm “Paper size” shows A4 and the layout matches the design.
  • Choose “Save as PDF” to generate a deterministic preview file you can share.

Frequently asked questions

Does @page work in every browser?

Yes — @page with size and margin is supported in every modern browser when printing or generating a PDF via the browser’s print dialog. The keywords (A4, Letter, Legal, A3, etc.) are part of the CSS spec. The user can still override the paper size from the print dialog — your @page sets the default.

What’s the difference between A4 as a keyword and explicit 210mm 297mm?

The keyword tells the print engine “this is A4 paper” — it picks the right physical paper from the printer when a real printer is involved. The explicit dimensions only define the page layout box. For browser PDF export they’re equivalent; for physical printing the keyword is more reliable.

Why margin: 0 — won’t content go to the edge?

Yes, intentionally — for an invoice or label that needs to fill the sheet, margin: 0 on @page removes the browser’s default print margins (usually 0.5in). Most home printers can’t actually print to the physical edge (there’s a hardware unprintable border), so you’ll still see a small white gutter. Test on the target printer.

How do I do landscape orientation?

Add it to @page: @page { size: A4 landscape; margin: 0; }. Common combos: size: A4 portrait (default), size: A4 landscape, size: Letter, size: Letter landscape. For mixed orientations within one document, use named pages with @page name { size: A4 landscape; } and apply page: name on specific elements.

Related guides

  • How to Change the Background Color of a Checkbox with CSS
  • Page Breaks Not Working with Tables in HTML / CSS

References

MDN @page: developer.mozilla.org/en-US/docs/Web/CSS/@page. MDN size: developer.mozilla.org/en-US/docs/Web/CSS/@page/size. MDN break-before/after/inside: developer.mozilla.org/en-US/docs/Web/CSS/break-before.

TAGGED:CSSlayoutprint

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Enforce a minimum phone-number length in WooCommerce checkout How to Set a Minimum Length for Phone Numbers in WooCommerce
Next Article Set and get cookies with JavaScript How to Set and Get Cookies with JavaScript
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026

You Might Also Like

Comment in a .gitignore file with the # character
Web Development

How to Comment in a .gitignore File

4 Min Read
PHP delete array element — unset, array_splice, array_filter, array_search
Web Development

How to Delete an Element from a PHP Array

7 Min Read
WordPress too many redirects HTTPS — Cloudflare flexible SSL loop and the wp-config fix
Web Development

Fix ERR_TOO_MANY_REDIRECTS in WordPress After Switching to HTTPS

7 Min Read
Include Composer packages in plain PHP projects
Web Development

How to Include Composer Packages in Plain PHP Projects (Autoload + Example)

5 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?