CSS not working with DOMPDF

CSS not working with DOMPDF

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

CSS Not Working with DOMPDF

If you are generating PDFs from HTML using DOMPDF and finding that your stylesheets or inline styles are completely ignored, the issue usually stems from how the library parses the document rather than a configuration error. You might see images rendering correctly while text remains unstyled, or layout properties like float failing silently. This disconnect often happens because DOMPDF operates on a simplified subset of CSS and relies heavily on specific initialization steps that differ from standard browser behavior.

The core problem in many cases is the timing of the DOM loading relative to the buffer output. In your setup, you are using ob_start() at the beginning of the script to capture the HTML content. While this is a valid technique for capturing raw HTML strings, it creates a specific constraint: the HTML string passed to $dompdf->load_html() must be fully formed and complete before DOMPDF attempts to parse its CSS rules.

When you use external stylesheets via <link href="flhaha.css" rel="stylesheet">, DOMPDF expects the path provided in the href attribute to be relative to the file system location where the HTML is saved, not the URL of the PHP script executing it. If that file does not exist in the expected directory relative to the current working directory, DOMPDF will ignore the link entirely without throwing an error. This often leads to the scenario where inline styles work (because they are embedded directly in the DOM) but external stylesheets fail silently.

A more robust approach for this specific use case is to move all CSS definitions into the <head> section using the <style> tag or, even better, inject them directly into the HTML string before passing it to load_html(). This eliminates path resolution issues and ensures that the CSS rules are available immediately when DOMPDF constructs its internal document object.

Regarding the specific limitation you noted about the float property: this is a known constraint in older versions of DOMPDF (such as 0.5.2). The library's rendering engine, based on FreeType and PDF generation logic, does not fully support CSS floats for layout purposes like side-by-side columns. Attempting to use float: left; or float: right; will result in elements stacking vertically instead of flowing around images. To achieve a similar visual effect without relying on unsupported CSS features, you must explicitly define the position using absolute positioning or table-based layouts, depending on your specific design requirements.

Another common pitfall involves font families. Even if you specify font-family: Arial in your CSS, DOMPDF will only apply it if that font is actually available to the PDF generator. If you are generating the PDF on a server where Arial is not installed or registered in the PDF font map, DOMPDF will fall back to a default serif font, making it appear as though no styles were applied at all. You may need to explicitly load custom fonts using $dompdf->font_add() and register them before rendering if your design relies on specific typography.

If you require more complex styling or need to ensure consistent CSS handling across different environments, consider upgrading DOMPDF to a newer version where these limitations have been addressed, or switch to an alternative library like mPDF or TCPDF which offer different CSS parsing capabilities. For those working within the Laravel ecosystem, utilizing packages that wrap these libraries can provide better abstraction and error handling, ensuring that your HTML-to-PDF conversion pipeline is less prone to silent failures caused by missing resources or unsupported CSS properties.

Our network