Image output

PNG with transparent background

Generate images with a transparent backgound

You can create PNGs with a transparent background by simply adding body { background: transparent } to your CSS. Take a look at this example.

Note: this only works for PNGs. If you render the image as a JPG, the background will show as white.

Preview of PNG with transparent background

Interactive example

Edit and render this image

Change the HTML, CSS, or API options. The browser preview updates as you edit; select Image to render the final output through the API.

Example code

Use this request in your app

Choose a language and copy a complete request using this example's HTML, CSS, and API options.

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
    "os"
    "time"
)

func main() {
    request := map[string]any{
        "html": `
                <div class="wrapper">
                  <svg id="sw-js-blob-svg" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" version="1.1">
                    <defs>
                      <linearGradient id="sw-gradient" x1="0" x2="1" y1="1" y2="0">
                        <stop id="stop1" stop-color="rgba(248, 117, 55, 1)" offset="0%"></stop>
                        <stop id="stop2" stop-color="rgba(251, 168, 31, 1)" offset="100%"></stop>
                      </linearGradient>
                    </defs>
                    <path fill="url(#sw-gradient)" d="M24.6,-27.9C31.2,-23.7,35.5,-15.4,34.6,-7.9C33.8,-0.5,27.8,6.1,23.1,12.4C18.3,18.7,14.8,24.8,9.5,27.3C4.2,29.8,-2.9,28.6,-10.8,26.9C-18.7,25.2,-27.3,23,-33.1,17.1C-38.9,11.2,-41.8,1.7,-38.9,-5.5C-36,-12.7,-27.5,-17.6,-20,-21.7C-12.6,-25.7,-6.3,-29,1.3,-30.6C9,-32.2,17.9,-32.1,24.6,-27.9Z" width="100%" height="100%" transform="translate(50 50)" stroke-width="0" style="transition: all 0.3s ease 0s;"></path>
                  </svg>
                </div>
                `,
        "css": `
               .wrapper {
                 width: 400px;
                 height: 400px;
               }
               body {
                 background: transparent;
               }
               
               `,
        "render_when_ready": false,
        "transparent_background": true,
    }

    body, err := json.Marshal(request)
    if err != nil {
        panic(err)
    }
    httpRequest, err := http.NewRequest(http.MethodPost, "https://hcti.io/v1/image", bytes.NewReader(body))
    if err != nil {
        panic(err)
    }
    httpRequest.SetBasicAuth(os.Getenv("HCTI_API_ID"), os.Getenv("HCTI_API_KEY"))
    httpRequest.Header.Set("Content-Type", "application/json")

    client := &http.Client{Timeout: 30 * time.Second}
    response, err := client.Do(httpRequest)
    if err != nil {
        panic(err)
    }
    defer response.Body.Close()
    responseBody, err := io.ReadAll(response.Body)
    if err != nil {
        panic(err)
    }
    if response.StatusCode >= http.StatusBadRequest {
        panic(fmt.Sprintf("HCTI request failed (%d): %s", response.StatusCode, responseBody))
    }

    var result struct {
        URL string `json:"url"`
    }
    if err := json.Unmarshal(responseBody, &result); err != nil {
        panic(err)
    }
    fmt.Println(result.URL)
}

Request configuration

Options used in this example

These values are sent with the HTML and CSS to control how HCTI loads the page and captures the final screenshot. You can change each one in the editor above.

Transparent Background

transparent_background
True
Specifies whether the image is rendered with a transparent background.
Learn about this option

From code to screenshot

How HCTI examples work

HTML/CSS to Image renders HTML and CSS in hosted Chrome, then captures the result as an image or PDF. Each example combines real source code with the API options used for the render, so it is both a working demo and a starting point for your own request.

Edit the source and options above to test changes in the browser, then render through the API to see the production result.

Common questions

Can I use the HTML and CSS from these examples?

Yes. Open an example to inspect and edit its complete HTML, CSS, and render options. Use it as a starting point, then send the resulting request from your application.

What is the difference between an example and a template?

Examples are complete API requests built from HTML and CSS. Templates are saved, reusable designs created in the visual editor and rendered repeatedly with different variable values.

Does HCTI create screenshots and PDFs?

Yes. HCTI can render HTML and CSS or capture a public webpage as an image, and it can generate PDFs when you need document output instead of a screenshot.

Keep exploring

Related examples

View all examples