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 CSSConvert 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.
-
1Install IronPDF with NuGet Package Manager
-
2Copy and run this code snippet.
new IronPdf.ChromePdfRenderer { RenderingOptions = { CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print } } .RenderUrlAsPdf("https://example.com") .SaveAs("responsive.pdf");C# -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
Minimal Workflow (5 steps)
- Download the IronPDF C# library for CSS and HTML integration
- Prepare the HTML file for conversion to PDF
- Specify CSS media type for optimal PDF formatting
- Set CSS media type 'Print' for repeated table headers in PDFs
- Configure viewport dimensions for responsive design
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?
![]()
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>
How Do I Implement Table Header Repetition in C#?
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");Imports IronPdf
Imports IronPdf.Rendering
Private renderer As 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
Dim pdf As PdfDocument = renderer.RenderHtmlFileAsPdf("tableHeader.html")
pdf.SaveAs("tableHeader.pdf")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?
The IronSuite play a crucial role in our operations. These are tools that increase efficiencies across the business including creating floor plans and improving inventory management.
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");Imports IronPdf
Imports IronPdf.Rendering
Dim renderer As 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
Dim customCss As String = "
@media print {
.no-print { display: none; }
body { font-size: 12pt; }
h1 { page-break-after: avoid; }
}
"
' Render with custom CSS injection
Dim htmlWithCss As String = $"<style>{customCss}</style>{yourHtmlContent}"
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlWithCss)
pdf.SaveAs("advanced-print-layout.pdf")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
- 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.
- Use Print for Documents: When creating formal documents, invoices, or reports, the print media type typically provides cleaner, more professional results.
- Leverage Media Queries: Utilize CSS media queries to fine-tune your layouts for different output scenarios.
- Consider Performance: Print stylesheets often render faster because they exclude unnecessary visual elements.
- 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 does IronPDF handle responsive design when converting HTML to PDF?
IronPDF allows developers to preserve responsive design in PDFs by supporting both 'screen' and 'print' media types, enabling CSS rules to be applied according to the target format.
What is the purpose of the 'print' media type in CSS when using IronPDF?
The 'print' media type in CSS is intended for optimizing the layout for printed pages, ensuring elements like font sizes and margins are adjusted, and unnecessary items are removed, providing a clean and professional PDF output.
Can IronPDF render both 'screen' and 'print' CSS styles into a PDF?
Yes, IronPDF provides complete control over which CSS media type to use, allowing developers to select 'screen' for web aesthetics or 'print' for formal document formatting.
How can I achieve repeated table headers in PDFs using IronPDF?
To repeat table headers in PDFs, set the CSS media type to 'PdfCssMediaType.Print' and ensure headers are enclosed in a '' tag. This exploits CSS rules that support pagination in print mode. Using 'screen' media type allows developers to retain interactive elements and rich visual designs from the web content, making PDFs suitable for viewing primarily on digital displays. IronPDF's Chrome PDF Rendering Engine provides developers with control over media types and ensures HTML to PDF conversions maintain intended design and functionality, suitable for both digital and printed outputs. Consider the document's use case: 'screen' is best for digital, interactive content, while 'print' is better for formal documents and printed materials requiring clean, ink-conservative designs. Developers can inject custom CSS by including a `What are the advantages of using the 'screen' CSS media type with IronPDF?
How does IronPDF's Chrome PDF Rendering Engine enhance PDF generation?
What should I consider when choosing between screen and print media types?
How can I inject custom CSS when generating PDFs with IronPDF?