User Agents

Web browsers and everyday web tech, explained

Fix Kerning Issues in Headless Chrome Screenshots

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Headless Chrome often renders text with incorrect kerning in screenshots, especially when using webfonts or certain font-smoothing settings. This guide explains how to fix kerning issues so your screenshots match the on-screen browser rendering. You'll need a basic understanding of Chrome DevTools and command-line usage.

What You'll Need

  • Google Chrome or Chromium (version 60+ recommended)
  • Node.js (if using Puppeteer) or a scripting environment
  • Access to the website or HTML file you're screenshotting
  • Basic knowledge of CSS and Chrome flags

Step-by-Step

  1. Identify the kerning issue. Take a screenshot with headless Chrome and compare it to a regular Chrome window. Look for uneven spacing between letter pairs (e.g., 'AV', 'To', 'Wa'). If you see it, proceed.
  2. Check your font loading. Ensure the webfont is fully loaded before the screenshot. In Puppeteer, use document.fonts.ready to wait. For CLI, use --virtual-time-budget=5000 to give fonts time to load.
  3. Enable font kerning in CSS. Add font-kerning: normal; to your CSS for text elements. Headless Chrome sometimes defaults to auto, which may disable kerning for some fonts.
  4. Set the correct font-feature-settings. If your font uses OpenType kerning, add font-feature-settings: "kern" 1; to ensure kerning is applied. This is especially important for fonts like Source Serif Pro or Playfair Display.
  5. Adjust the device scale factor. Kerning can appear off at non-integer scale factors. In Puppeteer, set deviceScaleFactor: 1 (or 2 for retina) to avoid subpixel blurring.
  6. Disable font smoothing. Headless Chrome on Linux may use different font smoothing. Add the flag --disable-lcd-text to force grayscale antialiasing, which often improves kerning consistency.
  7. Use the --force-color-profile flag. Run Chrome with --force-color-profile=srgb to standardize color and text rendering, which can affect kerning perception.
  8. Test with a simple HTML file. Create a minimal test page with known kerning pairs, screenshot it, and verify if the issue persists. This isolates the problem.
  9. Use Chrome's headless mode properly. For Chrome 112+, use --headless=new instead of the old headless mode, as it renders more consistently with the full Chrome.
  10. If using Puppeteer, set the viewport correctly. Use page.setViewport({ width: 1280, height: 800, deviceScaleFactor: 1 }) and wait for network idle plus font readiness.

Common Mistakes

  • Not waiting for webfonts to load before taking the screenshot.
  • Using font-kerning: auto which lets the browser decide—often disabling kerning for performance.
  • Forgetting to set font-feature-settings for fonts that require it.
  • Using an old headless mode (without --headless=new) that has different rendering.
  • Taking screenshots at non-integer device scale factors (e.g., 1.5) which causes subpixel positioning.

Tips

  • Always use the same Chrome version for testing and production screenshots.
  • If you're using Puppeteer, consider using page.emulateMediaType('screen') to avoid print styles that may alter kerning.
  • For critical screenshots, use a headless browser with the same OS as your target audience (e.g., Linux for server-side screenshots).
  • Use Chrome's remote debugging to inspect the computed styles of the text element to verify kerning is applied.
  • Test with multiple fonts—some fonts have poor kerning tables, and the issue may not be Chrome's fault.

Troubleshooting

If kerning still looks wrong after following the steps:

  • Check if the font is actually loaded. Use document.fonts.check('16px FontName') in the page context to confirm.
  • Try a different font. If the issue persists with a standard font like Arial, the problem is with Chrome's rendering. If it's only with your webfont, the font file may be missing kerning tables.
  • Use Chrome's headless with --disable-gpu. Sometimes GPU acceleration causes text rendering differences. Add --disable-gpu to the Chrome flags.
  • Compare with a non-headless screenshot. Take a screenshot using regular Chrome on the same machine. If they differ, the headless mode is the culprit.
  • Update Chrome. Kerning bugs are frequently fixed in newer versions. Always use the latest stable release.

Frequently Asked Questions

Why does headless Chrome ignore kerning?

Headless Chrome may disable kerning for performance reasons, especially when the page hasn't fully loaded or when using certain font-smoothing settings.

Does font-kerning: normal always fix it?

Not always. Some fonts require font-feature-settings: "kern" 1 as well. Also, if the font lacks kerning data, no CSS will help.

Can I use the same flags for Puppeteer?

Yes, you can pass Chrome flags via Puppeteer's launch({ args: ['--disable-lcd-text'] }).

Will this affect performance?

Enabling kerning and waiting for fonts may slow down screenshots slightly, but the difference is negligible for most use cases.

Does headless Chrome on Windows render kerning differently than on Linux?

Yes, font rendering varies by OS. Windows uses DirectWrite, Linux uses FreeType. Test on your target platform.

Is there a way to force kerning without changing the page CSS?

You can inject CSS via Puppeteer's page.addStyleTag to apply kerning rules dynamically.