docs(guides/images-and-svgs): monochrome images, inlined svg data urls (#1896)
Co-authored-by: WalkQuackBack <[email protected]>
This commit is contained in:
1 parent
eb178bfb5a
commit
54ad106e79
1 file changed
+53
-18
@@ -5,9 +5,25 @@ sidebar:
|
||||
order: 2
|
||||
---
|
||||
|
||||
## Monochrome `<img>` elements
|
||||
|
||||
If you encounter an `<img>` element with single colored contents, the easiest way to theme it is to apply a CSS color filter. The `@<color>-filter` filters can be used to theme Catppuccin colours.
|
||||
|
||||
For example:
|
||||
|
||||
```css
|
||||
img.twitter-icon {
|
||||
filter: @blue-filter;
|
||||
}
|
||||
```
|
||||
|
||||
## SVGs as `background-image`s
|
||||
|
||||
Websites will sometimes use the `background-image` CSS property to apply an SVG, often for icons. We will refer to these as "external SVGs" throughout the rest of this guide, as the SVGs are usually at a different URL and linked to with [`url()`](https://developer.mozilla.org/en-US/docs/Web/CSS/url). Below is an example of what a rule for an external SVG could look like.
|
||||
Websites will sometimes use the `background-image` CSS property to apply an SVG, often for icons. The value of the `background-image` is usually either a URL to an external SVG resource, such as `url("https://example.org/assets/xyz.svg")`, or an inlined SVG [data URL](https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data), like `url("data:image/svg+xml,%3Csvg%3E...%3C/svg%3E");`.
|
||||
|
||||
### External SVG URL
|
||||
|
||||
Below is an example of what a rule for an external SVG URL could look like.
|
||||
|
||||
```css
|
||||
.xyz {
|
||||
@@ -15,17 +31,46 @@ Websites will sometimes use the `background-image` CSS property to apply an SVG,
|
||||
}
|
||||
```
|
||||
|
||||
The easiest way to theme external SVGs is to visit the URL of the SVG and paste its contents in between the single quotes of the `escape('')` section of the following template:
|
||||
To obtain the contents of this remote SVG resource, you can either:
|
||||
- Visit the URL of the SVG, right click and select **View Page Source**, and copy its contents from the resulting page.
|
||||
- Run `curl "https://example.org/assets/xyz.svg"` and copy the contents from the terminal output.
|
||||
|
||||
> [!TIP]
|
||||
> On macOS, copy the SVG contents after `curl`-ing automatically with `| pbcopy`: `curl "https://example.org/assets/xyz.svg" | pbcopy`.
|
||||
|
||||
### Inlined SVG data URL
|
||||
|
||||
Below is an example of what a rule for an inlined SVG data URL could look like.
|
||||
|
||||
```css
|
||||
.xyz {
|
||||
background-image: url("data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%3E%3Cpath%20fill%3D%22red%22%20d%3D%22%22/%3E%3C/svg%3E");
|
||||
}
|
||||
```
|
||||
|
||||
The text after `data:image/svg+xml,` is URL encoded. To get the contents of the SVG, the string needs to be URL decoded.
|
||||
|
||||
There are two ways to do this:
|
||||
- Using the JavaScript built-in function `decodeURIComponent`. You can run `node -e 'console.log(decodeURIComponent("..."))'`, with the URL encoded SVG contents placed between the double quotes in place of `...`.
|
||||
- Use an online tool such as [https://www.urldecoder.io/](URLDecoder).
|
||||
|
||||
---
|
||||
|
||||
Once you have obtained the SVG contents, paste them in between the single quotes of `escape('')` in the following template:
|
||||
|
||||
<!-- deno-fmt-ignore -->
|
||||
```less
|
||||
.xyz {
|
||||
@svg: escape("<svg>...</svg>");
|
||||
@svg: escape('');
|
||||
background-image: url("data:image/svg+xml,@{svg}");
|
||||
}
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
> The `Invalid % without number` error may appear if you have not done the following step. Make sure to add/replace an interpolated color in the SVG contents (as is detailed below) for this error to go away.
|
||||
> Ensure `escape()` uses single quotes, not double quotes, before pasting the SVG contents — `escape('')`, not `escape("")`.
|
||||
|
||||
> [!WARNING]
|
||||
> The error `Invalid % without number` appears until an interpolated color is added in the SVG contents. This is covered in the following section.
|
||||
|
||||
Now, replace any colors in the SVG with their respective Catppuccin variants. For example, take the following SVG icon for Twitter:
|
||||
|
||||
@@ -44,9 +89,11 @@ There is only one color used, `fill="#1D9BF0"`. That hex code is a shade of blue
|
||||
}
|
||||
```
|
||||
|
||||
## `<img>` elements
|
||||
## External SVGs through `<img>` elements
|
||||
|
||||
Theming an inline image is similar, but `content` is used instead of `background-image` to cover up the original image with our new one. As with the previous tip for `background-image`, you only need to update the SVG inside of the `escape('')` (see above for details).
|
||||
Theming an external SVG referenced from an `<img>` element is similar. However, the `content` property is used instead of `background-image` to replace the image.
|
||||
|
||||
As with the previous tip for `background-image`, only the SVG inside of the `escape('')` needs to be updated.
|
||||
|
||||
```less
|
||||
img.xyz {
|
||||
@@ -65,15 +112,3 @@ img.twitter-icon {
|
||||
content: url("data:image/svg+xml,@{svg}");
|
||||
}
|
||||
```
|
||||
|
||||
## Non-SVG images or many `<img>` elements with external SVGs
|
||||
|
||||
If you encounter non-SVG images, or many `<img>` elements that are single-colored, the best approach is to apply a CSS color filter to the images. You can use the `@<color>-filter` variables provided out of the box.
|
||||
|
||||
For example:
|
||||
|
||||
```css
|
||||
img.twitter-icon {
|
||||
filter: @blue-filter;
|
||||
}
|
||||
```
|
||||
Reference in new issue
Block a user