You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Learn how to render an image with a transparent background. HTML/CSS to Image API.
8
+
Learn how to create images with transparent backgrounds using the HTML/CSS to Image API.
9
9
---
10
-
# Transparent background
10
+
11
+
# Transparent Backgrounds
11
12
{: .no_toc }
12
13
{: .fs-9 }
13
14
14
-
Learn how to create images with transparent backgrounds.
15
+
Create images with transparent backgrounds for logos, overlays, and more
15
16
{: .fs-6 .fw-300 }
16
17
17
18
<hr>
18
19
19
-
## How it works
20
+
Table of contents
21
+
{: .text-delta }
22
+
- TOC
23
+
{:toc}
24
+
25
+
<hr>
20
26
21
-
To render an image with a transparent background, include the following in your CSS when generating your image.
27
+
## Implementation
28
+
29
+
To render an image with a transparent background, set the background color to transparent in your CSS:
22
30
23
31
```css
24
32
body {
25
33
background-color: transparent;
26
34
}
27
35
```
28
36
29
-
## Example
37
+
{% include hint.md title="PNG Format Required" text="Transparency is only supported in PNG format. JPG and WebP images will render with a white background." %}
38
+
39
+
## Examples
30
40
31
-
This image has `background-color: transparent;` set on the `body`.
{% cloudinary /assets/images/cat.png sizes="400px" alt="HTML/CSS to Image with a transparent background." %}
47
+
{% cloudinary /assets/images/cat.png sizes="400px" alt="Example of transparent background" %}
36
48
</div>
37
49
</div>
38
50
39
51
[Try it yourself](https://htmlcsstoimage.com/examples/png-transparent-background){: .btn .btn-green .fs-5 .mb-4 .mb-md-0 .mr-2 }
40
52
41
-
<hr>
53
+
### Code Example
54
+
55
+
```html
56
+
<divclass="logo-container">
57
+
<imgsrc="logo.png"alt="Company Logo">
58
+
</div>
59
+
```
60
+
61
+
```css
62
+
body {
63
+
background-color: transparent;
64
+
}
65
+
.logo-container {
66
+
padding: 20px;
67
+
/* Any background styling here will be included in the image */
68
+
}
69
+
```
70
+
71
+
## Common Use Cases
42
72
43
-
## URL Images
44
-
When generating an image from a URL. Pass the same CSS in the CSS param and the background will be rendered as transparent.
73
+
1.**Logos**: Create logo variations with transparent backgrounds
74
+
2.**Overlays**: Generate images that layer well with other content
75
+
3.**Icons**: Design icons that work on any background
76
+
4.**Watermarks**: Create watermarks that blend seamlessly
77
+
78
+
## Best Practices
79
+
80
+
### Do's
81
+
- Always use PNG format for transparent backgrounds
82
+
- Test your image on different colored backgrounds
83
+
- Consider adding a subtle shadow for better visibility
84
+
- Use the `device_scale` parameter for higher quality
85
+
86
+
### Don'ts
87
+
- Don't use JPG format when transparency is needed
88
+
- Don't assume white is transparent
89
+
- Don't forget to set `background-color: transparent`
90
+
91
+
## URL-based Screenshots
92
+
93
+
When capturing screenshots from URLs, add the CSS via the API parameters:
94
+
95
+
```json
96
+
{
97
+
"url": "https://example.com",
98
+
"css": "body { background-color: transparent; }"
99
+
}
100
+
```
101
+
102
+
## Troubleshooting
103
+
104
+
### Common Issues
105
+
106
+
1.**White Background Appears**
107
+
- Ensure you're using PNG format
108
+
- Verify CSS is being applied
109
+
- Check you are passing CSS using the `css` parameter
110
+
111
+
2.**Partial Transparency**
112
+
- Look for background colors in child elements
113
+
- Check for overlapping elements
114
+
- Verify CSS inheritance
115
+
116
+
### Example Fix
117
+
118
+
```css
119
+
/* Before */
120
+
body { background: none; } /* This will not work */
121
+
122
+
/* After */
123
+
body { background-color: transparent; } /* This will work */
124
+
```
45
125
126
+
{% include hint.md title="Performance Tip" text="PNG files with transparency may be larger than JPGs. Consider using compression if file size is a concern." %}
46
127
47
-
{% include hint.md title="Works with PNG's only" text="Transparency is only supported by the **PNG** file format. By default, all images are rendered as PNG's. If you render the image as a JPG or Webp, the background will be white." %}
128
+
## Browser Compatibility
48
129
130
+
Transparent PNGs are supported in all modern browsers:
0 commit comments