Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG translate with em as unit?

Tags:

svg

Is there a way to use em as unit for SVG translations? As in

<rect height="10em" width="10em" transform="translate(0em, 10em)"
 style="fill:none;stroke-width:3;stroke:black/>

The rectangle does not translate in Firefox, unless I remove the em as unit.

like image 796
user1357415 Avatar asked Apr 25 '12 23:04

user1357415


People also ask

How do I set units in SVG?

You specify the size of the viewport using the width and height attributes on the outermost <svg> element. In SVG, values can be set with or without a unit identifier. A unitless value is said to be specified in user space using user units.

What is EM in SVG?

Em units. Equivalent to the computed font-size in effect for an element. ex. Ex units. Equivalent to the height of a lower-case letter in the font (and font-size) in effect for an element.

Can SVG use REM?

SVG elements don't support CSS rem units for height/width attributes.

What is viewBox in SVG?

The viewBox attribute defines the position and dimension, in user space, of an SVG viewport. The value of the viewBox attribute is a list of four numbers: min-x , min-y , width and height .


1 Answers

You can sort of do this if you wrap the element(s) you want to translate in a new coordinate system:

<svg>
  <svg width="1em" height="1em" overflow="visible" viewBox="0 0 1 1">
    <rect height="10" width="10" transform="translate(0, 10)" .../>
  </svg>
</svg>

Another option if you only need translations and use elements that have x and y attributes (or equivalent) is to use those instead, like this:

<rect x="0" y="10em" height="10em" width="10em" 
 style="fill:none;stroke-width:3;stroke:black/>

A new specification for transforms in CSS/SVG is currently being worked on, and it will indeed allow units in translations, see here.

like image 164
Erik Dahlström Avatar answered Sep 28 '22 09:09

Erik Dahlström