Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positioning SVG elements using CSS

Tags:

css

svg

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?

like image 792
Yorick Sijsling Avatar asked Feb 03 '10 17:02

Yorick Sijsling


People also ask

How do I change position in SVG?

As mentioned in the other comment, the transform attribute on the g element is what you want. Use transform="translate(x,y)" to move the g around and things within the g will move in relation to the g .

Can you style SVG with CSS?

Not only does it mean that SVG properties can be styled using CSS as presentation attributes or in style sheets, but this also can be applied to CSS pseudo-classes such as :hover or :active . SVG 2 also introduces more presentation attributes that can be used as styling properties.

Can we write CSS selector for SVG element?

SVG elements doesn't support the standard xpath format. It has a different format. 2. Shadow DOM elements don't support xpath so we can write only cssSelector for shadow DOM elements.

Can you nest SVG elements?

You can use a nested SVG to group elements together and then position them inside the parent SVG. Now, you can also group elements together and position them using the <g> group—by wrapping elements inside a group <g> element, you can position them on the canvas by using the transform attribute.


2 Answers

I've managed to move some SVG text in chrome using the following CSS:

text.identity{ transform: translate(74px,0px); -ms-transform: translate(74px,0px); /* IE 9 */ -webkit-transform: translate(74px,0px); /* Safari and Chrome */ -o-transform: translate(74px,0px); /* Opera */ -moz-transform: translate(74px,0px); /* Firefox */ } 

However, it's not budging in Firefox...

like image 64
16 revs, 12 users 31% Avatar answered Sep 22 '22 22:09

16 revs, 12 users 31%


I came here looking for a fix but fixed the issue myself, thought I would share:

transform: translate(100px, 100px) 

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:

<path> <polygon> <g> 

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 with transform: 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.

like image 44
ryanpither Avatar answered Sep 23 '22 22:09

ryanpither