The HTML/CSS to Image API has a powerful auto-cropping feature that automatically sizes your image to match your HTML content. When you send an HTML snippet, the API crops the resulting image to the dimensions of the outermost HTML element.
However, this only works for HTML snippets—not full HTML pages.
Key Concept
Auto-cropping works by detecting the outermost element in your HTML and cropping to its dimensions. If you send a full HTML page with <html> and <body> tags, the API renders the entire viewport instead.
The Most Common Mistake
The most frequent cropping issue we see: sending a full HTML page when you meant to send an HTML snippet.
What Happens with a Full HTML Page
When you include <!DOCTYPE html>, <html>, <head>, or <body> tags, the API treats your content as a full webpage and renders it inside the default viewport (1366x768 pixels).
Example of what NOT to do:
<!DOCTYPE html><html><head><style>body{margin:0;padding:0;background:#f5f5f5;}</style></head><body><divstyle="height: 200px; width: 400px; background: white; border: 2px solid #e74c3c;"><h2>My Card</h2><p>This should be 400x200</p></div></body></html>
Result: Instead of a 400x200 image, you get a 1366x768 image with your content in the corner:
Notice how the image includes the entire viewport with a gray background, not just your 400x200 card.
The Solution: Use HTML Snippets
Remove the <!DOCTYPE>, <html>, <head>, and <body> tags. Send only the HTML elements you want in your image.
Need some breathing room around your content? Add margin to the outermost element. The auto-cropper respects margins and includes them in the final image.
Notice the transparent space around the card when using margin: 20px. This is useful for adding padding around social cards, thumbnails, or any image that needs visual breathing room.
When You Need Full HTML Pages
Sometimes you do need to send a full HTML page—for example, when including external libraries, complex CSS, or JavaScript. In these cases, use the viewport_width and viewport_height parameters to control the image size: