Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply z-index to SVG path

Tags:

css

svg

z-index

Is there a way how to apply z-index on a SVG path?

I have simple SVG arrow:

<svg class="arrow">
  <path class="line" />
  <path class="endpoint"/>
</svg>

If I use css to chnge z-index of the whole .arrow everything works fine, but when I try to apply it only on .endpoint it doesn't work.

Is there some solution or workaround for this?

Thank you very much.

like image 907
Aida_Aida Avatar asked Nov 13 '13 16:11

Aida_Aida


People also ask

Does Z-Index work with SVG?

Contrary to the rules in CSS 2.1, the z-index property applies to all SVG elements regardless of the value of the position property, with one exception: as for boxes in CSS 2.1, outer 'svg' elements must be positioned for z-index to apply to them.

Can SVG be draggable?

One of the most common forms of interaction on a computer is clicking and dragging. I use it a lot for interactive demos, such as those in my SVG tutorial, and have a built a library for making simple draggable SVG diagrams.

Is SVG an Htmlelement?

The HTML <svg> element is a container for SVG graphics. SVG has several methods for drawing paths, boxes, circles, text, and graphic images.

How do I move a SVG element to the front?

If you use the svg. js library, you can use the ". front()" command to move any element to the top.


2 Answers

As an alternative, you can use "< use >"

Example:

<svg>
    <circle id="shape1" cx="20" cy="50" r="40" fill="yellow" />
    <rect id="shape2" x="4" y="20" width="200" height="50" fill="grey" />
    <circle id="shape3" cx="70" cy="70" r="50" fill="blue" />
    <use id="use" xlink:href="#shape2" />
</svg>

The shape2 layer will be the top most layer.

like image 172
shibualexis Avatar answered Oct 23 '22 01:10

shibualexis


There's no z-index in SVG, it uses painters model. You'd have to remove the path from the DOM and re-add it before any elements that should be on top of it and after any elements that it should be on top of.

As you've discovered z-index only works on the complete <svg> content as the html renderer controls how that is positioned before handing off to the SVG renderer to draw the internal SVG contents.

like image 22
Robert Longson Avatar answered Oct 23 '22 01:10

Robert Longson