webdevpuneet.com

HTML to PNG Quote Card Designer with html-to-image — Free JavaScript Snippet

HTML to PNG Quote Card Designer (html-to-image) · Tools · Plain HTML, CSS & JS · Live preview

Copy & paste

Source Code

Requires
<div class="hc">
  <aside class="hc-panel">
    <h2>Quote card</h2>
    <label>Quote <textarea id="hcQuote" rows="4" maxlength="220">Design is not just what it looks like and feels like. Design is how it works.</textarea></label>
    <label>Author <input id="hcAuthor" value="Steve Jobs" maxlength="40"></label>
    <label>Handle <input id="hcHandle" value="@northwind.studio" maxlength="30"></label>
    <fieldset>
      <legend>Theme</legend>
      <div class="hc-themes" id="hcThemes"></div>
    </fieldset>
    <label>Size <select id="hcSize">
      <option value="1080x1080">Square post · 1080 × 1080</option>
      <option value="1080x1350">Portrait post · 1080 × 1350</option>
      <option value="1200x630">Link preview · 1200 × 630</option>
    </select></label>
    <button type="button" id="hcExport">Export PNG</button>
    <p class="hc-status" id="hcStatus" role="status"></p>
  </aside>
  <main class="hc-stage">
    <div class="hc-label">Design (live DOM)</div>
    <div class="hc-card" id="hcCard">
      <div class="hc-mark">“</div>
      <p class="hc-text" id="hcText"></p>
      <div class="hc-foot"><span class="hc-author" id="hcAuthorOut"></span><span class="hc-handle" id="hcHandleOut"></span></div>
    </div>
    <div class="hc-label" id="hcResultLabel" hidden>Exported PNG</div>
    <div class="hc-result" id="hcResult"></div>
  </main>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month, 7 days free), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

DOM to PNG
html-to-image via SVG foreignObject.
Works in sandboxed iframes
No helper iframe, unlike html2canvas.
Embedded web fonts
CORS-mode stylesheet so rules are readable.
Exact output sizes
pixelRatio from target and on-screen width.
Social presets
1080 × 1080, 1080 × 1350 and 1200 × 630.
Transparent corners
Pixels outside the rounded card stay clear.
Font readiness
Waits for document.fonts.ready.
In-page result
Preview, dimensions and file size.

About this UI Snippet

HTML to PNG in the Browser — html-to-image, Web Fonts and Exact Sizes

Screenshot of the HTML to PNG Quote Card Designer (html-to-image) snippet rendered live

Designing an image with HTML and CSS is much faster than drawing it on a canvas by hand: layout, typography and gradients come for free. Turning that element into a PNG is the missing step. This snippet uses html-to-image to export a quote card at exact social-media sizes.

How html-to-image works

It clones the element, inlines its computed styles, embeds web fonts and images as data URLs, wraps the result in an SVG <foreignObject>, loads that SVG as an image and draws it onto a canvas. Because the browser itself renders the HTML inside foreignObject, modern CSS generally comes out as it looks on screen.

Why not html2canvas here?

html2canvas takes a different approach: it copies the whole document into a hidden iframe and repaints it with the Canvas API. In a sandboxed iframe without allow-same-origin — like this site's live preview, many CodePen-style embeds and some CMS previews — the page has an opaque origin and is not allowed to read that helper iframe, so html2canvas fails with "Blocked a frame with origin null". html-to-image never creates an iframe, so it works in the same sandbox.

Web fonts need CORS

To embed a web font, html-to-image reads the font stylesheet's cssRules. Browsers only expose rules of a cross-origin stylesheet if it was loaded in CORS mode, so the Google Fonts <link> is added with crossOrigin = 'anonymous' (Google Fonts sends the required headers). Without it, export still works but falls back to a default font and logs errors. document.fonts.ready is awaited first so the fonts are actually loaded.

Exact output size

The card is about 420 px wide on screen; a square post is 1080 px. pixelRatio: 1080 / cardWidth renders the PNG at exactly the target size, and the card's aspect-ratio follows the chosen preset.

Seeing the result

The exported PNG appears below the design with its dimensions and size, so the export is visible even where downloads are blocked.

Step by step

How to Use

  1. 1
    Edit the quoteChange the text, author and handle; long quotes shrink to fit.
  2. 2
    Pick a theme and sizeFour gradients; square, portrait or link-preview proportions.
  3. 3
    Export PNGRendered at the exact pixel size and shown below.
  4. 4
    Check the dimensionsThe result lists width, height and file size.
  5. 5
    DownloadUse the link under the image (in its own tab if the sandbox blocks it).

Real-world uses

Common Use Cases

Social quote and tip cards
Design a quote in HTML and CSS, then export a crisp PNG at 1080 × 1080 for square feeds or 1080 × 1350 for portrait posts. Change themes and fonts live before you download.
Open Graph and link preview images
Generate share images at exactly 1200 × 630 from a normal HTML layout instead of a design tool. Layout, typography and gradients all come free from CSS.
Shareable badges and achievements
Turn a completed course, streak or milestone into an image users can post. Transparent rounded corners mean the PNG drops cleanly onto any background colour.
Exports inside sandboxed previews
Because html-to-image needs no helper iframe, the export keeps working inside sandboxed or CMS preview panes, where libraries like html2canvas often fail.
Branded cards on marketing sites
Let visitors personalise a branded card and share it. Web fonts are embedded through a CORS-mode stylesheet, so the exported image uses your typeface instead of a fallback.
Related: jsPDF Certificate Generator
Vector output instead of pixels: jsPDF Certificate Generator with Custom Script Font.
Related: Social Post Card
A post-style card layout: Social Post Card.

Got questions?

Frequently Asked Questions

With html-to-image, call htmlToImage.toPng(element, options), which resolves to a PNG data URL. Use it as an image src or as a download link.

html2canvas copies the document into a helper iframe and then reads it. In an iframe sandboxed without allow-same-origin the page has an opaque origin, and reading the helper frame is blocked as cross-origin access. html-to-image doesn't use a helper iframe.

The font wasn't loaded yet, or its stylesheet was cross-origin without CORS. Await document.fonts.ready and load the font CSS with crossorigin="anonymous" so its rules can be read and the font embedded.

Set pixelRatio to the target width divided by the element's rendered width, and keep the element's aspect ratio equal to the target's. html-to-image also accepts canvasWidth and canvasHeight.

It works well in Chromium and Firefox. Safari has had issues with fonts and images inside foreignObject, so test your design there, sometimes calling toPng twice helps the first render load resources.