Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use SVG as content in a pseudo element ::before or ::after

I want to place some SVG images before some selected elements. I am using jQuery but that is irrelevant.

I would like to have the ::before element to be as:

#mydiv::before {   content: '<svg ... code here</svg>';   display: block;   width: 22px;   height: 10px;   margin: 10px 5px 0 10px; } 

If I do it as shown above, it just display the string. I checked the specs and there seems to be some restrictions on what content can be. Is there a work around to this restriction? Only CSS content is relevant to my question.

like image 682
Sunny Avatar asked Oct 08 '13 18:10

Sunny


People also ask

Can an SVG have a pseudo-element?

SVG content can be added using these pseudo-elements by following the methods described below: Method 1: Using the background-image property: The background-image property is used to set one or more background images to an element.

Can you put an SVG in content CSS?

Using SVG as an <object> But, if you want the CSS stuff to work, you can't use an external stylesheet or <style> on the document, you need to use a <style> element inside the SVG file itself.

Can a SVG be embedded inside a HTML doc?

SVG images can be written directly into the HTML document using the <svg> </svg> tag. To do this, open the SVG image in VS code or your preferred IDE, copy the code, and paste it inside the <body> element in your HTML document.

What does the pseudo-element :: Before do?

::before (:before) In CSS, ::before creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.


1 Answers

Yes you can! Just tested this and it works great, this is awesome! It still doesn't work with html, but it does with svg.

In my index.html I have:

<div id="test" style="content: url(test.svg); width: 200px; height: 200px;"></div> 

And my test.svg looks like this:

<svg xmlns="http://www.w3.org/2000/svg">    <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red"/>    <polyline points="20,20 40,25 60,40 80,120 120,140 200,180" style="fill:none;stroke:black;stroke-width:3"/> </svg>  
like image 147
dezman Avatar answered Oct 24 '22 17:10

dezman