How to Convert HTML to PDF with Responsive CSS using C#

How to Use CSS with HTML in C# for Responsive PDFs

IronPDF enables C# developers to convert HTML with CSS to PDF while preserving responsive design by supporting both 'screen' and 'print' media types. Print mode optimizes layouts for PDF output and enables features like repeated table headers across pages.

The CSS 'screen' media type is primarily for display on computer screens and similar devices. When styles are defined for the 'screen' media type, they affect how web content appears on screens, emphasizing visual design and interactivity. This includes elements like hover effects, animations, and background images that enhance the user experience on digital displays.

The CSS 'print' media type is designed for printing. It determines how the web page appears when printed, focusing on optimizing content for the printed page. This optimization may include adjusting font sizes, margins, and removing elements that are unnecessary when printed. Print stylesheets often remove navigation menus, sidebars, and decorative elements to focus on core content, making documents more readable and conserving ink.

When working with IronPDF's Chrome PDF Rendering Engine, developers have full control over which media type to use, ensuring that PDFs generated from HTML maintain the intended design and functionality. This flexibility is valuable when creating documents that need to serve both digital distribution and physical printing purposes.

Quickstart: Generate PDFs with Responsive HTML and CSS

Convert your HTML with responsive CSS to PDF using IronPDF. With just a few lines of code, ensure your PDFs render perfectly across devices, maintaining the integrity of both screen and print styles. This quick guide demonstrates how to use IronPDF to achieve professional PDF output from existing HTML content.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    new IronPdf.ChromePdfRenderer { RenderingOptions = { CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print } }
        .RenderUrlAsPdf("https://example.com")
        .SaveAs("responsive.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer


How Do Screen & Print CSS Types Work in CSS3?

Why Does Choosing the Right CSS Media Type Matter?

IronPDF generates PDFs from HTML in C# and can render a screen stylesheet to PDF by default. This is convenient because print stylesheets are often less well-documented, used, or developed compared to their screen counterparts. However, understanding the distinction between these media types is crucial for producing professional-quality PDFs.

When working with CSS (Screen & Print) in IronPDF, the choice between screen and print media types significantly impacts your final PDF output. Screen media types preserve interactive elements and rich visual designs, while print media types optimize content for readability and paper-based consumption.

CSS3 allows certain CSS styles to be rendered exclusively in printed documents, while others are intended for web browsers. IronPDF can work with either, giving you complete control over how your HTML content is rendered to PDF. This flexibility ensures that whether you're creating digital reports, printable invoices, or hybrid documents, your PDFs meet the specific requirements of their intended use case.

When Should I Use Print vs Screen CSS Media Type?

Create and apply a print stylesheet to your HTML: Learn how to create and apply a perfect print stylesheet..

Each CSS media type targets different use cases. Try each one to see which suits your requirement. Consider these guidelines:

Use Print CSS Media Type when:

  • Creating documents intended for physical printing
  • Generating invoices, reports, or formal documents
  • You need repeated table headers across pages
  • Conserving ink is a priority
  • Working with Custom Paper Size requirements

Use Screen CSS Media Type when:

  • Creating interactive PDFs with rich visual elements
  • Preserving web design aesthetics in the PDF
  • Including background images and colors
  • The PDF will primarily be viewed on screens
  • Working with responsive HTML to PDF conversions

What Are the Visual Differences Between Screen and Print Modes?

CSS print vs screen media types comparison showing Apple Mac page with images hidden in print version CSS media types comparison: Print layout omits icons, Screen layout includes icons for MacBook specifications

The examples above demonstrate how the same HTML content renders differently based on the CSS media type selection. Notice how the print version removes decorative images and icons to create a cleaner, more printable document, while the screen version maintains all visual elements for digital viewing.


How Do I Repeat Table Headers Across PDF Pages?

Why Do Table Headers Only Appear Once in Screen Mode?

When dealing with HTML tables that span multiple pages, set the CssMediaType property to PdfCssMediaType.Print. This ensures that the table header repeats at the top of each extended page. In contrast, PdfCssMediaType.Screen instructs Chrome to print the headers only once. This behavior is particularly important when creating PDF Reports with lengthy data tables.

The print media type activates specific CSS rules that browsers use for pagination, including the thead {display: table-header-group;} property that enables header repetition. This feature is essential for maintaining context when readers flip through multi-page documents.

What HTML Structure Is Required for Repeating Headers?

To ensure Chrome detects the table header, enclose it in a <thead> tag. Let's render the 'tableHeader.html example of repeating table headers' HTML file to PDF to see the effect.

Here's the recommended HTML structure:

<table>
    <thead>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
        </tr>
    </thead>
    <tbody>
        <!-- Your table data rows here -->
    </tbody>
</table>
<table>
    <thead>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
        </tr>
    </thead>
    <tbody>
        <!-- Your table data rows here -->
    </tbody>
</table>
HTML

How Do I Implement Table Header Repetition in C#?

:path=/static-assets/pdf/content-code-examples/how-to/html-to-pdf-responsive-css-table-header.cs
using IronPdf;
using IronPdf.Rendering;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Change the paper size to small
renderer.RenderingOptions.SetCustomPaperSizeinPixelsOrPoints(600, 400);

// Choose screen or print CSS media
renderer.RenderingOptions.CssMediaType = PdfCssMediaType.Print;

// Render HTML to PDF
PdfDocument pdf = renderer.RenderHtmlFileAsPdf("tableHeader.html");

pdf.SaveAs("tableHeader.pdf");
$vbLabelText   $csharpLabel

For more advanced table formatting options, explore Custom Margins to control spacing around your tables, or implement Page Breaks to ensure tables start on new pages when needed.

What Does the Final PDF Output Look Like?

Advanced CSS Media Type Configuration

When working with complex layouts, combine CSS media types with other IronPDF features. For instance, when using Viewport & Zoom settings, the CSS media type you choose affects how responsive designs scale in the final PDF.

Here's an example demonstrating advanced configuration:

using IronPdf;
using IronPdf.Rendering;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Configure for print media type with custom settings
renderer.RenderingOptions.CssMediaType = PdfCssMediaType.Print;
renderer.RenderingOptions.PrintHtmlBackgrounds = true; // Override print defaults
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;

// Add custom CSS for print media
string customCss = @"
    @media print {
        .no-print { display: none; }
        body { font-size: 12pt; }
        h1 { page-break-after: avoid; }
    }
";

// Render with custom CSS injection
string htmlWithCss = $"<style>{customCss}</style>{yourHtmlContent}";
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlWithCss);

pdf.SaveAs("advanced-print-layout.pdf");
using IronPdf;
using IronPdf.Rendering;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Configure for print media type with custom settings
renderer.RenderingOptions.CssMediaType = PdfCssMediaType.Print;
renderer.RenderingOptions.PrintHtmlBackgrounds = true; // Override print defaults
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;

// Add custom CSS for print media
string customCss = @"
    @media print {
        .no-print { display: none; }
        body { font-size: 12pt; }
        h1 { page-break-after: avoid; }
    }
";

// Render with custom CSS injection
string htmlWithCss = $"<style>{customCss}</style>{yourHtmlContent}";
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlWithCss);

pdf.SaveAs("advanced-print-layout.pdf");
$vbLabelText   $csharpLabel

For projects requiring pixel-perfect accuracy, consider using the Debug HTML with Chrome feature to preview exactly how your CSS media types will render before generating the final PDF.

Best Practices for CSS Media Types in IronPDF

  1. Test Both Media Types: Always test your HTML with both screen and print media types to determine which produces the best results for your specific use case.

  2. Use Print for Documents: When creating formal documents, invoices, or reports, the print media type typically provides cleaner, more professional results.

  3. Leverage Media Queries: Utilize CSS media queries to fine-tune your layouts for different output scenarios.

  4. Consider Performance: Print stylesheets often render faster because they exclude unnecessary visual elements.

  5. Validate Your CSS: Use the HTML Rendering Settings to ensure your CSS is properly applied during PDF generation.

By mastering CSS media types in IronPDF, you can create PDFs that perfectly match your requirements, whether they're designed for digital distribution or physical printing.

Frequently Asked Questions

How can I convert HTML with responsive CSS to PDF in C#?

IronPDF provides a simple way to convert HTML with responsive CSS to PDF in C#. You can use the ChromePdfRenderer class and specify the CSS media type (screen or print) through the RenderingOptions property. With just a few lines of code, IronPDF preserves your responsive design and ensures perfect rendering across different devices.

What is the difference between 'screen' and 'print' CSS media types when generating PDFs?

IronPDF supports both CSS media types. The 'screen' media type displays content as it appears on digital screens with hover effects and animations, while the 'print' media type optimizes content for printed pages by adjusting fonts, margins, and removing unnecessary elements like navigation menus. IronPDF's Chrome PDF Rendering Engine gives you full control over which media type to use.

How do I enable repeated table headers across PDF pages?

To enable repeated table headers across pages in your PDF, set the CSS media type to 'Print' in IronPDF. This can be done by setting CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print in the RenderingOptions. The print media type includes optimizations like repeated headers that make multi-page documents more readable.

Can I convert a live website URL directly to PDF with responsive design?

Yes, IronPDF allows you to convert live websites directly to PDF while maintaining responsive design. Use the RenderUrlAsPdf method with the ChromePdfRenderer class. You can specify whether to use screen or print CSS media types to ensure the PDF renders according to your requirements.

What rendering engine does the library use for HTML to PDF conversion?

IronPDF uses the Chrome PDF Rendering Engine, which provides accurate HTML to PDF conversion with full support for modern CSS3 features, JavaScript, and responsive design. This engine ensures that your PDFs maintain the intended design and functionality of the original HTML content.

How do I configure viewport dimensions for responsive PDF design?

IronPDF allows you to configure viewport dimensions through the RenderingOptions property of the ChromePdfRenderer. This ensures your responsive CSS media queries work correctly when converting HTML to PDF, allowing you to control how content appears at different viewport sizes.

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 16,821,795 | Version: 2025.12 just released