Home >Web Front-end >CSS Tutorial >How Can I Embed SVG Images Directly into My CSS Using Inline Definitions?

How Can I Embed SVG Images Directly into My CSS Using Inline Definitions?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-09 20:52:10855browse

How Can I Embed SVG Images Directly into My CSS Using Inline Definitions?

Incorporating Inline SVG Images into CSS

CSS provides a convenient method to integrate Scalable Vector Graphics (SVGs) directly within its codebase using inline definitions. This technique allows developers to embed SVG graphics as background images or other design elements.

Usage

To embed an inline SVG image in CSS, use the following syntax:

/* Class with inline SVG background image */
.my-class {
  background-image: url("data:image/svg+xml;utf8,
    <svg xmlns='http://www.w3.org/2000/svg' width='...' height='...'>
      ...
    </svg>");
}

Example

Consider the following example:

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

In this example, an inline SVG definition creates a colorful gradient background that spans the entire page. The inline SVG contains a linearGradient to define a color gradient and a rect filled with the gradient.

The above is the detailed content of How Can I Embed SVG Images Directly into My CSS Using Inline Definitions?. 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