Home > Web Front-end > CSS Tutorial > How Can I Embed SVGs in CSS ::before and ::after Pseudo-elements?

How Can I Embed SVGs in CSS ::before and ::after Pseudo-elements?

Linda Hamilton
Release: 2024-12-06 13:35:12
Original
773 people have browsed it

How Can I Embed SVGs in CSS ::before and ::after Pseudo-elements?

Using SVGs in ::before and ::after Pseudo-Elements

It's often desired to embed SVG images as content within pseudo-elements ::before and ::after. While the plaintext version doesn't work, there are alternative solutions using URL() or inline data URI.

Using URL()

The first method involves referencing the SVG file's URL:

#mydiv::before {
  content: url(path/to/your.svg);
  width: 200px;
  height: 200px;
}
Copy after login

Using Data URI

Another option is to encode the SVG directly into the CSS using data URI encoding:

#mydiv::before {
  content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'%3E%3Ccircle cx='100' cy='50' r='40' stroke='black' stroke-width='2' fill='red'/%3E%3Cpolyline points='20,20 40,25 60,40 80,120 120,140 200,180'>
Copy after login

This method allows you to embed the SVG image directly into the CSS, making it self-contained.

The above is the detailed content of How Can I Embed SVGs in CSS ::before and ::after Pseudo-elements?. 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