IRONSOFTWAREHOME

Header and Content Misalignment

Curtis Chau
Curtis Chau
Updated: June 25, 2026

When rendering HTML to PDF, the header and footer can sit flush against the page edge while the body content is pushed inward, most visibly along the left and right margins. This happens even with MarginLeft = 0 set and UseMarginsOnHeaderAndFooter enabled.

The cause is Chrome's default styling. Its rendering engine applies an 8-pixel margin to the <body> element of every page. That default is fine for normal web content, but it leaves a visible 8px gap between your content and the edge of the PDF, regardless of the MarginLeft = 0 you pass to ChromePdfRenderer.RenderingOptions. The header and footer are unaffected: with margins set to 0, they align directly to the page edge as expected, so only the body drifts out of line.

This snippet reproduces the mismatch:

var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    HtmlFragment = "<h1>This is header</h1>"
};
renderer.RenderingOptions.MarginLeft = 0;
renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = UseMargins.Left;
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
C#

PDF showing header flush left but body content misaligned inward

Solution

Override Chrome's default <body> margin so the content lines up with the header and footer across the full page width. Apply negative margins to cancel the built-in 8px offset:

body {
margin-left: -8px;
margin-right: -8px;
}
Text

The negative values neutralize Chrome's default padding so the header, body, and footer all align to the same edge. Applied inline, the corrected render looks like this:

var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    HtmlFragment = "<h1>This is header</h1>"
};
renderer.RenderingOptions.MarginLeft = 0;
renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = UseMargins.Left;
var pdf = renderer.RenderHtmlAsPdf("<h1 style=\"margin-left: -8;\">Hello World</h1>");  //adjust margin left to -8 px to cancel out default Chrome margin
C#

PDF showing header and body content aligned to the same left edge after the fix

Debug Tips

  • Prefer a clean slate: instead of negative margins, apply a CSS reset or set margin: 0 explicitly on the <body> element to remove the default offset at the source.
  • Scope of the behavior: the 8px gap shows up whenever HTML is rendered through headless Chrome, including IronPDF, Puppeteer, and the Chrome DevTools Protocol, so the same fix applies across those tools.
Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Ready to Get Started?

Nuget Downloads 21,105,021Version:2026.9just released

Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

OR
bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999