Home >Web Front-end >CSS Tutorial >Can Inline SVGs Be Embedded Directly in CSS Styles?

Can Inline SVGs Be Embedded Directly in CSS Styles?

Linda Hamilton
Linda HamiltonOriginal
2024-12-13 16:44:12984browse

Can Inline SVGs Be Embedded Directly in CSS Styles?

Inline SVG in CSS: Embedding Vector Graphics in Styles

Can we embed SVG images directly into CSS styles? Imagine using the following syntax:

.my-class {
  background-image: <svg>...</svg>;
}

Answer:

Yes, it is indeed possible to use inline SVG definitions in CSS. Here's how you can achieve this:

body {
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'>
        <linearGradient>

This code creates a URL-encoded SVG image and uses it as the background image for the element. The SVG consists of a linear gradient and a rectangle that fills the entire viewport.

The above is the detailed content of Can Inline SVGs Be Embedded Directly in CSS Styles?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn