我想在CSS中给SVG text
的背景涂上类似于background-color
的 colored颜色
我只能在fill
上找到文档,它为文本本身着色
有可能吗?
我想在CSS中给SVG text
的背景涂上类似于background-color
的 colored颜色
我只能在fill
上找到文档,它为文本本身着色
有可能吗?
不,这是不可能的,SVG元素没有background-...
presentation attributes.
要模拟这种效果,可以在文本属性后面画一个fill="green"
或类似的矩形(过滤器).使用JavaScript,您可以执行以下操作:
var ctx = document.getElementById("the-svg"),
textElm = ctx.getElementById("the-text"),
SVGRect = textElm.getBBox();
var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
rect.setAttribute("x", SVGRect.x);
rect.setAttribute("y", SVGRect.y);
rect.setAttribute("width", SVGRect.width);
rect.setAttribute("height", SVGRect.height);
rect.setAttribute("fill", "yellow");
ctx.insertBefore(rect, textElm);