Home > Web Front-end > CSS Tutorial > Can I Use SVG Images as Content in CSS Pseudo-elements?

Can I Use SVG Images as Content in CSS Pseudo-elements?

Barbara Streisand
Release: 2024-12-08 09:51:11
Original
645 people have browsed it

Can I Use SVG Images as Content in CSS Pseudo-elements?

Displaying SVG as Content in Pseudo-Elements

In CSS, pseudo-elements such as ::before and ::after can enhance element appearance by adding content before or after it. While text content is commonly used, is it possible to display an SVG image as content within these pseudo-elements?

To achieve this, specify the SVG image's URL as the content value for the pseudo-element. For instance:

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

Alternatively, you can embed the SVG directly within the CSS using the data:image/svg xml syntax:

#test::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 technique allows you to incorporate complex SVG graphics into your web pages using the power of CSS.

The above is the detailed content of Can I Use SVG Images as Content in CSS 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