Skip to content

Commit 9e295f4

Browse files
committed
improve transparent background doc
1 parent 18f9681 commit 9e295f4

File tree

1 file changed

+100
-12
lines changed

1 file changed

+100
-12
lines changed

guides/transparent-background.md

Lines changed: 100 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,135 @@ permalink: /guides/transparent-background/
55
parent: Guides
66
nav_order: 1
77
description: >-
8-
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.
99
---
10-
# Transparent background
10+
11+
# Transparent Backgrounds
1112
{: .no_toc }
1213
{: .fs-9 }
1314

14-
Learn how to create images with transparent backgrounds.
15+
Create images with transparent backgrounds for logos, overlays, and more
1516
{: .fs-6 .fw-300 }
1617

1718
<hr>
1819

19-
## How it works
20+
Table of contents
21+
{: .text-delta }
22+
- TOC
23+
{:toc}
24+
25+
<hr>
2026

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:
2230

2331
```css
2432
body {
2533
background-color: transparent;
2634
}
2735
```
2836

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
3040

31-
This image has `background-color: transparent;` set on the `body`.
41+
### Basic Example
42+
43+
This image demonstrates a transparent background:
3244

3345
<div class="code-example" markdown="1">
3446
<div class="hcti-container" style="background-color: unset;">
35-
{% 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" %}
3648
</div>
3749
</div>
3850

3951
[Try it yourself](https://htmlcsstoimage.com/examples/png-transparent-background){: .btn .btn-green .fs-5 .mb-4 .mb-md-0 .mr-2 }
4052

41-
<hr>
53+
### Code Example
54+
55+
```html
56+
<div class="logo-container">
57+
<img src="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
4272

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+
```
45125

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." %}
46127

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
48129

130+
Transparent PNGs are supported in all modern browsers:
131+
- Chrome
132+
- Firefox
133+
- Safari
134+
- Edge
135+
- Opera
136+
- Mobile browsers
49137

50138
{% include code_footer.md version=3 %}
51139

0 commit comments

Comments
 (0)