Usage Tips
html2canvas element alignment fix
When using html2canvas, elements may misalign compared to browser rendering. The problem: html2canvas element alignment differs from actual browser rendering. Solution: Dynamic CSS injection Fix alignment by injecting temporary fix styles and controlling render parameters.| Parameter | Recommended Value | Purpose |
|---|---|---|
foreignObjectRendering | false | Disable foreignObject rendering, use traditional Canvas drawing to avoid alignment issues from SVG rendering differences |
box-sizing | border-box | Unified box model calculation (includes padding/border) to avoid size calculation errors from default content-box |
line-height | 1.2 | Fixed line height to eliminate vertical alignment shifts from dynamic line height changes |
img { display } | inline-block | Avoid baseline alignment issues from img default inline element vertical-align: baseline |
- Configuration consistency: All export scenarios (single export, batch export) should use the same config to avoid alignment issues from different rendering logic.
-
Wait adequately: After changing page state (dynamic content loading, style changes), wait at least
1000msbefore capturing to ensure DOM and styles are fully stable. -
Clean up promptly: Use
try...finallyto ensure temporary fix styles (export-fix-style) are removed after capture to avoid polluting normal page styles. - Test thoroughly: Cover “single export,” “batch export,” “complex layouts (images/text/nested elements)” to verify alignment consistency.

