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
Release: 2024-12-13 16:44:12
Original
972 people have browsed it

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>;
}
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template