有没有一种方法可以将SVG用作伪元素中的内容:before或:after

我想在一些选定的元素之前放置一些SVG图像。 我正在使用JQuery,但这是无关紧要的。 我希望将before元素设置为:

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

如果我这样做,如上所示,它只是显示string。 我检查了规格,似乎有什么内容的限制。 有没有解决这个限制的工作? 只有内容CSS是与我的问题相关的。

对不起, 规范说 , content属性只支持生成的文本内容,没有HTML,SVG的东西,或标签。

是的你可以! 刚刚testing过这个,它工作得很好,这真棒! 它仍然不能使用html,但它与svgs。

在我的index.html中我有:

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

我的test.svg如下所示:

 <svg xmlns="http://www.w3.org/2000/svg" version="1.1"> <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> 

是。 您可以将SVG添加为:after或之前的空白背景图像。 干得好:

 .anchor:before { display: block; content: ' '; background-image: url('..http://img.dovov.comanchor.svg'); background-size: 28px 28px; height: 28px; width: 28px; } 
 #mydiv:before { content: url("data:image/svg+xml; utf8, <svg.. code here</svg>"); display:block; width:22px; height:10px; margin:10px 5px 0 10px; } 

确保你的svg不包含双引号,并且uriencode#符号。

利用CSS精灵和数据uri提供了额外的有趣的好处,如快速加载和更less的请求,我们通过使用image / base64获得IE8的支持:

使用SVG的Codepen示例

HTML

 <div class="div1"></div> <div class="div2"></div> 

CSS

 .div1:after, .div2:after { content: ''; display: block; height: 80px; width: 80px; background-image: url(data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20height%3D%2280%22%20width%3D%22160%22%3E%0D%0A%20%20%3Ccircle%20cx%3D%2240%22%20cy%3D%2240%22%20r%3D%2238%22%20stroke%3D%22black%22%20stroke-width%3D%221%22%20fill%3D%22red%22%20%2F%3E%0D%0A%20%20%3Ccircle%20cx%3D%22120%22%20cy%3D%2240%22%20r%3D%2238%22%20stroke%3D%22black%22%20stroke-width%3D%221%22%20fill%3D%22blue%22%20%2F%3E%0D%0A%3C%2Fsvg%3E); } .div2:after { background-position: -80px 0; } 

对于IE8,请更改为:

  background-image: url(data:image/png;base64,data......); 

小心所有的其他答案在IE中有一些问题。

让我们有这种情况 – button与前置图标。 所有的浏览器都可以正确处理这个问题,但是IE使用元素的宽度并且缩放之前的内容来适应它。 的jsfiddle

 #mydiv1 { width: 200px; height: 30px; background: green; } #mydiv1:before { content: url("data:url or /standard/url.svg"); } 

解决方法是将大小设置为之前的元素,并保持它的位置:

 #mydiv2 { width: 200px; height: 30px; background: green; } #mydiv2:before { content: url("data:url or /standard/url.svg"); display: inline-block; width: 16px; //only one size is alright, IE scales uniformly to fit it } 

background-image + background-size解决scheme也适用,但是不太方便,因为您必须指定两次相同的大小。

IE11中的结果:

IE渲染

 <div class="author_">Lord Byron</div> 
 .author_ { font-family: 'Playfair Display', serif; font-size: 1.25em; font-weight: 700;letter-spacing: 0.25em; font-style: italic; position:relative; margin-top: -0.5em; color: black; z-index:1; overflow:hidden; text-align:center; } .author_:after{ left:20px; margin:0 -100% 0 0; display: inline-block; height: 10px; content: url(data:image/svg+xml,%0A%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22120px%22%20height%3D%2220px%22%20viewBox%3D%220%200%201200%20200%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%3E%0A%20%20%3Cpath%20stroke%3D%22black%22%20stroke-width%3D%223%22%20fill%3D%22none%22%20d%3D%22M1145%2085c17%2C7%208%2C24%20-4%2C29%20-12%2C4%20-40%2C6%20-48%2C-8%20-9%2C-15%209%2C-34%2026%2C-42%2017%2C-7%2045%2C-6%2062%2C2%2017%2C9%2019%2C18%2020%2C27%201%2C9%200%2C29%20-27%2C52%20-28%2C23%20-52%2C34%20-102%2C33%20-49%2C0%20-130%2C-31%20-185%2C-50%20-56%2C-18%20-74%2C-21%20-96%2C-23%20-22%2C-2%20-29%2C-2%20-56%2C7%20-27%2C8%20-44%2C17%20-44%2C17%20-13%2C5%20-15%2C7%20-40%2C16%20-25%2C9%20-69%2C14%20-120%2C11%20-51%2C-3%20-126%2C-23%20-181%2C-32%20-54%2C-9%20-105%2C-20%20-148%2C-23%20-42%2C-3%20-71%2C1%20-104%2C5%20-34%2C5%20-65%2C15%20-98%2C22%22%2F%3E%0A%3C%2Fsvg%3E%0A); } .author_:before { right:20px; margin:0 0 0 -100%; display: inline-block; height: 10px; content: url(data:image/svg+xml,%0A%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22120px%22%20height%3D%2220px%22%20viewBox%3D%220%200%201200%20130%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%3E%0A%20%20%3Cpath%20stroke%3D%22black%22%20stroke-width%3D%223%22%20fill%3D%22none%22%20d%3D%22M55%2068c-17%2C6%20-8%2C23%204%2C28%2012%2C5%2040%2C7%2048%2C-8%209%2C-15%20-9%2C-34%20-26%2C-41%20-17%2C-8%20-45%2C-7%20-62%2C2%20-18%2C8%20-19%2C18%20-20%2C27%20-1%2C9%200%2C29%2027%2C52%2028%2C23%2052%2C33%20102%2C33%2049%2C-1%20130%2C-31%20185%2C-50%2056%2C-19%2074%2C-21%2096%2C-23%2022%2C-2%2029%2C-2%2056%2C6%2027%2C8%2043%2C17%2043%2C17%2014%2C6%2016%2C7%2041%2C16%2025%2C9%2069%2C15%20120%2C11%2051%2C-3%20126%2C-22%20181%2C-32%2054%2C-9%20105%2C-20%20148%2C-23%2042%2C-3%2071%2C1%20104%2C6%2034%2C4%2065%2C14%2098%2C22%22%2F%3E%0A%3C%2Fsvg%3E%0A); } 
  <div class="author_">Lord Byron</div>