Assume the following svg document:
<svg version="1.1" baseProfile="full" width="300" height="200" xmlns="http://www.w3.org/2000/svg">
<text x="20" y="20">My text</text>
</svg>
Now what i want to do is reposition this text using css.
I have tried adding style="dx:20"
and style="transform: translate(20)"
. Both have no effect in firefox and safari. Adding these as normal attributes works fine but then i can't split the positioning from the actual code.
Setting x
, y
, left
and top
in the style isn't working either.
Is there a way to position an svg element using css?
polygon r="7" id="map_points_55" points="23,10 15,27 34,16 10,16 31,27" style="fill:lime;stroke:purple;stroke-width:0;fill-rule:nonzero;"
if you want to move the star then add to 10 or more in points like points="33,20 25,37 44,26 20,26 41,37"
Here is a hacky possibility to position specifically text-elements purely by CSS, by abusing the attributes ‘letter-spacing’ for the x-coordinate and ‘baseline-shift’ for the y-coordinate:
‘baseline-shift’ is only applicable on ‘tspan’ Elements, thus making the inner
<tspan>
necessary in the presented code. Positive values for baseline-shift move the text upwards, opposite of the normal direction in the svg.‘letter-spacing’ needs an initial letter to have an effect, thus making the
.
necessary. To eliminate the width of this first letter, we use the special made fontcssPosAnchor
, where the dot has no width and no shape. The following<tspan>
additionally restores font and letter-spacing.Scope
Should work in every conforming SVG implementation.
There is one indefinite limitation though for negative x-coordinates. The specification for the ‘letter-spacing’ attribute says: “Values may be negative, but there may be implementation-specific limits.”
Compatibility
Text ‘direction’ change should work just fine, when imposed on the inner
<tspan>
.A non-standard ‘writing-mode’ must be imposed on the outer
<text>
. There will most certainly be problems with that.The probably more important ‘text-anchor’ values middle and end can be made possible like this:
The
‹space›.
before the closing<\text>
tag produces spacing equal to minus x-coordinate. So the inner<tspan>
is moved around but preserves it's space in the<text>
as if it was still there.Since there may be implementation-specific limits on negative values for the spacing attributes, this is not guaranteed to work on all clients!
At the moment, it seems -according to Shelley Powers, in her A List Apart Article "Using SVG for Flexible, Scalable and Fun Backgrounds: Part I" and "Part II"- that CSS is not currently best-suited to positioning of SVG. In fact it seems to be quite a minefield to incorporate SVG into a web-page, without directly embedding it within the html itself.
I hope that there are solutions to be found, and, indeed, Powers does offer a couple of workarounds, to enable proper separation of style and content for SVG. I'd hazard a guess that the current problems are the relative new-ness of the concept/standard (relative to, for example,
.gif
or even.png
...), sadly.I'm sorry I can't offer a better answer. =/
I've managed to move some SVG text in chrome using the following CSS:
However, it's not budging in Firefox...
Use css positioning:
index.html
style.css
I came here looking for a fix but fixed the issue myself, thought I would share:
Appears to work in all modern browsers except for Internet Explorer, right up until Microsoft Edge (which isn't even out yet) which is quite disappointing. The elements I've tested on are:
The only issue I've had is with
<text>
elements, and the solution there is to wrap the<text>
in a<g>
and apply the transformation to that. That should also work for any elements I haven't yet tested that have issues withtransform: translate()
.I haven't found a decent fallback for Internet Explorer, instead I've made sure that the transforms aren't vital to the function of the SVG.